66.2k views
5 votes
Int value[10] = {1, -7, 95, 123, 80, 67, -30, 17, 152, 121} ;

Given the declaration above, write a for loop that will add all the elements in an int variable called total.
C code

User Eduardohl
by
5.3k points

1 Answer

3 votes

Answer:

for loop code:

int total=0;

for(int i=0;i<10;i++)

{

total + = value[i];

}

Step-by-step explanation:

By declaration we know that the array is of size 10 which means the index will start from 0.

The variable name to be used to store the sum is given as total

We will initialize total with zero so that no garbage value is used.

For loop will be used to access the elements of array and calculate the sum. The syntax of for loop is given as:

for (initialization; condition; increment/decrement)

{statement}

Code:

int total=0;

for(int i=0;i<10;i++)

{

total + = value[i];

}

User Erik Ringsmuth
by
4.6k points