# Largest number program

• 11-29-2005
rebel
Largest number program
Hi Guys,

I'm trying to build a program to get the largest number of 20 numbers put in a program and the largest number is outputted on the screen:

Here's my code;

values3.h
Code:

```// Determining the largest number #include <iostream> using namespace std; #include <string> class figures   {   public:     int get_largest_number(int a[]);     int display_largest_number();       protected:     int largest_number;     int i;   }; int figures::get_largest_number(int a[]) {     cout << "Enter Number: ";     cin >> a;     largest_number = a[0];         for(i=1; i<20; i++)     {       if(a[i] > largest_number)       {             largest_number = a[i];       }     }     return(largest_number); }```
Main program to get it to run - values.cpp

Code:

```// ASSIGN77.CPP // A Program to display the largest number #include "values_new.h" #include <cstdlib> int main () {     figures numbs;     numbs.get_largest_number(int a[]);     system ("pause"); }```
So in theory the user enters 20 numbers which are stored in an array and then when the 20th number is finally entered it outputs the largest number entered on the screen.

When I try and run it though it comes up with an errors saying:

values3.h no match for 'operator>>' in 'std::cin >> a'

std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&) [with _Traits = std::char_traits<char>]

Thanks
• 11-29-2005
SlyMaelstrom
You have a bunch of problems here. Starting with your library, you can't say " cin >> a " and expect it to do cin 20 times. It doesn't work like that. When you want to input to an array you need an index. Secondly, there is no reason to do a priming read on your loop, so you can knock out the first input and just do all the inputs inside of the loop.

Then in your main program, you attempt to make a function call using the name of the datatype and a variable that doesn't exist in the scope of main(). Also you didn't return a value in main. Atleast you defined it as an integer, though. :)

And lastly, never make a call to the system. It's a big security issue. Use cin.get() to pause the program.

Here is some corrected code for you. With a little perk or two for nicer output.

Code:

```// Determining the largest number #include <iostream> using namespace std; #include <string> class figures   {   public:     int get_largest_number(int []);     int display_largest_number();       protected:     int largest_number;     int i;   }; int figures::get_largest_number(int a[]) {     for(i=0; i<20; i++)     {       cout << "Enter Number " << i + 1 << ") ";       cin >> a[i];       cin.ignore();       if(a[i] > largest_number)       {             largest_number = a[i];       }     }     return largest_number; }```
Code:

```// ASSIGN77.CPP // A Program to display the largest number #include "values_new.h" int main () {     int a[20];     figures numbs;     cout << "The largest number is " << numbs.get_largest_number(a) << endl;     cin.get();     return 0; }```
Please try to understand your mistakes before continuing on with your studies, if you go too far ahead, you'll find yourself lost in the basics forever.
• 11-29-2005
rebel
Cheers SlyMaelstrom,

Array's seems to go over my head totally. I'm pretty good and understand most of the basics but arrays just goes over my head and struggle to take it in.

Anyone know of a really good and basic website they can point me in?

Thanks
• 11-29-2005
SlyMaelstrom
Arrays are one of those things that can only be learned properly through trial and error. Use them in your programs and learn from your mistakes. It was just the fact that I saw you using classes that made me add that comment. You should make a good solid attempt at learning arrays fully before getting into more advanced structures like classes.

It's a pretty good program, though, and it's clear that you have a grasp of what you're doing. You just have to lose the temptation of going ahead of yourself, because you may assume things that are wrong. Unlearning things bad technique and syntax can be much, much harder than learning properly.
• 11-29-2005
SlyMaelstrom
Another thing I'd like to mention, it's usually a bad habit to put input and output in libraries, that's the kind of thing you want to see right in front of you. If down the road you decided to include your library in a different program, you might forget that's in there and wonder where the hell the output is coming from when you run the program.

I won't edit this one for you, but consider moving the array input into main and passing the filled array to the function to find the largest number.
• 11-29-2005
Prelude
>Starting with your library, you can't say " cin >> a " and expect it to do cin 20 times. It doesn't work like that.
But it can if you really want it to. It stomps all over the implementation, but you can still do it by creating your own operator>> that takes a reference to an array of 20 elements. I wouldn't recommend it though, since there are semantic issues as well as portability issues. ;)

>Also you didn't return a value in main.
You don't have to. If there's no explicit return from main, 0 is assumed by the implementation. With modern compilers, it's purely stylistic.

>And lastly, never make a call to the system.
Never say never. :) But for pausing the program, I always recommend this (wrapped in a pretty little function):
Code:

```#include <limits> #include <ios> void pause_program() {   std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );   std::cin.get(); }```
• 11-30-2005
rebel
Hi Guys,

Last question on this if I may?

Tried to take your points on board and looked around and now have my program working that works out the lowest number, highest number, mean and sum. Here's the code:

Code:

```// Determining the largest number #include <iostream> using namespace std; #include <string> class figures   {   public:     int get_largest_number(int a[]);     int get_smallest_number(int a[]);     int get_sum_number(int a[]);     int get_mean_number(int a[]);     int get_histogram(int a[]);       protected:     int largest_number;     int smallest_number;     int i;     int mean;     int sum;   }; int figures::get_largest_number(int a[]) {     for(i=0; i<20; i++)     {       cout << "Enter Number: " << i + 1 << ") ";       cin >> a[i];       cin.ignore();       if(a[i] > largest_number)       {             largest_number = a[i];       }     }     return largest_number; } int figures::get_smallest_number(int a[]) {         for(i=0; i<20; i++)     {       if(a[i] < smallest_number)       {             smallest_number = a[i];       }     }     return smallest_number; } int figures::get_sum_number(int a[]) {   int sum = 0;   for ( int i = 0; i < 20; i++ )   {       sum += a[i];   }   return sum; } int figures::get_mean_number(int a[]) {   return get_sum_number(a) / 20; }```
CPP File

Code:

```// ASSIGN77.CPP // A Program to display the largest number #include "values3.h" #include <cstdlib> int main () {     int a[20];     figures numbs;     cout << "The largest number is " << numbs.get_largest_number(a) << endl;     cout << "The smallest number is " << numbs.get_smallest_number(a) << endl;     cout << "The sum of the numbers is " << numbs.get_sum_number(a) << endl;     cout << "The mean of the numbers is " << numbs.get_mean_number(a) << endl;     cin.get();     return 0;         system ("pause"); }```
Now i'm really happy I have it working, but now I want to create a Histogram if possible, but the only reference I can find is Here which confused me big time!

Now I want it to output like:

Range 0-9: ****
Range 10-19: **

Can anyone point me in the right direction and if this is simply done as what I have looked at so far is very confusing!

Thanks

Chris
• 11-30-2005
SlyMaelstrom
This is starting to look a whole lot like homework.

Horizontal histograms are easy, just get the amount in each range before hand and print out those amounts one range at a time. I'm not gonna tell you more than that, because... well this really looks like homework. If it is homework, then I'd tell you, most teachers really like it if you can make the histogram look like this:

Code:

```Histogram 10|  9|            ***  8|      ***  ***  7|      ***  ***  6|      ***  ***  5|      ***  ***  4|      ***  ***  3|  ***  ***  ***  ***  2|  ***  ***  ***  ***  ***  1|  ***  ***  ***  ***  ***   --------------------------       A    B    C    D    F```
It's a bit more difficult, but worth a try.
• 11-30-2005
treenef
If I were you I'd try the horizontal histogram first. It's easier.
Here's an example of how you might format your output:-

Code:

```/*  * How to format your histogram using iomanip  * Lifted from the FAQ  */ #include <iostream> #include <iomanip> using namespace std; void Print_Stars(void); int main() {   cout<< setiosflags ( ios_base::right )       << setw ( 15 ) << "Range 0-9:";       Print_Stars();       cout<<endl;         cout<< setiosflags ( ios_base::right )       << setw ( 15 ) << "Range 10-19:";       Print_Stars();       cout<<endl;         cout<< setiosflags ( ios_base::right )       << setw ( 15 ) << "Range 20-29:";       Print_Stars();       cout<<endl;           cin.get();           return 0; } void Print_Stars(void) {     for(int i=0; i<10; i++)     {         cout<<"*";     } }```

My output:
Code:

```    Range 0-9:**********   Range 10-19:**********   Range 20-29:**********```
• 12-01-2005
puja399
Quote:

Originally Posted by Prelude
>... I always recommend this (wrapped in a pretty little function):
Code:

```#include <limits> #include <ios> void pause_program() {   std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );   std::cin.get(); }```

That's neat. Thanks for the tip. :) :) :)
• 12-01-2005
rebel
Cheers Guys,