Thread: HELP! Not sure how to?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    83

    HELP! Not sure how to?

    I have to make an if statement with int that are num1, num2, num3, num4, largest, smallest

    This is easy but for some reason I can't figure it out by using an if statement. I can't remember how to do this? Could someone help me out? I am trying to make it look like this:

    Enter number 1: __
    Enter number 2: __
    Enter number 3: __
    Enter number 4: __

    The largest value entered was __
    The smallest value entered as __

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    I have this because I am supposed to set num1 to largest and smallest. How would I do it from here:

    Code:
    #include <iostream>
    
    int main() {
      int num1, num2, num3, num4, largest, smallest;
    
     num1 = largest;
      num1 = smallest;
    and remember I have to do an if statement and I can't remember how to do it with an if statement.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    This is my new code but I don't get it. It is giving me the largest and smallest as the same thing.

    Code:
    // CSE 1020  Lab 5  Keith Deitz  diddies708@yahoo.com  
    
    #include <iostream>
    
    int main() {
      double num1, num2, num3, num4, largest, smallest;
      
      cout << "CSE 1020  Lab 5  Keith Deitz  diddies708@yahoo.com" << endl 
           << endl;
      
      cout << "Enter number 1: ";
       cin >> num1;
      cout << "Enter number 2: ";
       cin >> num2;
      cout << "Enter number 3: ";
       cin >> num3;
      cout << "Enter number 1: ";
       cin >> num4;
      
      largest = num1;
      smallest = num2;
      
      if (num2 > largest) 
      
      largest = num2;
      
      else if (num2 < smallest)
      
      smallest = num2;
      
      else if (num3 > largest) 
      
      largest = num3;
      
      else if (num3 < smallest)
      
      smallest = num3;
      
      else if (num4 > largest) 
      
      largest = num4;
      
      else if (num4 < smallest)
      
      smallest = num4;
      
      
      
      cout << endl << "The largest value entered was "
           << largest << endl;
           
      cout << endl << "The smallest value entered was "
           << smallest << endl;
      
      return 0;
      }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    1.
    Code:
    cout << "Enter number 1: ";
       cin >> num4;
    should be "Enter number 4"

    2. smallest = num2;

    should be smallest = num1;

    3.

    Code:
    if (num2 > largest) 
      
      largest = num2;
      
      else if (num2 < smallest)
      
      smallest = num2;
      
      else if (num3 > largest) 
      
      largest = num3;
      
      else if (num3 < smallest)
      
      smallest = num3;
      
      else if (num4 > largest) 
      
      largest = num4;
      
      else if (num4 < smallest)
      
      smallest = num4;
    Red parts should be removed - numbres should be checked independently one from another
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed