To solve this question I will first items the steps we need to follow then write the program that solves the problem. Note that array index begin with 0. That was why our array iterator was initialize to 0
- Define a length variable to hold the size of the array using macro.
- Declare an empty array.
- Declare a variable to hold the sum of the array and initialize it to zero.
- Collect the user input and store the values into the empty array using a for loop.
- Iterate through the element of the array using a second loop and sum all the array value.
- Finally print out the sum of the values.
Answer
// Online C compiler to run C program online
#include <stdio.h>
#define LENGTH 8
int main() {
// Write C code here
int array[LENGTH];
int sumArray=0;
for (int a = 0; a < LENGTH; a++){
printf("Enter value at array[%d]\n", a);
scanf("%d", &array[a]);
}
for (int b = 0; b < LENGTH; b++){
//printf("Enter value at array[%d]\n", a);
sumArray+=array[b];
}
printf("The sum of all values in the array is %d ", sumArray);
return 0;
}
Tags:
C