8 Replies - 883 Views - Last Post: 30 May 2012 - 10:05 PM Rate Topic: -----

#1 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • 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  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,963
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

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

Posted 30 May 2012 - 08:19 PM

View Post#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?

.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,963
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

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

Posted 30 May 2012 - 08:46 PM

View Post#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:/
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,432
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,963
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

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

Posted 30 May 2012 - 09:43 PM

View Post#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'
Was This Post Helpful? 0
  • +
  • -

#9 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1762
  • View blog
  • Posts: 3,417
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1