The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+1 vote
2.1k views

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 _______

asked in Programming by Veteran (414k points)
edited by | 2.1k views

1 Answer

+9 votes
Best answer
$\text{sum}=0, *b = a+4  \text{ i.e.pointing to }10$

$\text{sum}= \text{sum}+ (*b-i) - *(b-i)$

$i=0$
$\text{sum}= 0+ (10-0) - (10) = 0$

$i=1$
$\text{sum}= 0 + (10-1) - (8) = 1$

$i=2$
$\text{sum}= 1 + (10-2) - (6) = 3$

$i=3$
$\text{sum}= 3 + (10-3) - (4) = 6$

$i=4$
$\text{sum}= 6 + (10-4) - (2) = 10$
answered by Veteran (59.9k points)
edited by
+1
For i=4,
You took sum = 7 while in i=3 sum is 6
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,811 questions
54,529 answers
188,403 comments
75,480 users