Thread: Arrays & Sums

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

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

4. Thanks for the help everyone.

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

5. I guess you can also do away with count variable. Instread you can use (i+1) instead.

6. Originally Posted by Machoscorpion
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. 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++) {```

8. Originally Posted by dwks
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. Originally Posted by mrlooneytoon
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. Originally Posted by KONI
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