I can't get the Largest and Smallest numbers using functions in C++.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ArtStar
    New Member
    • Feb 2015
    • 2

    I can't get the Largest and Smallest numbers using functions in C++.

    #include<iostre am>
    using namespace std;
    int computeTotal();
    int findSmallest();
    int findLargest();
    int main()
    {
    int n1,n2,n3,n4,n5, total,average,s mallest,largest ;

    cout << "Number 1:";
    cin >> n1;
    cout << "Number 2:";
    cin >> n2;
    cout << "Number 3:";
    cin >> n3;
    cout << "Number 4:";
    cin >> n4;
    cout << "Number 5:";
    cin >> n5;

    total = n1 + n2 + n3 + n4 + n5;
    cout << "Total:"<<total <<endl;

    average = (n1 + n2 + n3 + n4 + n5)/5;
    cout << "Average:"<<ave rage<<endl;


    cout << "Smallest:"<<sm allest<<endl;

    cout << "Largest :"<<largest<<en dl;

    system("pause") ;
    return 0;
    }//end of main()
    int computeTotal(in t n1,int n2,int n3,int n4,int n5)
    {
    int total=0;
    total = n1+n2+n3+n4+n5;
    cout << "Total :"<<total<<"\n" ;
    return total;
    }
    int computeAverage( int n1,int n2,int n3,int n4,int n5)
    {
    int average;
    average = (n1+n2+n3+n4+n5 )/5;
    cout << " Average :"<<average<<"\ n";
    return average;
    }
    int findSmallest(in t n1,int n2,int n3,int n4,int n5)
    {
    int smallest;

    smallest = n1;

    if(n2<smallest)
    smallest = n2;
    if(n3<smallest)
    smallest = n3;
    if(n4<smallest)
    smallest = n4;
    if(n5<smallest)
    smallest = n5;

    return smallest;
    }
    int findLargest(int n1,int n2,int n3,int n4,int n5)
    {
    int largest;
    largest = n1;

    if(n2>largest)
    largest = n2;
    if(n3>largest)
    largest = n3;
    if(n4>largest)
    largest = n4;
    if(n5>largest)
    largest = n5;


    return largest;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You display your largest and smallest numbers in main() but you never call the functions findSmallest and findLargest.

    Comment

    • ArtStar
      New Member
      • Feb 2015
      • 2

      #3
      I'm new of C++ programming. if you know better ways that can help me display the exact value for largest and smallest. thanks ;)

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        There are better ways to display the largest and smallest numbers but, as you say, you are just beginning. The first requirement of a program is that it works. After that, it's just fine points. I would not worry about this but I would just continue on. You will get better as time goes by.

        Comment

        • jaseel97
          New Member
          • Feb 2015
          • 16

          #5
          use this:
          int n[4];
          cout<<"Enter the four numbers:";
          cin>>n[0]>>n[1]>>n[2]>>n[3];

          int largest=a[0];
          int smallest=a[3];//these are temporary.

          for(int i=0;i<4;i++)
          {
          if(a[i]<smallest)
          smallest=a[i];
          if(a[i]>largest)
          largest=a[i];
          }
          you may split it up into two loops if you want them as separate functions

          Comment

          Working...