GATE 2023
September 19, 2023GATE 2023
September 21, 2023Programming-for-Output-Problems
Question 4 |
Consider the following C program:
#include <stdio.h> int main() { int a[] = {2, 4, 6, 8, 10} ; int i, sum = 0, *b = a + 4 ; for (i = 0; i < 5; i++) sum = sum + (*b - i) - *(b - i) ; printf ("%d\n", sum) ; return 0 ; }
The output of the above C program is _____.
3 | |
7 | |
11 | |
10 |
Question 4 Explanation:
///////////////////////////////////PROGRAM
#include
int main()
{
int a[] = {2,4,6,8,10};
int i, sum = 0, *b = a+4;
for(i=0; i<5; i++)
{ printf(“*b, (*b-i): %d , %d\n”,*b, (*b-i) );
printf(“*(b-i): %d\n”,*(b-i) );
printf(“sum = %d + %d – %d\n”,sum, (*b-i),*(b-i));
sum = sum + (*b-i) – *(b-i);
printf(“sum = %d\n”, sum);
}
printf(“%d\n”, sum);
return 0;
}
//////////////////////////////OUTPUT
*b, (*b-i): 10 , 10
*(b-i): 10
sum = 0 + 10 – 10
sum = 0
*b, (*b-i): 10 , 9
*(b-i): 8
sum = 0 + 9 – 8
sum = 1
*b, (*b-i): 10 , 8
*(b-i): 6
sum = 1 + 8 – 6
sum = 3
*b, (*b-i): 10 , 7
*(b-i): 4
sum = 3 + 7 – 4
sum = 6
*b, (*b-i): 10 , 6
*(b-i): 2
sum = 6 + 6 – 2
sum = 10
10
#include
int main()
{
int a[] = {2,4,6,8,10};
int i, sum = 0, *b = a+4;
for(i=0; i<5; i++)
{ printf(“*b, (*b-i): %d , %d\n”,*b, (*b-i) );
printf(“*(b-i): %d\n”,*(b-i) );
printf(“sum = %d + %d – %d\n”,sum, (*b-i),*(b-i));
sum = sum + (*b-i) – *(b-i);
printf(“sum = %d\n”, sum);
}
printf(“%d\n”, sum);
return 0;
}
//////////////////////////////OUTPUT
*b, (*b-i): 10 , 10
*(b-i): 10
sum = 0 + 10 – 10
sum = 0
*b, (*b-i): 10 , 9
*(b-i): 8
sum = 0 + 9 – 8
sum = 1
*b, (*b-i): 10 , 8
*(b-i): 6
sum = 1 + 8 – 6
sum = 3
*b, (*b-i): 10 , 7
*(b-i): 4
sum = 3 + 7 – 4
sum = 6
*b, (*b-i): 10 , 6
*(b-i): 2
sum = 6 + 6 – 2
sum = 10
10
Correct Answer: D
Question 4 Explanation:
///////////////////////////////////PROGRAM
#include
int main()
{
int a[] = {2,4,6,8,10};
int i, sum = 0, *b = a+4;
for(i=0; i<5; i++)
{ printf(“*b, (*b-i): %d , %d\n”,*b, (*b-i) );
printf(“*(b-i): %d\n”,*(b-i) );
printf(“sum = %d + %d – %d\n”,sum, (*b-i),*(b-i));
sum = sum + (*b-i) – *(b-i);
printf(“sum = %d\n”, sum);
}
printf(“%d\n”, sum);
return 0;
}
//////////////////////////////OUTPUT
*b, (*b-i): 10 , 10
*(b-i): 10
sum = 0 + 10 – 10
sum = 0
*b, (*b-i): 10 , 9
*(b-i): 8
sum = 0 + 9 – 8
sum = 1
*b, (*b-i): 10 , 8
*(b-i): 6
sum = 1 + 8 – 6
sum = 3
*b, (*b-i): 10 , 7
*(b-i): 4
sum = 3 + 7 – 4
sum = 6
*b, (*b-i): 10 , 6
*(b-i): 2
sum = 6 + 6 – 2
sum = 10
10
#include
int main()
{
int a[] = {2,4,6,8,10};
int i, sum = 0, *b = a+4;
for(i=0; i<5; i++)
{ printf(“*b, (*b-i): %d , %d\n”,*b, (*b-i) );
printf(“*(b-i): %d\n”,*(b-i) );
printf(“sum = %d + %d – %d\n”,sum, (*b-i),*(b-i));
sum = sum + (*b-i) – *(b-i);
printf(“sum = %d\n”, sum);
}
printf(“%d\n”, sum);
return 0;
}
//////////////////////////////OUTPUT
*b, (*b-i): 10 , 10
*(b-i): 10
sum = 0 + 10 – 10
sum = 0
*b, (*b-i): 10 , 9
*(b-i): 8
sum = 0 + 9 – 8
sum = 1
*b, (*b-i): 10 , 8
*(b-i): 6
sum = 1 + 8 – 6
sum = 3
*b, (*b-i): 10 , 7
*(b-i): 4
sum = 3 + 7 – 4
sum = 6
*b, (*b-i): 10 , 6
*(b-i): 2
sum = 6 + 6 – 2
sum = 10
10
Subscribe
Login
0 Comments