tricky interview question for C++
- by Alexander
Given the code below. How would you create/implement SR.h so that it produces the correct output WITHOUT any asterix in your solution? I got bummed by this question
#include <cstdio>
#include "SR.h"
int main()
{
int j = 5;
int a[] = {10, 15};
{
SR x(j), y(a[0]), z(a[1]);
j = a[0];
a[0] = a[1];
a[1] = j;
printf("j = %d, a = {%d, %d}\n", j, a[0], a[1]);
}
printf("j = %d, a = {%d, %d}\n", j, a[0], a[1]);
}
//output
j = 10, a = {15, 10}
j = 5, a = {10, 15}
#include <cstdio>
#include "SR.h"
int main()
{
int sum = 0;
for (int i = 1; i < 100; i++) {
SR ii(i);
while (i--)
sum += i;
}
printf("sum = %d\n", sum);
}
//The output is "sum = 161700".