Thread: Trouble understanding code to calculate sum of n numbers

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Question 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.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by jackloring View Post
    Code:
    	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.


    Quote Originally Posted by jackloring View Post
    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.

  3. #3
    C-no_Ob Bennie98's Avatar
    Join Date
    Oct 2010
    Location
    Ledeberg, Ghent, East-Flanders, Belgium
    Posts
    49
    Quote Originally Posted by JohnGraham View Post
    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
    Because Tetris Is Unrealistic.

    "The fear of death is the most unjustified of all fears, for there's no risk of accident for someone who's dead." - Albert Einstein

    "The Edge... there is no honest way to explain it because the only people who really know where it is are the ones who have gone over." - Hunter S. Thompson

    "I never think of the future. It comes soon enough." - Albert Einstein

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bennie98 View Post
    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.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Thanks for the replies, all. I understand it much better now. The program actually still works without the _getch () line. Just a quick question, though... what does \t actually do?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackloring
    what does \t actually do?
    Escape sequence for a tab, so '\t' is a literal tab character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    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.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CommonTater View Post
    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.
    Neither _getch() nor getch() are standard, but that doesn't matter in this case anyway. In jack's code, the line is immediately after a line "return 0;" in main(), so will never be executed.
    Right 98% of the time, and don't care about the other 3%.

    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.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    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?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jackloring View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum of the numbers between two integers
    By Sridar in forum C++ Programming
    Replies: 16
    Last Post: 02-01-2011, 10:44 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Replies: 1
    Last Post: 10-01-2001, 10:39 AM

Tags for this Thread