Write a program which stores 10 decimal numbers in an array.
For these numbers accept input from the users. Once the array is populated do
the followings:
Display each elements of the array
Display the sum of all the elements of array
Once you've declared the array, you can use for loops to populate the elements then print them out. Remember that element numbering in arrays starts at 0 instead of 1.
If you post your code (use the <> button in the Format palette at the right to format the code part) someone here can help if you're running into problems.
#include <stdio.h>
int main()
{
int values[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, i, p=0;
for (i = 0; i <= 9; i++)
printf("%d ", values[i]);
for (i = 0; i <= 9; i++)
p += values[i];
printf("\n\nthe total of all array elements is %d", p);
return 0;
}
values[] is unsized array. language will account for amount of integers, or
you can put [10] in the brackets. first for loop prints integers of array to screen.
second for loop adds integers in array stores to p.
#include <stdio.h>
int main()
{
int values[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, i, p=0;
// both values to screen and add elements to one loop
for (i = 0; i <= 9; i++)
{
printf("%d ", values[i]);
p += values[i];
}
printf("\n\nthe total of all array elements is %d", p);
return 0;
}