#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;
}
Find the sum of numbers in an array in C
Page 1 of 18 Replies - 332 Views - Last Post: 30 May 2012 - 10:05 PM
#1
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:)
Replies To: Find the sum of numbers in an array in C
#2
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.
So you need to have the array outside the switch statement block.
32 float item[200];
So you need to have the array outside the switch statement block.
#3
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.
So you need to have the array outside the switch statement block.
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?
.
Attached image(s)
#4
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
Re: Find the sum of numbers in an array in C
Posted 30 May 2012 - 08:46 PM
#6
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
Jim
#7
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?
Using a better variable name for the number of items could help.
64 sum+=item[i];
Using a better variable name for the number of items could help.
#8
Re: Find the sum of numbers in an array in C
Posted 30 May 2012 - 09:43 PM
#9
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|