calculating maximum and minimum values for a basic C++ program

This is a discussion on calculating maximum and minimum values for a basic C++ program within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble creating a program that has 4 input values and from that calculating the minimum and maximum values, ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    Unhappy calculating maximum and minimum values for a basic C++ program

    I'm having trouble creating a program that has 4 input values and from that calculating the minimum and maximum values, for example if you had 1, 4, 19, 5, the maximum value for this data set would be 19 and the minimum value would be 1, I'm getting stuck on the if statement, how would I construct a better one including all variables?

    This is what I've got so far:

    float min, max(float a, b, c, d)
    void main ( ) {
    cout<<"insert 4 #'s \n";
    cin>>a>>b>>c>>d;
    if max(a,b,c,d == 1);
    cout<<d, "_"<<c, "_" <<b, "_"<<a;
    else if max(a, b, c, d) == 2)
    cout<<"maximum value";
    cout << " \n" ;
    cout<<"insert 4#'s \n";
    cin>>a>>b>>c>>d;
    if min(a, b, c, d ==3)
    cout<<a, "_"<<b, "_" <<c, "_"<<d;
    else if min(a, b, c, d) == 4)
    cout<<d, "_"<<c, "_" <<b, "_"<<a;
    else
    cout<<"minimum value";
    }

    what should I do?

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    This is not the complete way to do it. It does ask for input and it sorts 2 groups of 2. But its a start to making it easier. Ill try and change it because now it only sorts it in two groups of two. Try it out though

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    
    {
        
        void order(int*, int*);//prototype
        int n1,n2,n3,n4;
       cout <<"Enter first number"<<endl;
       cin >>n1;
       cout <<"Enter second number"<<endl;
       cin >>n2;
       cout <<"Enter third number"<<endl;
       cin >>n3;
       cout <<"Enter fourth number"<<endl;
       cin >>n4;
        
        cout<< "Before sort"<<endl;
        cout<< "a ="<<n1<<endl;//before sort
        cout<< "b ="<<n2<<endl;
        cout<< "c ="<<n3<<endl;
        cout<< "d ="<<n4<<endl;
        
        order(&n1,&n2); //function call
        order(&n3,&n4);
        
        cout <<"After Sort"<<endl;
        cout<< "a ="<<n1<<endl;//after sort
        cout<< "b ="<<n2<<endl;
        cout<< "c ="<<n3<<endl;
        cout<< "d ="<<n4<<endl;
        
        getch();
        return 0;
    }
    
    void order(int* numb1, int* numb2) // swawp numbers
        {
            if(*numb1 > *numb2)
                   {
                   int temp = *numb1;
                   *numb1 = *numb2;
                   *numb2 = temp;
                   }
        }

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Code:
    void max(int *arr, int size)
    {
       int i;
       int min, max;
    
       min = arr[0];
       max = arr[0];
       for(i = 1; i < size; i++)
       {
          if(arr[i] < min)
             min = arr[i];
          if(arr[i] > max)
             max = arr[i];
       }
       cout << "max = " << max << endl << "min = " << min << endl;
    }
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  3. maximum and minimum
    By aslak in forum C Programming
    Replies: 35
    Last Post: 12-14-2008, 03:54 PM
  4. Displaying Minimum and Maximum values from input
    By mgardnertech in forum C Programming
    Replies: 1
    Last Post: 06-29-2008, 09:47 PM
  5. Maximum and Minimum Values
    By swaugh in forum C Programming
    Replies: 7
    Last Post: 12-16-2006, 09:43 PM

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