Alternate Way

This is a discussion on Alternate Way within the C++ Programming forums, part of the General Programming Boards category; Ok this is kind of a bizzarre question but it's really bugging me. The book I'm reading has barely covered ...

  1. #1
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96

    Alternate Way

    Ok this is kind of a bizzarre question but it's really bugging me.

    The book I'm reading has barely covered the basics (only cout, cin, math, small if statements (not else if or anything like that), and that's about it. One of the excersizes at the end of the chapter is:

    Write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers. The screen dialogue should appear as follows:

    Input three different integers: 13 27 14
    Sum is 54
    Average is 18
    Product is 4914
    Smallest is 13
    Largest is 27
    Now since I do know a little more about programming than the chapter I am currently in I did this:
    Code:
    //Outputs various facts.
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
       int n1, n2, n3, sum, average, product;
    
       cout<<"Input 3 integers: ";
       cin>>n1>>n2>>n3;
    
       sum = n1+n2+n3;
       average = sum/3;
       product = n1*n2*n3;
    
       cout<<"Sum is "<<sum<<"\n";
       cout<<"Average is "<<average<<"\n";
       cout<<"Product is "<<product<<"\n";
    
       if ( n1 < n2 && n1 < n3 )
        cout<<"Smallest is "<<n1<<"\n";
       if ( n2 < n1 && n2 < n3 )
        cout<<"Smallest is "<<n2<<"\n";
       if ( n3 < n1 && n3 < n2 )
        cout<<"Smallest is "<<n3<<"\n";
    
       if ( n1 > n2 && n1 > n3 )
        cout<<"Largest is "<<n1<<"\n";
       if ( n2 > n1 && n2 > n3 )
        cout<<"Largest is "<<n2<<"\n";
       if ( n3 > n1 && n3 > n2 )
        cout<<"Largest is "<<n3<<"\n";
       return 0;
    }
    Thing is, he's never taught the && yet. So I'm thinking there is a way to do this without it. Is there? Please try to keep it basic. Yes, I know there are shorter ways to doing this, but I am just trying to follow my book right now. Thanks.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    if (n1>n2)
    {
    	if (n2>n3); //n1 is greatest
    	else if (n3>n1); //n3 is greatest
    }
    else
    {
    	if (n3>n2); //n3 is greatest
    	else; //n2 is greatest
    }
    Or something like this. You should check for equality too.

    EDIT: Here is another one using a function:
    Code:
    int largest(int a,int b)
    {
         if (a>b)
             return a;
         else
             return b;
    }
    
    .
    .
    .
    max=largest( largest(n1,n2),n3);
    It's technically just a bunch of if statements.
    Last edited by bennyandthejets; 11-23-2003 at 11:12 PM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    It's ugly, either way:

    Code:
    int a = 4, b = 3, c = 5;
    
    int largest, smallest = largest = a;
    
    if(b > largest)
     largest = b;
    else if(b < smallest)
     smallest = b;
     
    if(c > largest)
     largest = c;
    else if(c < smallest)
     smallest = c;
    
    cout << "Max: " << largest << endl;
    cout << "Min: " << smallest << endl;


    The problem is primarily that it only solves *this* particular problem. You need an automated way of doing it in such a way that works with any amount of data. Naturally, arrays enable that to work so that you can simply plug them into a function like:


    int largest(int array[], int length);


    You may already know how to do this (as you mentioned you were ahead of your class), but if not, I would strongly advise that you give it a go.



    ITSA
    Socket Library!

  4. #4
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    LOL You guys I found what I had to do. Check this out:

    Code:
    //determines largest and smallest out of 5 integers
    #include <iostream>
    using std::cout;
    using std::cin;
    
    int main()
    {
       int n1, n2, n3, n4, n5;
       cout<<"Type some integers: ";
       cin>>n1>>n2>>n3>>n4>>n5;
    
       if ( n1 > n2 )
        if ( n1 > n3 )
         if ( n1 > n4 )
          if ( n1 > n5 )
           cout<<"The largest integer is: "<<n1<<"\n";
       if ( n2 > n1 )
        if ( n2 > n3 )
         if ( n2 > n4 )
          if ( n2 > n5 )
           cout<<"The largest integer is: "<<n2<<"\n";
       if ( n3 > n1 )
        if ( n3 > n2 )
         if ( n3 > n4 )
          if ( n3 > n5 )
           cout<<"The largest integer is: "<<n3<<"\n";
       if ( n4 > n1 )
        if ( n4 > n3 )
         if ( n4 > n2 )
          if ( n4 > n5 )
           cout<<"The largest integer is: "<<n4<<"\n";
       if ( n5 > n1 )
        if ( n5 > n3 )
         if ( n5 > n4 )
          if ( n5 > n2 )
           cout<<"The largest integer is: "<<n5<<"\n";
       if ( n1 < n2 )
        if ( n1 < n3 )
         if ( n1 < n4 )
          if ( n1 < n5 )
           cout<<"The smallest integer is: "<<n1<<"\n";
       if ( n2 < n1 )
        if ( n2 < n3 )
         if ( n2 < n4 )
          if ( n2 < n5 )
           cout<<"The smallest integer is: "<<n2<<"\n";
       if ( n3 < n1 )
        if ( n3 < n2 )
         if ( n3 < n4 )
          if ( n3 < n5 )
           cout<<"The smallest integer is: "<<n3<<"\n";
       if ( n4 < n1 )
        if ( n4 < n3 )
         if ( n4 < n2 )
          if ( n4 < n5 )
           cout<<"The smallest integer is: "<<n4<<"\n";
       if ( n5 < n1 )
        if ( n5 < n3 )
         if ( n5 < n4 )
          if ( n5 < n2 )
           cout<<"The smallest integer is: "<<n5<<"\n";
       return 0;
    }
    Boy that was one heck of a mind jogger. At one point I made an error and my compiler said "Confused by earlier errors, bailing out" LOL.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    That's really fugly. If you're going to write crazy code, at least do it right:
    Code:
    #include <iostream>
    
    using namespace std;
    
    #define max(a,b) ( (a) > (b) ? (a) : (b) )
    #define min(a,b) ( (a) < (b) ? (a) : (b) )
    
    int main()
    {
      int n[5];
    
      cout<<"Enter five numbers: "<<flush;
      cin>> n[0] >> n[1] >> n[2] >> n[3] >> n[4];
      cout<<"The largest is "<< max ( max ( n[0], n[1] ), max ( max ( n[2], n[3] ), n[4] ) ) <<endl;
      cout<<"The smallest is "<< min ( min ( n[0], n[1] ), min ( min ( n[2], n[3] ), n[4] ) ) <<endl;
    }
    My best code is written with the delete key.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Still pretty lumpy, in a chewy crunchy kinda way

    Code:
    #include <climits>
    
    int n[5];
    int max = INT_MIN;  // lowest possible max
    for ( int i = 0 ; i < 5 ; i++ ) {
        cin >> n[i];
        if ( n[i] > max ) max = n[i];
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alternate Languages in C++
    By Kadell in forum C++ Programming
    Replies: 5
    Last Post: 07-03-2007, 04:35 AM
  2. alternate rendering loop
    By psychopath in forum Game Programming
    Replies: 2
    Last Post: 07-16-2005, 12:30 PM
  3. Alternate energy sources
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 02-02-2005, 06:07 PM
  4. alternate to string.data()
    By Eber Kain in forum C++ Programming
    Replies: 1
    Last Post: 06-07-2004, 12:59 PM
  5. alternate function to sprintf
    By Eber Kain in forum C++ Programming
    Replies: 5
    Last Post: 12-27-2002, 09:38 PM

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