GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Nov-2004, 08:28
agentxx04 agentxx04 is offline
New Member
 
Join Date: Sep 2004
Posts: 12
agentxx04 is on a distinguished road
Question

What's wrong with my program?


Hi. Our assignment was to write a program that finds the mean, median and mode of up to 50 integers entered by the user. My professor returned my assignment & said that my program is supposed to work for up to 50 integers, not just 50. If anyone could please look at my program & help me figure out how I could correct, I would really appreciate it.
Thank You,

CPP / C++ / C Code:
#include <stdio.h>

float getMedian(int item[]);
float getAvg(int item[]);
void getMode(int item[]);

int main()
{
	int item[100];
	int a, b, t, mode;
	int median_index;
	float median, avg;


	/*Type the 50 integers*/
	printf("Enter 50 integers: * Press enter between each integer* ");
	for(a=0; a<50; a++){
		scanf("%d", &item[a]);
	}
	/*Bubble Sort*/
	for(a=1; a<50; a++)
		for(b=50-1; b>=a; --b){
			if(item[b-1] > item[b]){
				t=item[b-1];
				item[b-1] = item[b];
				item[b]=t;
			}
		}
/*median*/

median = getMedian(item);
printf("The Median is %.1f\n", median);

/*average*/

avg = getAvg(item);
printf("Average is: %.1f\n", avg/50);

/*mode*/
getMode(item);


return 0;
}

float getMedian(int item[])
{
	float median;
	int median_index;

	median = 50/2;
	if(50%2 ==1){
		median = item[50/2];
	}
	else {
		median_index = 50/2;
		median = (item[median_index]+item[median_index-1])/2.0;
	}
	return median;
}

float getAvg(int item[])
{
	float avg;
	int a;
	avg = 0;
	for(a = 0; a < 50; a++){
		avg = avg + item[a];
	}
	return avg;
}

void getMode(int item[])
{
	int a, b, j, i, mode;
	int singles[50];
	int frequency[50];

	/*Another Sort*/
	for(i = 0; i < 50; i++)
	{
		singles[i] = -1;
		frequency[i] = 0;
	}
	mode = 0;
	for(a=0; a<50; a++){
		for(b=0; b < 50; b++)
		{
			if(singles[b] == item[a])
			{
				frequency[b]++;
				if(mode < frequency[b])
				{
					mode = frequency[b];
				}
				break;
			}
			else
			if(singles[b]== -1)
			{
				singles[b] = item[a];
				frequency[b]++;
				if(mode < frequency[b])
				{
					mode = frequency[b];
				}
				break;
			}
		}
	}


	for(j=0; j < 50; j++)
	{
		if(frequency[j] == frequency[b])
		{
			mode= frequency[j];
			printf("Mode is: %d\n", singles[j]);
		}
		else if(singles[j]==0)
			printf("0");

	}
}
/* end of program */
Attached Files
File Type: txt program.txt (2.3 KB, 21 views)
  #2  
Old 23-Nov-2004, 09:21
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by agentxx04
Hi. Our assignment was to write a program that finds the mean, median and mode of up to 50 integers entered by the user. My professor returned my assignment & said that my program is supposed to work for up to 50 integers, not just 50.


Well, the idea is that you don't know beforehand how many items are going to be entered. So your program has to be able to tell when the end of the input data occurs.

Three methods come to mind:

1. Use a special integer that will never be part of the data set, lets say -9999. Then when the user enters that number, break out of the input loop.

I don't really like this, since it restricts the data set that can be valid numbers.

2. Break out of the loop if the program encounters End-of-File. This is the one to use if input comes from a file rather than user keyboard input. (End-of-File on Windows systems is usually ctrl-Z, but on Linux systems, and Windows systems running, say Cygwin and Bash, it's ctrl-D).

3. Break out of the loop if the program encounters a non-integer. This is the one that I like. You really should be testing user input for non-integers anyhow.

Then your input loop looks like this:
CPP / C++ / C Code:

   int temp; /* at the beginning of main()*/
 



  for(a=0; a<50; a++){
    
    if(scanf("%d", &temp) != 1) {/* non-integer was entered */
      break;
    }
    else {
       item[a] = temp;
    }
  }
  /* Now check the value of a to see how many were entered */

Now, you only have to make sure that the rest of the program works as advertised with the actual number of data items.

Regards,

Dave
Last edited by davekw7x : 23-Nov-2004 at 09:54.
  #3  
Old 23-Nov-2004, 20:48
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by davekw7x
1. Use a special integer that will never be part of the data set, lets say -9999. Then when the user enters that number, break out of the input loop.
I actually like this way, at least for a beginner. Later you can get fancy.

1) Define MaxNum and initialize it to 0
2) Use a while loop instead of a for loop to input your values
3) For every value that gets entered, increment MaxNum
4) Change all your 50's to MaxNum
That should do it.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogMore photos on Flickr by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 13:30
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
Something's wrong w/my program. agentxx04 C Programming Language 1 07-Nov-2004 10:03
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 20:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.