I am working on a program in c and I am trying to declare a ten element array, using a for loop to insert the elements. I am also using a second for loop to acquire the sum of all the elements in the array. I'm new to programming so I am curious where I am going wrong with this program.

1. #include <stdio.h>
2. void main()
3. {
4. int sum = 0;
5. int arayvalues[10];
6. int n = 0;
7. for (n = 0; n < 10; n = n + 1)
8. {
9. printf("Input an integer:");
10. scanf"("%d", &arayvalues);
11. }
12. int i = 0;
13. for(i = 0; i < 5; i = i + 1)
14. {
15. sum = sum + arayvalues;
16. }
17. printf("The sum is:", sum);
18. return;
19. }

Not a bad first effort but there are a few things: main() always returns an int, and is not void.

You need to specify that you want &arayvalues[n] on line #10.

Your second loop should be to 10 unless you want to add only the first 5 values together. Give yourself a small challenge and see if you can do it all in one loop.

Finally, just a stylistic thing but n = n+1 is usually abbreviated n++ (same for i obviously). If you want your code to be C89 compliant you should move line 12 up after line 6, at the top of the block with the others.

Edited 6 Years Ago by jonsca: n/a

You need to specify that you want &arayvalues[n] on line #10.

Your second loop should be to 10 unless you want to add only the first 5 values together. Give yourself a small challenge and see if you can do it all in one loop.

Hello
line 10;

scanf"("%d", &arayvalues);

should be

scanf("%d", &arayvalues[n]);

Specify where you want to store the integer value in the array thats just about it..

code will finally be

#include <stdio.h>
int sum = 0;
    int arayvalues[10];
    int n = 0;

    for (n = 0; n < 10; n = n + 1) {
        printf("Input an integer:");
        scanf("%d", &arayvalues[n]);
    }

    int i = 0;
    //addition of elements in array.

    for (i = 0; i < 10; i = i + 1) {
      
        sum = sum + arayvalues[i];
        
    }
    printf("The sum is:%d", sum);
return 0;

Not a bad first effort but there are a few things: main() always returns an int, and is not void.

You need to specify that you want &arayvalues[n] on line #10.

Your second loop should be to 10 unless you want to add only the first 5 values together. Give yourself a small challenge and see if you can do it all in one loop.

Finally, just a stylistic thing but n = n+1 is usually abbreviated n++ (same for i obviously). If you want your code to be C89 compliant you should move line 12 up after line 6, at the top of the block with the others.

i hv some q based on this ans im also a new
main() alwys returns an int bt progrm also runs when main returns void... and plz explain it and why main reqr return type

Comments
Dnt hjck othr ppls thrds -- and this is not SMS!!

Madawar, thank you very much. It worked great. I had been staring at the same program for too long, and I missed all the syntax errors. I just needed someone else to point it out.

Thanks Again

This question has already been answered. Start a new discussion instead.