Thread: Largest number program

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    25

    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:perator>>(std::basic_istream<char, _Traits>&, unsigned char&) [with _Traits = std::char_traits<char>]

    Can anyone please advise to where I have gone wrong?

    Thanks
    Last edited by rebel; 11-29-2005 at 06:54 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Last edited by SlyMaelstrom; 11-29-2005 at 08:16 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    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

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Sent from my iPadŽ

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Sent from my iPadŽ

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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();
    }
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    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

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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.
    Sent from my iPadŽ

  9. #9
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    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:**********

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Thumbs up

    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.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Cheers Guys,

    I'm there now - thanks for your advice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  2. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  3. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. problem with my prime number program
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-12-2002, 12:30 PM