Trouble understanding code to calculate sum of n numbers

This is a discussion on Trouble understanding code to calculate sum of n numbers within the C Programming forums, part of the General Programming Boards category; Hey all,
I am having trouble understanding all of this code to give the sum of n numbers. I think ...

Trouble understanding code to calculate sum of n numbers

Hey all,

I am having trouble understanding all of this code to give the sum of n numbers. I think I understand most of it, but there are a few lines which don't make too much sense to me. If anyone could dive deeper into an explanation that would be great!

Thanks

Code:

#include <stdio.h>
#include <conio.h>
int main ()
{
int i, a[10], sum=0, n;//declares integers i, a[10] (a single demensional array* subscript), sum=0 (to initialize), and n
printf("Enter the amount of numbers:");//prompts user to enter the desired amount of numbers
scanf("%d", &n);//reads the input from the keyboard
printf("Enter the numbers\n");//prompts user to enter the actual numbers as an array
for(i=0; i<n; i++)//for loop: for i, i=0, i<n (the desired amount of numbers), and i incremented
{
scanf("%d", &a[i]);//scans input from keyboard and the array specifications entered by user as defined by the ints i
}
for(i=0; i<n; i++)//for loop: for i, i=0, i<n (the desired amount of numbers), and i incremented
printf("%d", a[i]);//prints... I can't understand this line
for(i=0; i<n; i++)//for loop: for i, i=0, i<n (the desired amount of numbers), and i incremented
{
sum=sum+a[i];//regarding for loop, sum now = sum (0) + the array specifications entered by user as defined by the ints i
}
printf("sum=\t%d", sum);//prints the sum calculated by the previous for loops
return 0;//Can't understand this line either
_getch();Can't understand this line either
}
//*a one dimensional array subscript which is a data script that allows the collection of variables each defined by one or more integer indices.
//can store ten elements in it (0-9). Array indices always begin at the int 0 in C programming.

for(i=0; i<n; i++)//for loop: for i, i=0, i<n (the desired amount of numbers), and i incremented
printf("%d", a[i]);//prints... I can't understand this line

printf() is used here like it's used later (as in the line "printf("sum=\t%d", sum);"). The only difference is that only the number is printed, and that number is a[i], the i-th element of a.

Originally Posted by jackloring

Code:

return 0;//Can't understand this line either
_getch();Can't understand this line either

Do you understand how to return from a function? If not, looking for tutorials on functions and return values will help. Returning from main() is equivalent to returning a value from your program.

I assume _getch() is from the Windows API, so I can only give you my guess that it waits for a character to be entered at the terminal. However, that doesn't matter as this statement will never be reached, since the previous "return" statement leaves the function.

I assume _getch() is from the Windows API, so I can only give you my guess that it waits for a character to be entered at the terminal. However, that doesn't matter as this statement will never be reached, since the previous "return" statement leaves the function.

normally people will present getch(); to be a good way to have your program stay open even when it's done.. typing something then leaves the program ...

it's not a good way, I used to use this too but when i changed to Pelles C IDE the compiler didn't accept that statement.
So it's very dependable on which compiler you're using..
but it's in no way a good statement, I would try to forget that one...

I could be wrong too... because you use _getch(); and not getch(); which I was using

it's not a good way, I used to use this too but when i changed to Pelles C IDE the compiler didn't accept that statement.
So it's very dependable on which compiler you're using..
but it's in no way a good statement, I would try to forget that one...

I could be wrong too... because you use _getch(); and not getch(); which I was using

Actually if you look it up the PellesC helpfile... It will do _getch() not getch()... It also says Not Standard C in red at the top of the page.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Aha, so \t is just something like \n, then. Another question struck me last night: why is it a good idea to define the a[] as a[10] in the beginning? When I leave the array blank, it doesn't seem to create a problem, although my debugger freaks out about it. Any ideas?

Aha, so \t is just something like \n, then. Another question struck me last night: why is it a good idea to define the a[] as a[10] in the beginning? When I leave the array blank, it doesn't seem to create a problem, although my debugger freaks out about it. Any ideas?

It's about reserving memory. Every variable, array, structure, etc. in C has to have it's own bit of protected memory. This is to prevent other information from overwriting it and to prevent it from overwriting other information. In many cases, where the size is known at compile time C will do this for you... for example it knows how big an int is and will automatically reserve the right amount of memory. However, arrays, structs, strings etc. require you to tell the compiler how much to set aside. A[] is of an unknown size, so the compiler cannot reserve memory for it and assumes that --good programmers we all are-- we're going to take care of that ourselves.

But with no memory reservation, when using the array you will be writing to an unknown memory location that just might cause some real problems for your system. Your data might overwrite data from another program (or the system) or data from other sources might clobber yours.