declaring arrays

i need help with this

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
Last edited on
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.
closed account (1CfG1hU5)
this has all but user input from keyboard

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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;

 }

closed account (1CfG1hU5)
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.
Last edited on
closed account (1CfG1hU5)
also done this way. one less for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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;

 }

Topic archived. No new replies allowed.