# Find the sum of numbers in an array in C

Page 1 of 1

## 8 Replies - 1643 Views - Last Post: 30 May 2012 - 10:05 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=281262&amp;s=a47f40a61476df8a691822104ddb49c5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Midi_

Reputation: 9
• Posts: 111
• Joined: 22-May 12

# Find the sum of numbers in an array in C

Posted 30 May 2012 - 06:38 PM

Ok so this is my code. If you look under the 'case 2:' part, I am trying to find the sum of all the numbers inputed into an array from 'case 1:" but it doesnt ever do it, and I am not sure why. ignore the commented out stuff. Any help would be much appreciated:)
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{

int x, j, i;
double mean;

mean = 1;
x = 1;

while( x != 3)
{
printf("Choose one of the following selections\n\n");

printf("1) Enter Data.\n");
printf("2) Display the data and following statistics:\n");
printf("(the number of date item, the high and low values in the data, the mean, median, mode, variance and standard deviation.)\n");
printf("3)Quit the Program\n\n");

printf("Option #:");
scanf("%d", &x);

switch(x)
{
float item[200];
char ch;
int i, sum;
sum = 0;

case 1:
i=0;
printf("Enter one data item after each prompt. Press return after each one.\n");
printf("Signal with <EOF> when you are done with data input. \n\n");

while ((ch = getchar()) != EOF)
{

printf("Item #:%d       :", i+1);
scanf("%f", &item[i]);
i++;
}
break;

case 2:

/*No of inputed items*/
printf("You have inputed %d items.\n", i -1);

/*Mean of inputed numbers*/
while( mean < i )
{

sum+=item[i];
//printf("Sum='%d'\n\n", sum);
//printf("i='%d'\n\n", i-1);
//printf("mean='%f'\n\n", mean);
//sum = 0.0;
//i--;
mean++;
}
printf("Sum='%d'\n\n", sum);
//printf("item[0] = %f item[1] = %f item[2] = %f\n\n", item[0] , item[1], item[2] );

break;

case 3:
break;

default:
printf("That was not a valid option\n");

}
}

return 1;

}

```

Is This A Good Question/Topic? 0

## Replies To: Find the sum of numbers in an array in C

### #2 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Find the sum of numbers in an array in C

Posted 30 May 2012 - 07:58 PM

Hi, you create the array inside the switch statement block (scope). Each time the switch statement is entered a new array is created.

```32	        float item[200];

```

So you need to have the array outside the switch statement block.

### #3 Midi_

Reputation: 9
• Posts: 111
• Joined: 22-May 12

## Re: Find the sum of numbers in an array in C

Posted 30 May 2012 - 08:19 PM

#define, on 30 May 2012 - 07:58 PM, said:

Hi, you create the array inside the switch statement block (scope). Each time the switch statement is entered a new array is created.

```32	        float item[200];

```

So you need to have the array outside the switch statement block.

'yeahh that didn't work. This is what im seeing, if this helps?

.

### #4 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Find the sum of numbers in an array in C

Posted 30 May 2012 - 08:41 PM

Hi, you use the i variable to store the number of entries. It is declared in the switch block as well. Don't know if that is all.

### #5 Midi_

Reputation: 9
• Posts: 111
• Joined: 22-May 12

## Re: Find the sum of numbers in an array in C

Posted 30 May 2012 - 08:46 PM

#define, on 30 May 2012 - 08:41 PM, said:

Hi, you use the i variable to store the number of entries. It is declared in the switch block as well. Don't know if that is all.

hmmm still not working:/

### #6 jimblumberg

Reputation: 5487
• Posts: 17,064
• Joined: 25-December 09

## Re: Find the sum of numbers in an array in C

Posted 30 May 2012 - 09:18 PM

Have you tried running your program through your debugger? Set a break point early in main and single step trough the program, inspecting the variables as you proceed.

Jim

### #7 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Find the sum of numbers in an array in C

Posted 30 May 2012 - 09:32 PM

What is the value of i here?

```64	        sum+=item[i];

```

Using a better variable name for the number of items could help.

### #8 Midi_

Reputation: 9
• Posts: 111
• Joined: 22-May 12

## Re: Find the sum of numbers in an array in C

Posted 30 May 2012 - 09:43 PM

#define, on 30 May 2012 - 09:32 PM, said:

What is the value of i here?

```64	        sum+=item[i];

```

Using a better variable name for the number of items could help.

the value of 'i' is the amount of #'s(items) entered into 'case 1'

### #9 Salem_c

• void main'ers are DOOMED

Reputation: 2160
• Posts: 4,225
• Joined: 30-May 10

## Re: Find the sum of numbers in an array in C

Posted 30 May 2012 - 10:05 PM

```\$ gcc -W -Wall -Wextra -O2 bar.c
bar.c: In function ‘main’:
bar.c:10: warning: unused variable ‘i’
bar.c:10: warning: unused variable ‘j’
bar.c:27: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
bar.c:47: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
bar.c:34: warning: ‘i’ may be used uninitialized in this function
bar.c:34: warning: ‘sum’ may be used uninitialized in this function

```

You should definitely move all those variables you declare in your switch to an outer scope (you have i declared in two scopes for instance).

The sum=0 on line 35 NEVER happens.