Thread: Arrays & Sums

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    Arrays & Sums

    Hi everyone!

    I'm writing a program to perform statistical calculations on some electrical equipment. In my entire program, this one part is messing with me so I broke it up into one separate just to debug.

    Anyway, what the code below does is; define an array of 10 elements and each element is entered by the user. After each entry is entered the loop traverses and adds up the entered values to date printing out the sum to date. However at the end of the loop (ie: after entering the last value) the sum printed out is wrong.

    I have a feeling it's got something to do with the vars being floats and ints.
    PS: The code works fine if I declare "Resistor_Sum, Resistor[ArraySize]" as global vars but apparently this is bad practice and want to avoid this.

    Code:
    #include <stdio.h>	  // Needed for printf and scanf
    #include <stdlib.h>		// Needed for system()
    
    #define ArraySize 9		// Define a constant array size
    
    int main() {
    
    	// Define Variables
    	
    	// Number of elements in the array (this should be 10 after counting)
    	int count = 0;
    
    	// Main resistor array variable which user enters, sum of resistors entered
    	
    	float Resistor_Sum, Resistor[ArraySize];
    
    	for (int i = 0 ; i <= ArraySize ; i++) {
    		printf ("Enter resistor %d in [Ohms]: ", i);
    		scanf ("%f", &Resistor[i]);
    		count++;
    		Resistor_Sum += Resistor[i];
    
    		printf ("The sum is: %f, The count is: %d\n\n", Resistor_Sum, count);
    	}
    
    	system ("pause");
    
    	return 0;
    
    }
    Also below is a sample output:
    Code:
    Enter resistor 0 in [Ohms]: 10
    The sum is: 10.000000, The count is: 1
    
    Enter resistor 1 in [Ohms]: 20
    The sum is: 30.000000, The count is: 2
    
    Enter resistor 2 in [Ohms]: 30
    The sum is: 60.000000, The count is: 3
    
    Enter resistor 3 in [Ohms]: 40
    The sum is: 100.000000, The count is: 4
    
    Enter resistor 4 in [Ohms]: 50
    The sum is: 150.000000, The count is: 5
    
    Enter resistor 5 in [Ohms]: 60
    The sum is: 210.000000, The count is: 6
    
    Enter resistor 6 in [Ohms]: 70
    The sum is: 280.000000, The count is: 7
    
    Enter resistor 7 in [Ohms]: 80
    The sum is: 360.000000, The count is: 8
    
    Enter resistor 8 in [Ohms]: 90
    The sum is: 450.000000, The count is: 9
    
    Enter resistor 9 in [Ohms]: 100
    The sum is: 200.000000, The count is: 10
    
    Press any key to continue . . .
    Thank you for your help!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Your array is too small by one. Generally for an array of size N, loop from 0 to N-1 in a for loop using:
    Code:
    for ( i = 0; i < N; ++i)
    Your code has other issues that others will undoubtedly mention.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You also need to initialize Resistor_Sum before starting to add something to it... Don't you get any warnings? You should increase the warnng level to maximum
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    Thanks for the help everyone.

    My array wasn't big enough. It's now of size 10 with changes in the for loop.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    I guess you can also do away with count variable. Instread you can use (i+1) instead.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    Quote Originally Posted by Machoscorpion View Post
    I guess you can also do away with count variable. Instread you can use (i+1) instead.
    I'm passing 'count' into several different functions created in the original program and manipulating it so I will probably need that.

    Thanks for the tip though!

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    for (int i = 0 ; i <= ArraySize ; i++) {
    Declaring variables in for loop initializer sections is C99. To be compatible with C89, the more common standard of C, you'd declare the variable before the for loop.
    Code:
    int i;
    // [...]
    for(i = 0 ; i < ArraySize ; i++) {
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    Quote Originally Posted by dwks View Post
    Code:
    for (int i = 0 ; i <= ArraySize ; i++) {
    Declaring variables in for loop initializer sections is C99. To be compatible with C89, the more common standard of C, you'd declare the variable before the for loop.
    Code:
    int i;
    // [...]
    for(i = 0 ; i < ArraySize ; i++) {
    Done. Thanks for the tip!

  9. #9
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by mrlooneytoon View Post
    Done. Thanks for the tip!
    To be even more compatible with C89, you can't mix declarations with statements within your program, so you shouldn't do this:

    Code:
    int myArray[200];
    
    // some stuff here involving statements
    
    int i;
    for (i =0; i < 200; i++)
    {}
    The above example is not standard C89, you'd have to move the definition of "int i" above the statements for it to be compatible. Even though you should worry about that, I think C tries to avoid having to declare all the variables at top since it is deprecated (a historical requirement we don't need anymore).

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    Quote Originally Posted by KONI View Post
    To be even more compatible with C89, you can't mix declarations with statements within your program, so you shouldn't do this:

    Code:
    int myArray[200];
    
    // some stuff here involving statements
    
    int i;
    for (i =0; i < 200; i++)
    {}
    The above example is not standard C89, you'd have to move the definition of "int i" above the statements for it to be compatible. Even though you should worry about that, I think C tries to avoid having to declare all the variables at top since it is deprecated (a historical requirement we don't need anymore).
    The program does not compile if all my declarations are not in one spot. So yes, you are correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. Structures, Arrays, Sums...
    By Inept Pig in forum C Programming
    Replies: 19
    Last Post: 04-19-2002, 04:07 PM