finding highest number entered

This is a discussion on finding highest number entered within the C++ Programming forums, part of the General Programming Boards category; Isn't there an easier way to write this program? Code: #include <iostream> using namespace std; int main() { int num1; ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Question finding highest number entered

    Isn't there an easier way to write this program?

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int num1;
    	int num2;
    	int num3;
    	int num4;
    
    	cout << "Enter four numbers" << endl;
    	cin >> num1;
    	cin >> num2;
    	cin >> num3;
    	cin >> num4;
    
    	if (num1 > num2 && num1 > num3 && num1 > num4)
    		cout << "The highest number entered was the first one" << endl;
    
    	else if (num2 > num1 && num2 > num3 && num2 > num4)
    		cout << "The highest number entered was the second one" << endl;
    
    	else if (num3 > num1 && num3 > num2 && num3 > num4)
    		cout << "The highest number entered was the third one" << endl;
    
    	else
    		cout << "The highest number entered was the fourth one" << endl;
    
    	return 0;
    
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Here's a thought:
    Code:
        char *ordinal[] = {"first","second","third","fourth"};
    
        int num[4];
        cin >> num[0] >> num[1] >> num[2] >> num[3];
    
        int maxi = findmax(num,4);
    
        cout << "The highest number entered was the " << ordinal[maxi] << " one" << endl;
    You just have to implement findmax().

    gg

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by Codeplug
    Here's a thought:
    Code:
        char *ordinal[] = {"first","second","third","fourth"};
    
        int num[4];
        cin >> num[0] >> num[1] >> num[2] >> num[3];
    
        int maxi = findmax(num,4);
    
        cout << "The highest number entered was the " << ordinal[maxi] << " one" << endl;
    You just have to implement findmax().

    gg
    Can you explain findmax and *ordinal[] for me please?

    I'm a little fimiliar with arrays, but pointers...

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    You will have to learn pointers, but for now, maybe this will be easier to understand (have you worked with c++ strings yet?):

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        int i,max,maxpos,num[4];
        string ordinal[4] = {"first", "second", "third", "fourth"};
    
    
        for(i=0;i<4;i++)
        {
            cout << "Enter a number > ";
            cin >> num[i];
        }
        max=num[0];
        maxpos=0;
        for(i=1;i<4;i++)
            if(num[i]>max)
            {   max=num[i];
                maxpos=i;
            }
    
        cout << "The highest number entered was the " << ordinal[maxpos] << " one." << endl;
    
        return 0;
    }

  5. #5
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Can anyone explain findmax and *ordinal[] for me?

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    I guess you haven't learned much about functions yet either...

    int findmax(num,4) would be a function (written by you) that would receive two parameters: "num", an array of "somethings", presumably integers, and "n", or in this case "4", which is how many elements are in the array. This function would return an integer giving the array index of the highest num.

    char *ordinal[] is an array of pointers to c-style character strings, and Codeplug's example was initializing the array with pointers to the four string literals "first","second","third","fourth".

    Like I said, you do have to learn pointers if you want to program. Functions too.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Greetings.

    Here's a sample template for finding MAX:

    Code:
    
    
    
    // calculate the zero-based offset of 'index'
    
    template <typename type>
    unsigned mem_pos(type * first, type * index) {
     return (unsigned)((unsigned)index -(unsigned)first)/ sizeof(type);
    }
    
    
    // return a pointer to the MAX element
    
    template <typename type>
    type *
     max(type data[], type * end) {
    type * index, max = *data;
       for(index = data; data != end; ++data)
         if(*data > max) 
           index = data;
     return index;
    }
    
    
    template <typename type>
    type
     max_value(type data[], type * end) {
     return * max<type>(data, end);
    }
    
    
    // return the actual zero-based index of the MAX element
    
    template <typename type>
    unsigned
     max_index(type data[], type * end) {
     return mem_pos<type>(data, max<type>(data, end));
    }
    
    
    
    
    int main()
    {
     double mem[100], * input = &mem[-1];
    
     puts("Enter up to 100 doubles, '0' to exit.");
    
     do{
        cin >> *(++input);
     }while(*input != 0 && input != mem+100);
    
     unsigned index = max_index(mem, input);
    
     cout << endl << mem_pos(mem, input)
     << " numbers entered." << endl
     << "Maximum :  " << mem[index] << endl
     << "Position : " << index << endl;   
    
     return getch();
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Here's another one:
    Code:
    void find_max_confusion_for_newbie_by_using_templates(){
       ...
    }
    gg

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Templates are merely advanced substitution macros that are very easy to use.

    The central point was making it clear that the address of the maximum value was the most important data to collect because from it we can :
    1) have the address readily available for assignment.
    2) retrieve the maximum value.
    3) calculate the index of max.

    My second point was that it is actually desirable to template algorithms, since we can build catalogues of code for later use.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    In all seriousness, Volk, if you learn and understand Sebastiani's post, you will have indeed learned a lot.

    I suggest you kick off your learning by understanding pointers and functions first.

    gg

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Last time I replied to this question I got bashed for giving away the answer. It was the exact same question....

    :-D!

  12. #12
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    Posts
    28

    The idea...

    ... is to store the input digits in an array. U can do it without pointers and such. But if you don't know the number of the inputs, then create a dynamic array. See the tutorial N14 I think on this page to create such array.
    But the simpliest method is using static array -
    Code:
    int arr[500];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Finding the 3 largest numbers of the five entered
    By Bkgrant in forum C Programming
    Replies: 11
    Last Post: 02-13-2009, 12:08 PM
  3. Replies: 2
    Last Post: 02-08-2009, 08:26 PM
  4. Finding largest number in array
    By el_chupacabra in forum C Programming
    Replies: 2
    Last Post: 01-22-2009, 01:31 PM
  5. finding number of cores in a multi-core processor
    By doubleanti in forum Tech Board
    Replies: 3
    Last Post: 03-05-2008, 02:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21