Quantcast

Jump to content


Photo

C++ homework help?


  • Please log in to reply
22 replies to this topic

#1 grafxmaster

grafxmaster
  • 35 posts

Posted 28 September 2011 - 05:43 PM

I'm an absolute beginner to programming (outside of HTML/CSS, but that doesn't count LOL)

We were assigned the following problem:

Write a program with a loop to accept a series of positive integers from the user. Use a sentinel of -99 to terminate the series. After all of the numbers have been entered the program should display the largest and smallest number that was entered and the average of the numbers.


This is what I have so far, what I can't figure out is when the program terminates it displays 0's for the highest and lowest. I need to figure out a way to fix this.

#include<iostream>
using namespace std;

int main ()
{
	int counter = 0;
	int total = 0;
	int number;
	int lowest = 0;
	int largest = 0;
	double average;
	do
	{
		cout << "Enter positive integer (Use -99 to terminate) #" << counter+1 << ": ";
		   if(!(cin >> number))
		   {
				cin.clear();
				cin.ignore(INT_MAX, '\n');
				cout << "\nInvalid integer\n";    	
		   }
		   else if(number != -99 && number < 0)
		   {
			   cout << "\nNot positive\n"; 	
		   }
		   else if(number != -99)
		   {
				total += number;        	//Accumulator
				counter++;        	//Counter
				if(number > largest)
					number = largest;
				else if(number < lowest)
					number = lowest;
		   }        	
	}while(number != -99);
	average = total / counter; 
	cout << endl << "Average of values: " << average << endl 
		     	<< "Largest number: " << largest << endl 
		     	<< "Lowest number: " << lowest << endl;
	system("pause");
}

Any help would be appreciated! Seriously stressing out about this.

#2 Junsu

Junsu
  • 1566 posts

Posted 28 September 2011 - 08:30 PM

typed this long ass message for your but NC gave me an error so you just get this tiny message.


number = largest; and number = lowest;
changes the number value and not the largest/lowest values.
that why largest/lowest remains 0

You also have another logical error but I'll only type it if you can't figure it out

Edited by Rapist, 28 September 2011 - 08:32 PM.


#3 Guest_jcrgirl_*

Guest_jcrgirl_*

Posted 28 September 2011 - 09:22 PM

Your average function wasn't working because even though you specified it was a double, it kept putting out ints because I think somewhere along the line they were converted to ints (?)

Anyway mister rapist was correct. You mixed up when you were assigning values to largest and smallest. Also, 0 is not a positive integer so remember to exclude that as well.

Another problem was that uh, lowest = 0; but you can't input 0 first off, so any number compared to 0 will obviously result in lowest being 0 ...
Can't believe your prof made you do this without an array Posted Image

Edited by jcrgirl, 28 September 2011 - 09:30 PM.


#4 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 28 September 2011 - 10:05 PM

Your problem is you're initializing lowest to be 0 even though it's not a user input number. 0 is the smallest valid integer so it will never actually change. By the looks of the code largest should still display a proper number but not sure what's up with that.

It's an easy program to write out so here it is:

#include <iostream>

using namespace std;

int main()
{
	int min, max, total, input, counter;
	total = input = counter = 0;
	min = 32767;
	max = 0;

	while (ttrue)
	{
		cout << "Enter number: " << endl;
		cin >> input;
		if (input == -99)
			break;
		else if (input < 0)
			cout << "Invalid input!" << endl;
		else
		{
			counter++;
			if (input < min)
				min = input;
			if (input > max)
				max = input;
				
			total += input;
		}				
	}
	
	double avg = total / counter;
	cout << "Max: " << max << "\nMin: " << min << "\nAverage: " << avg << endl;
}

Edit: just read the other two posts. You're definitely new to programming :p keep it up though that's better than what I could've come up with my first time.

Edit 2: realized you don't need that take_input flag, also you should set min to larger than that number I have.Whatever 2^31 is...

#5 Guest_jcrgirl_*

Guest_jcrgirl_*

Posted 28 September 2011 - 10:08 PM

Your problem is you're initializing lowest to be 0 even though it's not a user input number. 0 is the smallest valid integer so it will never actually change. By the looks of the code largest should still display a proper number but not sure what's up with that.

It's an easy program to write out so here it is:

Spoiler


I actually tried this earlier with lowest being the maximum possible double and it gave me more problems XD (I couldn't enter numbers bigger than like 6 or 7 digits and you can easily exceed the min. Also, if you enter just one number it didn't work (with the original code)
I'm pretty sure this if/else fixes it because now you can enter just one value and it works too.



#include<iostream>

using namespace std;




int main ()

{

    double counter = 0;

    double total = 0;

    double number;

    double lowest = 0; 

    double largest = 0;

    double average;

    do

    {

        cout << "Enter positive integer (Use -99 to terminate) #" << counter+1 << ": ";

        

        if(!(cin >> number))

        

        {

            cin.clear();

            cin.ignore(INT_MAX, '\n');

            cout << "\nInvalid integer\n";          

        }

        

        else if(number != -99 && number <= 0) // remember to make this greater than or EQUAL to 0 

        

        {

            cout << "\nNot positive\n";  

        }

        

        else if(number != -99)

        

        {

            total += number;                //Accumulator

            counter++;              //Counter

            

            

            if(number > lowest && number > largest)  // if the number is greater than lowest AND greater than largest, then the number is officially the largest

                largest = number;

            else if (number < largest) // otherwise if the number is less than largest, then it's officially the lowest number

                lowest = number;

        }

        

    }

    while(number != -99);

    average = total / counter; 

    cout << endl << "Average of values: " << average << endl 

    << "Largest number: " << largest << endl 

    << "Lowest number: " << lowest << endl;

    system("pause");

}





Woah, Xcode makes huge gaps when I copy and paste, whatever.

Edited by jcrgirl, 28 September 2011 - 10:17 PM.


#6 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 28 September 2011 - 10:21 PM

I actually tried this earlier with lowest being the maximum possible double and it gave me more problems XD (I couldn't enter numbers bigger than like 6 or 7 digits and you can easily exceed the min. Also, if you enter just one number it didn't work (with the original code)
I'm pretty sure this if/else fixes it because now you can enter just one value and it works too.



#include<iostream>

using namespace std;




int main ()

{

    double counter = 0;

    double total = 0;

    double number;

    double lowest = 0; 

    double largest = 0;

    double average;

    do

    {

        cout << "Enter positive integer (Use -99 to terminate) #" << counter+1 << ": ";

        

        if(!(cin >> number))

        

        {

            cin.clear();

            cin.ignore(INT_MAX, '\n');

            cout << "\nInvalid integer\n";          

        }

        

        else if(number != -99 && number <= 0) // remember to make this greater than or EQUAL to 0 

        

        {

            cout << "\nNot positive\n";  

        }

        

        else if(number != -99)

        

        {

            total += number;                //Accumulator

            counter++;              //Counter

            

            

            if(number > lowest && number > largest)  // if the number is greater than lowest AND greater than largest, then the number is officially the largest

                largest = number;

            else if (number < largest) // otherwise if the number is less than largest, then it's officially the lowest number

                lowest = number;

        }

        

    }

    while(number != -99);

    average = total / counter; 

    cout << endl << "Average of values: " << average << endl 

    << "Largest number: " << largest << endl 

    << "Lowest number: " << lowest << endl;

    system("pause");

}





Woah, Xcode makes huge gaps when I copy and paste, whatever.


You don't need to use doubles do you? The problem states you're taking in positive integers, the max possible signed integer is something like 2.1 billion or something which I supposed is sufficiently large. With doubles the user is allowed to enter fractional numbers and you would need to check for those, which I doubt they would want them to.

Also how come you're comparing number to largest when you're updating lowest?

#7 Guest_jcrgirl_*

Guest_jcrgirl_*

Posted 28 September 2011 - 10:28 PM

You don't need to use doubles do you? The problem states you're taking in positive integers, the max possible signed integer is something like 2.1 billion or something which I supposed is sufficiently large. With doubles the user is allowed to enter fractional numbers and you would need to check for those, which I doubt they would want them to.

Also how come you're comparing number to largest when you're updating lowest?


Oh ignore the doubles.
The averager is the only thing that actually needs to output a double value. I was just testing the code and it was giving me int values when it should have been giving me doubles so I changed them all to doubles lol.

Simply put, if I compare lowest to 0, it will always give me 0 because I initialized lowest as 0. If it ain't the largest, it must be the lowest. (I only need the lowest to take a value the first time around when the code is running so that it no longer takes a 0 value, does that make sense?)

It works without intializing lowest = 1.7E +/- 308 or whatever.


Edit:
Actually ... both give me bugs now -rage-

Edited by jcrgirl, 28 September 2011 - 10:32 PM.


#8 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 28 September 2011 - 10:39 PM

Oh ignore the doubles.
The averager is the only thing that actually needs to output a double value. I was just testing the code and it was giving me int values when it should have been giving me doubles so I changed them all to doubles lol.

Simply put, if I compare lowest to 0, it will always give me 0 because I initialized lowest as 0. If it ain't the largest, it must be the lowest. (I only need the lowest to take a value the first time around when the code is running so that it no longer takes a 0 value, does that make sense?)

It works without intializing lowest = 1.7E +/- 308 or whatever.


Edit:
Actually ... both give me bugs now -rage-


Oh you're right! I didn't bother doing that either but I think you use something like setprecision inside the cout parts to make it output fractional numbers. I hardly ever have to work with floating point numbers so I can't say for sure.

So are you a computing science student? I remember helping you out once with something but you weren't exactly "enthusiastic" with programming as you are now.

#9 grafxmaster

grafxmaster
  • 35 posts

Posted 28 September 2011 - 10:40 PM

Wow, thank you all so much for your responses, They helped so much!, I just finished rewriting it, it seems to be working now with your suggestions. :D :D :D

You saved my grade on this thing!
+rep +rep +rep :p

I can't wait to get into to more advanced programming :thumbsup:

#10 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 28 September 2011 - 10:43 PM

Wow, thank you all so much for your responses, They helped so much!, I just finished rewriting it, it seems to be working now with your suggestions. :D :D :D

You saved my grade on this thing!
+rep +rep +rep :p

I can't wait to get into to more advanced programming :thumbsup:


Are you using visual studio?

#11 Guest_jcrgirl_*

Guest_jcrgirl_*

Posted 28 September 2011 - 10:45 PM

Oh you're right! I didn't bother doing that either but I think you use something like setprecision inside the cout parts to make it output fractional numbers. I hardly ever have to work with floating point numbers so I can't say for sure.

So are you a computing science student? I remember helping you out once with something but you weren't exactly "enthusiastic" with programming as you are now.


Yeees I'm still doing comp sci.... apparently not very good at it but I guess practicing doing other peoples hw will add up eventually Posted Image
I remember you helping meeee <3

#12 grafxmaster

grafxmaster
  • 35 posts

Posted 28 September 2011 - 10:46 PM

Are you using visual studio?


Microsoft Visual C++ Express 2010, although it confuses me (because I'm a noob) LOL :p

#13 Guest_jcrgirl_*

Guest_jcrgirl_*

Posted 28 September 2011 - 10:57 PM

Microsoft Visual C++ Express 2010, although it confuses me (because I'm a noob) LOL :p


Better than notepad + terminal
the shit they made me do was more ghetto than leftover KFC popcorn chicken for breakfast with purple kool aid

#14 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 28 September 2011 - 11:02 PM

Yeees I'm still doing comp sci.... apparently not very good at it but I guess practicing doing other peoples hw will add up eventually Posted Image
I remember you helping meeee <3

Lol, it makes me wish my homework was this easy =P What year are you in? I'm doing my minor in that, majoring in cmpt engineering.

Microsoft Visual C++ Express 2010, although it confuses me (because I'm a noob) LOL :p


Yeh I still haven't heard of a intro programming class that uses a different compiler for their course with C++. It's bloated with a whole mess of features students don't use and don't know what to do with =P

Better than notepad + terminal
the shit they made me do was more ghetto than leftover KFC popcorn chicken for breakfast with purple kool aid


Noiti and Joe rave about how great notpad is. It's just plain barbaric :p But I remember you were using gcc as your compiler, were you using it on windows?

#15 Guest_jcrgirl_*

Guest_jcrgirl_*

Posted 28 September 2011 - 11:08 PM

Lol, it makes me wish my homework was this easy =P What year are you in? I'm doing my minor in that, majoring in cmpt engineering.

Noiti and Joe rave about how great notpad is. It's just plain barbaric :p But I remember you were using gcc as your compiler, were you using it on windows?


4th year but I was futzing around with art on the side so I have a few years left :(


Notepad is nice for html/css but if you miss just one semicolon you're fucked and you have to recompile and you can't find it ugh... the gcc was on my windows partition. I don't know how long I'm going to be stuck with more programming classes since It's not what I aim for. (I'm probably going to do more statistics related programming... lulz ez-pz ... to manipulate data for banks and shit. I think my internship next year involves SAS)

ANYWAY computers are dumb and what's worse than C++ is that I have to do assembly intro too this semester with my data structs
all I see all day long is 00000101010100101001010101011010111101 written across the blackboard Posted Image

#16 grafxmaster

grafxmaster
  • 35 posts

Posted 28 September 2011 - 11:20 PM

Yeh I still haven't heard of a intro programming class that uses a different compiler for their course with C++. It's bloated with a whole mess of features students don't use and don't know what to do with =P


Definitely better than notepad LOL
Yeah, I just want a simple console program compile, without all of the options and ways to mess up LOL.
Do you have an suggestions for a compiler like that? Just wondering. :D

#17 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 28 September 2011 - 11:46 PM

4th year but I was futzing around with art on the side so I have a few years left :(


Notepad is nice for html/css but if you miss just one semicolon you're fucked and you have to recompile and you can't find it ugh... the gcc was on my windows partition. I don't know how long I'm going to be stuck with more programming classes since It's not what I aim for. (I'm probably going to do more statistics related programming... lulz ez-pz ... to manipulate data for banks and shit. I think my internship next year involves SAS)

ANYWAY computers are dumb and what's worse than C++ is that I have to do assembly intro too this semester with my data structs
all I see all day long is 00000101010100101001010101011010111101 written across the blackboard Posted Image

Uggh...hate having to write assembly. But as you can guess since I'm in engineering I've been dealing with binary since my 2nd semester. It's pretty cool though, with all the hardware/architecture related courses you get to basically learn how a computer functions from the bottom to the top. We get to make our own processors next semester :D

Definitely better than notepad LOL
Yeah, I just want a simple console program compile, without all of the options and ways to mess up LOL.
Do you have an suggestions for a compiler like that? Just wondering. :D


g++, it comes with the GNU compiler collection(GCC). It's widely used amongst many companies and developers. It's all command line though, you have to write your code in some sort of text editor(I'd go with notepad++) then call g++ with that source file.

#18 Junsu

Junsu
  • 1566 posts

Posted 29 September 2011 - 02:54 PM

I use Geany and DevC++
Both are amazing and simple to use, esp Geany.

#19 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 29 September 2011 - 04:46 PM

I use Geany and DevC++
Both are amazing and simple to use, esp Geany.


It's good but it's an IDE, not a text editor which is more like what he should be using.

#20 Junsu

Junsu
  • 1566 posts

Posted 29 September 2011 - 05:24 PM

It's good but it's an IDE, not a text editor which is more like what he should be using.


But isnt a IDE easier to use as its basically everything put together :((

#21 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 29 September 2011 - 05:31 PM

But isnt a IDE easier to use as its basically everything put together :((


Think of it this way, what's easier to open Visual Studio or Notepad?

#22 Junsu

Junsu
  • 1566 posts

Posted 29 September 2011 - 05:48 PM

Think of it this way, what's easier to open Visual Studio or Notepad?


Geany :D

#23 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 29 September 2011 - 05:54 PM

Geany :D


Different folks, different strokes.


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users