Jump to content
xisto Community
Sign in to follow this  
Tran-Gate

A Program I Am Creating average, min, max, (mode), range, (median)

Recommended Posts

Hi, I am new to C++ and I tried creating a program but it didn't turn out so well..... Before I explain my problems, I will show you the program:

#include <iostream>using namespace std;int main() {	double num[99], min_val, max_val, avg, range;	int i;	char ch;	do {		cout << "Enter: ";		cin >> num[i];	}	while ( num[i] != 'e' );		for ( i = 0;; i++) {		avg = 0;		avg += num[i];	}	avg /= i;		cout << "\n\nAverage: " << avg << "\n";	i = 0;	min_val = max_val = i;	for ( i = 0; num[i]; i++) {		if (min_val > num[i]) min_val = num[i];		if (max_val < num[i]) max_val = num[i];	}		range = max_val - min_val;	cout << "Minimum: " << min_val << "\n";	cout << "Maximum: " << max_val << "\n";	cout << "Range: " << range << "\n";   	system("PAUSE");	return 0;	}

I don't know exactly how to end input for the numbers....I also do not know how to get the median and mode into the program....I have thought about it for a few days now but nothing clicks.

I have tried have a char value end the input but I didn't know how to....When I press 'e', the program goes crazy..... I am trying to get that fixed..... If anyone can help me or give me suggestions. I would very well appreciate it.

Thanks in advance~!!

Share this post


Link to post
Share on other sites

Sorry if this doesn't exactly work - I'm not a C++'er... but I'll try my best :lol: Everyone else - feel free to point out what I'm saying wrong!

 

A few things here:

If you use void instead of int, you won't have to return 0;

You defined num as a double - there's gonna be some problems if you try and put a string in there... Maybe you could instead use -1 as your end value?

I don't think you're incrementing i in the input loop, you should probably increment it and make sure it doesn't exceed 99

for ( i = 0;; i++) { might cause errors, or an infinite loop, or it might just skip past - you need something that it tests (i <= 99 maybe?)

for ( i = 0; num; i++) { will terminate if a 0 turns up, and it might act weirdly when i exceeds 99.

If your range is -5 to -3, max will record as 0. You should probably define max as the most negative a double can get.

If your range is 3 to 5, min will record as 0. You should probably define min as the most positive a double can get.

 

It might help to define a one byte integer (which is a char I assume) as the number of items you have - I'll call it c for simplicity. You could have it as your first input (not part of the loop), then in your input loop (as well as incrementing i) you test that i < c. And you can do the same test in your for loops!

Share this post


Link to post
Share on other sites

Hey guys, thanks for the help but if I put a number to end the input, it would count the number as part of the input....and this is not homework.... I self study, my school doesn't teach C++. I am a MS student.

Share this post


Link to post
Share on other sites

if I put a number to end the input, it would count the number as part of the input....

Not if you add the following code after your do-while statement:
num[i]=null;

Share this post


Link to post
Share on other sites

OK tran...As I promised you here is the better and easiest version of your program to find minimum, maximum and average.

Since you are at the starting stage of learing C++ I eliminated char values etc...to make the program easy to understand.

And my porgram will be C as I already told you I don't know C++ and you told me you can understand C. And my program is just to teach you the simple logic behind calculating these. You should make changes to printf and scanf things to cout and cin to make them work in C++ or save the program with .c extension and compile as usual. Remove clrscr() and getch() if they give any trouble.

 

OK, here is the program. First I will give the whole program and then, I will explain each part.

 

void main(){	   int number[50];	   int i,j,n,total=0,min=0,max=0,temp; 	   float avg;	   clrscr();//It clears the screen just like cls command	   printf("How many numbers you want to enter: ");	   scanf("%d",&n);	   for(i=0;i<n;i++)	   {			printf("Enter the number: ");			scanf("%d",&number[i]);		}	   printf("The numbers you entered are \n");	   for(i=0;i<n;i++)	   {		  printf("%d ",number[i]);	   } /*this is sorting section */	   for(i=0;i<n;i++)	   {		   for(j=i+1;j<n;j++)		  {	   if(number[i]>number[j])	   {		  temp=number[i];		  number[i]=number[j];		  number[j]=temp;	   }		  }	   }/*calculating the total */	  for(i=0;i<n;i++)	  {		   total=total+number[i];	  }/* calculating and printing our min max and avgerage */	min=number[0];	max=number[n-1];	avg=(float)total/n;	printf("\n The minimum number is: %d \n",min);	printf("The maximum number is: %d \n",max);	printf("The average is: %f",avg);	getch();}

Ok, lets see some important parts of the code. First of all, I eliminated the char part. Instead your program will first ask you how many numbers you want to enter. It reads the entered value and stores it in n;

The i,j are used as increment values in for loops, temp is temporary variable to use in swapping.

 

 

for(i=0;i<n;i++)	  {			printf("Enter the number: ");			scanf("%d",&number[i]);	  }
This above code asks you to enter numbers. IT read the numbers and put them int array number[] upto n times.

 

The code below prints the entire array you entered.

for(i=0;i<n;i++){	   printf("%d ",number[i]);}

 

The next part is very important part, and you should understand it carefully. This is calling sorting. That is the following part of the code will sort all numbers in the array in minimum(smallest number) to maximum(biggest number) order.

As galexcd told, this is called bubble sorting. The simplest of all.

 

for(i=0;i<n;i++){	  for(j=i+1;j<n;j++)	 {		  if(number[i]>number[j])		 {	  temp=number[i];	  number[i]=number[j];	  number[j]=temp;		 }	 }}

we used nested for loops to achieve sorting. The condition in the if block will check if the first number is greater than second number. If greater it swaps two numbers. Else it continues. And then the first number will then compare to third number so on...This process will continue until the entire array is in min to maximum order (until the end of the loop actually).

 

Next part will calculate the sum of all numbers in the array.

for(i=0;i<n;i++){		total=total+number[i];}
And the last part calculates the average and print minimum, maximum and average.

avg=(float)total/n;
I added float before total because we explicitly converting the integer value into float.

 

I think this program made everything clear to you. If you have any doubts about the program ask it here or IM me...

There are many better versions for this program. But at starting stage this is best for you. I left those range,median things. You can do them yourself.

Edited by xpress (see edit history)

Share this post


Link to post
Share on other sites

Wow....I can't believe I understanded all of that....... YAY for me~!! Thanks galexcd and xpress..... The bubble sorting took a while but I got it..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.