Finding the lowest score

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #16
    Originally posted by namcintosh
    Oh. I though that at first all I could do was just declare LOW to be a large number and then do the if/else if statements to test it out. But I guess it didn't work because all of the numbers are smaller than 1000.
    Even if there were number larger than 1000 it would not work as u planed.One more thing:

    It can be done without using loop but then it would be done only and only by
    using if,not if-else.

    If u are intrested/intrigued by this let me know!!!

    :D

    Savage

    Comment

    • namcintosh
      New Member
      • Apr 2007
      • 67

      #17
      Originally posted by Savage
      Even if there were number larger than 1000 it would not work as u planed.One more thing:

      It can be done without using loop but then it would be done only and only by
      using if,not if-else.

      If u are intrested/intrigued by this let me know!!!

      :D

      Savage
      Oh, why Savage, please let me know. I am very interested. :-)

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #18
        Glad to hear that!!

        So here we go:

        Start is the same:

        int S=score1;

        now to the important part:

        Code:
        if(S>score2) s=score2;
        if(S>score3) s=score3;
        if(s>score4) s=score4;
        if(s>score5) s=score5;
        this is it.

        Now to test it:

        score1=10;
        score2=15;
        score3=8;
        score4=12;
        score5=9;

        1) s=10;
        2) s>15 no;
        3) s>8 yes-> s=8;
        4) s>12 no;
        5) s>9 no;

        6) s=8

        As u can see it's 'alive' and working properly.

        And why it would not work with LO and if-else?

        It would not work becasue it's wrong logic to set number to random value,always instead set it to one of the scores and then search if there is a smaller number and secound that if-else chain would work only in loop.


        Savage

        Comment

        • fantasticamir
          New Member
          • Apr 2007
          • 42

          #19
          remove all of "else"s and it is gonna work!

          Comment

          • namcintosh
            New Member
            • Apr 2007
            • 67

            #20
            Originally posted by Savage
            Glad to hear that!!

            So here we go:

            Start is the same:

            int S=score1;

            now to the important part:

            Code:
            if(S>score2) s=score2;
            if(S>score3) s=score3;
            if(s>score4) s=score4;
            if(s>score5) s=score5;
            this is it.

            Now to test it:

            score1=10;
            score2=15;
            score3=8;
            score4=12;
            score5=9;

            1) s=10;
            2) s>15 no;
            3) s>8 yes-> s=8;
            4) s>12 no;
            5) s>9 no;

            6) s=8

            As u can see it's 'alive' and working properly.

            And why it would not work with LO and if-else?

            It would not work becasue it's wrong logic to set number to random value,always instead set it to one of the scores and then search if there is a smaller number and secound that if-else chain would work only in loop.


            Savage

            OK, so how can I get it to return a value

            Comment

            • ilikepython
              Recognized Expert Contributor
              • Feb 2007
              • 844

              #21
              Originally posted by namcintosh
              OK, so how can I get it to return a value
              From the function? You add "return s;" at the end of the function.

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #22
                Yup.

                And then when u call the function call it like:

                int s;//this is in main

                s=findLowest(sc ore1,score2,sco re3,score4,scor e5);

                and also:
                function must be of :int,float...al l those types except void which can't return anything.

                Savage

                Comment

                • namcintosh
                  New Member
                  • Apr 2007
                  • 67

                  #23
                  Originally posted by Savage
                  Yup.

                  And then when u call the function call it like:

                  int s;//this is in main

                  s=findLowest(sc ore1,score2,sco re3,score4,scor e5);

                  and also:
                  function must be of :int,float...al l those types except void which can't return anything.

                  Savage
                  Okay, thanks!

                  Comment

                  • Savage
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1759

                    #24
                    Originally posted by namcintosh
                    Okay, thanks!
                    U are welcome!!

                    Savage

                    Comment

                    • namcintosh
                      New Member
                      • Apr 2007
                      • 67

                      #25
                      Originally posted by Savage
                      U are welcome!!

                      Savage

                      Ok, but now I am having problems with the void calcAverage() function. This function should calculate and display the average of the four highest test scores. It should also be called just once by main, and should be passed the five scores.

                      Now, here s my program so far:

                      Code:
                      #include <iostream>
                      #include <conio>
                      using namespace std;
                      
                      //Function prototype
                      void getscore(int&, int&, int&, int&, int&);
                      int findLowest (int, int, int, int, int, int);
                      void calcAverage (int)
                      int main()
                      {
                              int average, score1, score2, score3, score4, score5;
                      
                              getscore (score1, score2, score3, score4, score5);
                              calcAverage (average)
                              getch();
                              return 0;
                      }
                      
                      
                      
                      void getscore (int &score1, int &score2, int &score3, int &score4, int &score5)
                      {
                              cout << "Enter first score: ";
                              cin  >> score1;
                              cout << "Enter second score: ";
                              cin  >> score2;
                              cout << "Enter third score: ";
                              cin  >> score3;
                              cout << "Enter fourth score: ";
                              cin  >> score4;
                              cout << "Enter fifth score: ";
                              cin  >> score5;
                      }
                      
                      void calcAverage (int average)
                      {
                              int average;
                      }
                      int findLowest(int small, int score1, int score2, int score3, int score4, int score5)
                      {
                              small =score1;
                      
                              if(small>score2)
                              {
                              small=score2;
                              }
                              if(small>score3)
                              {
                              small=score3;
                              }
                              if(small>score4)
                              {
                              small=score4;
                              }
                              if(small>score5)
                              {
                              small=score5;
                              }
                              return small; 
                      }
                      I know how to calculate the average: Average = ((score 1 + score 2 + score 3 + score 4 + score5 -SMALL))/4

                      I just don't know how to put it in the program

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #26
                        Originally posted by namcintosh
                        This function should calculate and display the average of the four highest test scores. It should also be called just once by main, and should be passed the five scores.
                        Code:
                        void calcAverage (int)
                        int main()
                        {
                                calcAverage (average)
                        }
                        void calcAverage (int average)
                        {
                                int average;
                        }
                        I know how to calculate the average: Average = ((score 1 + score 2 + score 3 + score 4 + score5 -SMALL))/4

                        I just don't know how to put it in the program
                        I snipped the above for brevity. So reading what the function should do, what do you need to pass to it, and where do you put that? Once you have those, they are considered local variables, so your implementation should be ready to work.

                        (If you're getting stuck on the function declarations/implementations , look at the example of the last one you just made. What did the outline say for what you needed to do to create it, and then how did you create it - where did you put the things it said.) I'm pretty sure you can figure this one out - just look at the one you've already created as an example.

                        Comment

                        • namcintosh
                          New Member
                          • Apr 2007
                          • 67

                          #27
                          Okay, this is what I came up with:


                          Code:
                          void getscore(int&, int&, int&, int&, int&);
                          int findLowest (int, int, int, int, int, int);
                          [B]void calcAverage (int)[/B]
                          int main()
                          {
                                  int average, score1, score2, score3, score4, score5;
                          
                                  getscore (score1, score2, score3, score4, score5);
                                  calcAverage (average)
                                  getch();
                                  return 0;
                          }
                          
                          
                          
                          void getscore (int &score1, int &score2, int &score3, int &score4, int &score5)
                          {
                                  cout << "Enter first score: ";
                                  cin  >> score1;
                                  cout << "Enter second score: ";
                                  cin  >> score2;
                                  cout << "Enter third score: ";
                                  cin  >> score3;
                                  cout << "Enter fourth score: ";
                                  cin  >> score4;
                                  cout << "Enter fifth score: ";
                                  cin  >> score5;
                          }
                          
                          void calcAverage (int average)
                          {
                                  int average;
                                  average = ((score1 + score2 + score3 + score4 + score5)-small)/4;
                          
                                  cout << average;
                          }
                          int findLowest(int small, int score1, int score2, int score3, int score4, int score5)
                          {
                                  small =score1;
                          
                                  if(small>score2)
                                  {
                                  small=score2;
                                  }
                                  if(small>score3)
                                  {
                                  small=score3;
                                  }
                                  if(small>score4)
                                  {
                                  small=score4;
                                  }
                                  if(small>score5)
                                  {
                                  small=score5;
                                  }
                                  return small; 
                          }
                          But now, when I try to run the program, I am getting a declaration syntax error (see bold) Why is it doing that???

                          Comment

                          • Savage
                            Recognized Expert Top Contributor
                            • Feb 2007
                            • 1759

                            #28
                            [QUOTE=namcintos h]Okay, this is what I came up with:



                            void calcAverage (int)


                            It looks like u are missing semicolumn.

                            Savage

                            Comment

                            • Ganon11
                              Recognized Expert Specialist
                              • Oct 2006
                              • 3651

                              #29
                              It looks like you are missing a semicolon after the declaration.

                              Comment

                              • Savage
                                Recognized Expert Top Contributor
                                • Feb 2007
                                • 1759

                                #30
                                I can't belive that i writed semicolumn instead of semicolon.LOL

                                Savage

                                Comment

                                Working...