Jump to content
xisto Community
Tran-Gate

New Issues On C++ Simple Program Issues

Recommended Posts

Hi again~!! I have just modified my other version of my mean, median, mode, etc. program. I am receiving no compiling errors but errors when I am running the program.

#include <iostream>using namespace std;int main() {	double num[99], avg, min_val, max_val, range, median, temp;	int i, j, n, count = 0;	char s;		cout << "How many numbers would you like to enter? (max 99): ";	cin >> n;		for ( i = 0; i < n; i++ ) {		cout << "Enter a number: ";		cin >> num[i];	}		cout << "\n\n";	for ( i = 0; i < n; i++ ) {		for ( j += i; j < n; j++ ) {			if ( num[i] > num[j] ) {				 temp = num[i];				 num[i] = num[j];				 num [j] = temp;			}		}	}		for ( i = 0; i < n; i++ ) {		avg += num[i];	}		cout << "Would you like to display the numbers you inputted in order? (y/n) : ";		cin >> s;	if ( s == 'y' ) {	   for ( i = 0; i < n; i++ ) {			   if ( count = 10 ) {					cout << "\n";					count = 0;			   }	   cout << num[i];	   }	}		cout << "\n\n";		median = avg;	median /= 2;	avg /= n;	min_val = 999999999;	max_val = -999999999;	for ( i = 0; i < n; i++ ) {		if ( num[i] < min_val ) min_val = num[i];		if ( num[i] > max_val ) max_val = num[i];	}	range = max_val - min_val;		cout << "Minimum: " << min_val << "\n";	cout << "Maximum: " << max_val << "\n";	cout << "Range: " << range << "\n";	cout << "Average: " << avg << "\n";	cout << "Median: " << median << "\n";	cout << "Mode: COMING SOON\n";		system("PAUSE");	return 0;	}

First of all, when I input 'y' into the "Would you like to display the numbers you inputted in order? (y/n) :", it doesn't put them in order.... I thought I had used the bubble sorting method to make it "sorted". I am very confused on what is happening. I have looked over the program multiple times but I can't seem to find the error.

Another question I have is that when my program calculates the average and median, it outputs, "1.#QNAN". Does anyone know why and how would I fix that?

I am also a little confused with the count part of the program. If you looked into the program, you will see what I mean.

Thanks again (I had a different topic) in advance~!! I really appreciate it~!!
Edited by Tran-Gate (see edit history)

Share this post


Link to post
Share on other sites

Hey Tran, You did some common mistakes again. I think you must get compilation errors with your program. I will show you some mistakes in your program. But before that I will give you some suggestions.

Don't use short hands notations( like j+=i etc..) frequently. Since you started learning you'll get confusion easily. They are not necessary at all. Since you are getting some problems unknown to you, initialize all variables when defining them. For example int i=0,j=0 etc......This will avoid some possible errors you are getting. (These are called logical errors)

 

Ok, lets see your mistakes...

 

Here is your first mistake in sorting.

In the second for loop, you used j+=i, it means j=j+i but you should use j=i+1. You have two mistakes here. First one is your sorting is wrong because j=j+i is not correct. The second and important one is, since you haven't initialized j (means you didn't set j to 0 or something), j may have some random value. Then j=j+i will give undesirable results(because, you're using the value of j before it gets some value you want). So, overall this sorting will give wrong result.

 

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

Next, you want this avg to find out average, but you are using this to calculate total. It will lead to some confusion.

 

for ( i = 0; i < n; i++ ) {	avg += num[i];}

 

There is no use with the following code, and also it has some mistakes. In the inner if block( if count=10), you used an assignment operator(=). If you want to compare you must use ==. Single "=" is an error there. Even though if you want to print the array in sorted order, there is no need to use if(count==10) code. And also, you had never used count in the program. So, does it make sense to use it here? :lol:

cout << "Would you like to display the numbers you inputted in order? (y/n) : ";			  cin >> s;	if ( s == 'y' ) 			{	   for ( i = 0; i < n; i++ ) 				{			   if ( count = 10 ) 					   {			cout << "\n";			count = 0;				}	   cout << num[i];	   }			 }

Use a total variable to avoid confusion. Then median=total/2 and avg=total/n. There is no point in assigning avg to median, isn't it?

median = avg;median /= 2;avg /= n;

I have no idea about this code...No need to use....and the signs you used are also confusing(you used positive for minimum and negative for maximum)

min_val = 999999999;max_val = -999999999;

Unnecessary and probably wrong code again. After sorting, As per our program num[0] will contain the minimum value and

num[n] will contain the maximum value. Is there any need to check the minimum and maximum values again?

So unnecessary for loop also.

for ( i = 0; i < n; i++ ) {	if ( num[i] < min_val ) min_val = num[i];	if ( num[i] > max_val ) max_val = num[i];}

Rest of your code is ok (is there anything I left? ^_^ ).....Change your program as I suggested. If you get any other problems post them here.....

Edited by xpress (see edit history)

Share this post


Link to post
Share on other sites

I got all of this, but I got my median wrong...... All of thins works besides my median......

#include <iostream>using namespace std;int main() {	double num[99], avg, min_val, max_val, range, median, temp;	int i, j, n, count = 0;	char s;		cout << "How many numbers would you like to enter? (max 99): ";	cin >> n;		for ( i = 0; i < n; i++ ) {		cout << "Enter a number: ";		cin >> num[i];	}		cout << "\n\n";	for ( i = 0; i < n; i++ ) {		for ( j = i + 1; j < n; j++ ) {			if ( num[i] > num[j] ) {				 temp = num[i];				 num[i] = num[j];				 num [j] = temp;			}		}	}		avg = 0;		for ( i = 0; i < n; i++ ) {		avg += num[i];	}		cout << "Would you like to display the numbers you inputted in order? (y/n) : ";	cin >> s;		if ( s == 'y' ) {	   for ( i = 0; i < n; i++ ) {		   if ( count == 3 ) {					cout << "\n";					count = 0;			   }		   count++;	   cout << num[i] << "   ";	   }	   cout << "\nRead from left to right; top to bottom..." << "\n";	}		cout << "\n\n";		median = avg;	median /= 2;	avg /= n;	min_val = 999999999;	max_val = -999999999;	for ( i = 0; i < n; i++ ) {		if ( num[i] < min_val ) min_val = num[i];		if ( num[i] > max_val ) max_val = num[i];	}	range = max_val - min_val;		cout << "Minimum: " << min_val << "\n";	cout << "Maximum: " << max_val << "\n";	cout << "Range: " << range << "\n";	cout << "Average: " << avg << "\n";	cout << "Median: " << median << "	<----   *Note that this is probably wrong*" "\n";	cout << "Mode: COMING SOON\n";		system("PAUSE");	return 0;	}

Share this post


Link to post
Share on other sites

OH and, xpress, I don't see how the average is off...... I just forgot to make avg = 0;

min_val = 999999999;max_val = -999999999;

I don't see how that doesn't work. If I declare them 0 then the min_val, max_val, range won't work.....
The user just cant input a number greater than 999999999 or lower than -999999999.
Anyways, this is for personal use so I doubt I will be needing that high numbers......

Thanks again xpress~!!

Share this post


Link to post
Share on other sites

min_val = 999999999;max_val = -999999999;
I don't see how that doesn't work. If I declare them 0 then the min_val, max_val, range won't work.....

The user just cant input a number greater than 999999999 or lower than -999999999.

Anyways, this is for personal use so I doubt I will be needing that high numbers......


Maybe I am missing something here but was just wondering why do you need to initialize min_val and max_val to those numbers? wouldn't it be enough to initialize them to the first element in the array num?

 

min_val = num[0];	max_val = num[0];	for ( i = 0; i < n; i++ ) {		if ( num[i] < min_val ) min_val = num[i];		if ( num[i] > max_val ) max_val = num[i];	}	range = max_val - min_val;

Share this post


Link to post
Share on other sites

Here are my 2 cents...

#include <iostream>using namespace std;int main() {	double num[99], avg, range, median, mode;	int i, j, n;	char s;		cout << "How many numbers would you like to enter? (max 99): ";	cin >> n;		for ( i = 0; i < n; i++ ) {		cout << "Enter a number: ";		cin >> num[i];	}	cout << "\n\n";	/*** Here i added several lines to calc the mean**/	double prevNumber = num[0] - 1;	int timesUsed, mostUsed = -1;	for ( i = 0; i < n; i++ ) {		if (prevNumber == num[i]) {			timesUsed++;		} else {			prevNumber = num[i];			timesUsed = 1;		}		if (timesUsed > mostUsed) {			mostUsed = timesUsed;			mode = num[i];		}//end of meany stuff		for ( j = i + 1; j < n; j++ ) {			if ( num[i] > num[j] ) {				swap(num[i], num[j]);			//easier, ain't it?;-)				//why not use STL sort/qsort?			}		}	}		avg = 0;	for ( i = 0; i < n; i++ )		avg += num[i];	avg /= n;		cout << "Would you like to display the numbers you inputted in order? (y/n) : ";	cin >> s;		if ( s == 'y' || s == 'Y' ) {		//C languages are case-sensitive...	   for ( i = 0; i < n; i++ ) {		   if ( i % 3 == 0) {			//means, "every third character..."					cout << "\n";			}	   cout << num[i] << "\t\t";		//equally spaced-out numbers	   }	   cout << "\nRead from left to right; top to bottom..." << "\n";	}	cout << "\n\n";/*** Range -- easier, min/max -- removed unnecessary code (see the outputs)**/	range = num[n-1] - num[0];/*** Median -- take the middle score or the avg of 2 middle scores**/	if (n % 2) {		median = num[n/2];	} else {		median = (num[n/2] + num[n/2-1]) / 2;	}		cout << "Minimum: " << num[0] << "\n";		//look at this	cout << "Maximum: " << num[n-1] << "\n";	//and this	cout << "Range: " << range << "\n";	cout << "Average: " << avg << "\n";	cout << "Median: " << median << "\n";	cout << "Mode: " << mode << "\n";		system("PAUSE");	return 0;	}

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

×
×
  • 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.