435,463 Members | 3,115 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,463 IT Pros & Developers. It's quick & easy.

Finding the lowest score

 P: 67 First of all, here is my program: Expand|Select|Wrap|Line Numbers #include  #include  using namespace std;   //Function prototype void getscore(int&, int&, int&, int&, int&); void findLowest (int, int, int, int, int, int); int main() {         int LO, score1, score2, score3, score4, score5;           getscore (score1, score2, score3, score4, score5);         findLowest(LO, score1, score2, score3, score4, score5);           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 findLowest(int LO, int score1, int score2, int score3, int score4, int score5) {           LO = 1000;           if (score1 < LO)         {             LO = score1;             cout << "The lowest score is" <
54 Replies

 Expert Mod 2.5K+ P: 4,677 Your else-if chain only checks to see if the numbers are less than LO, or 1000, and they only do it once, there's no looping, so it only does the first one (100), but doesn't loop back after LO is reset. (If a branch is taken in an if-else statement, the rest of it is disregarded) You should put that in a loop, and you'll have to change your logic a bit - otherwise it will print out each time the loop is executed. Apr 20 '07 #2

 P: 67 Your else-if chain only checks to see if the numbers are less than LO, or 1000, and they only do it once, there's no looping, so it only does the first one (100), but doesn't loop back after LO is reset. (If a branch is taken in an if-else statement, the rest of it is disregarded) You should put that in a loop, and you'll have to change your logic a bit - otherwise it will print out each time the loop is executed. Can you please show me in my program where that goes??? Apr 20 '07 #3

 Expert Mod 2.5K+ P: 4,677 I tried it with if statements, but all of the numbers printed out as low numbers Have you tried a while loop? Having it repeat several times, and then exiting only when you're sure it has checked all 4 other values against your lowest? Apr 20 '07 #4

 Expert Mod 2.5K+ P: 4,677 Can you please show me in my program where that goes??? I'd recommend sticking it here: Expand|Select|Wrap|Line Numbers void findLowest(int LO, int score1, int score2, int score3, int score4, int score5) {           LO = 1000;           if (score1 < LO)         {             LO = score1;             cout << "The lowest score is" <

 P: 67 Have you tried a while loop? Having it repeat several times, and then exiting only when you're sure it has checked all 4 other values against your lowest? I tried a while loop, but I kept getting an error message Apr 20 '07 #6

 Expert Mod 2.5K+ P: 4,677 I tried a while loop, but I kept getting an error message What's the error? Apr 20 '07 #7

 P: 67 What's the error? It keeps saying that I am missing a brace Oh, and I tried that code that you gave me, and it's still doing the same thing. Run my program on your computer and you will see what I am talking about. Apr 20 '07 #8

 Expert 100+ P: 1,764 You need to use a loop just as sicarie sayed. LO in call to function is not necsasary and why are u using the largest number instead of smallest,more important is to use a loop like: int(int score1,int score2,int score3,int score4) { int S=score1;//we need to declare one number as smallest int i=0;//we also need a counter for the loop //Right here we need a do - while loop wchich will loop until it finds the smallest number(while i<4) so here goes the loop body: Expand|Select|Wrap|Line Numbers       if(score2

 P: 67 Why are there four scores instead of five??? There should be five scores Apr 20 '07 #10

 Expert 100+ P: 1,764 Yes,and I know it.I wish that u learn so I have left one for u!! :D Savage Apr 20 '07 #11

 Expert 100+ P: 1,764 Another thing: Functions like these are not modular so I would rather use arrays for such function just in case that I feel need for it some time in future. Savage Apr 20 '07 #12

 P: 67 Yes,and I know it.I wish that u learn so I have left one for u!! :D Savage You have one more what Is there any way that I can use it doing the if/else if statements Apr 20 '07 #13

 Expert 100+ P: 1,764 You have one more what Is there any way that I can use it doing the if/else if statements Well,in that case do it like: S=score1; Expand|Select|Wrap|Line Numbers if(s>score2) s=score2; else if(s>score3) s=score3;        else if(s>score4) s= score4;               else s=score5 this approach to reguires a same loop Apr 20 '07 #14

 P: 67 Well,in that case do it like: S=score1; Expand|Select|Wrap|Line Numbers if(s>score2) s=score2; else if(s>score3) s=score3;        else if(s>score4) s= score4;               else s=score5 this approach to reguires a same loop 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. Apr 22 '07 #15

 Expert 100+ P: 1,764 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 Apr 22 '07 #16

 P: 67 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. :-) Apr 23 '07 #17

 Expert 100+ P: 1,764 Glad to hear that!! So here we go: Start is the same: int S=score1; now to the important part: Expand|Select|Wrap|Line Numbers 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 Apr 23 '07 #18

 P: 42 remove all of "else"s and it is gonna work! Apr 23 '07 #19

 P: 67 Glad to hear that!! So here we go: Start is the same: int S=score1; now to the important part: Expand|Select|Wrap|Line Numbers 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 Apr 23 '07 #20

 Expert 100+ P: 844 OK, so how can I get it to return a value From the function? You add "return s;" at the end of the function. Apr 23 '07 #21

 Expert 100+ P: 1,764 Yup. And then when u call the function call it like: int s;//this is in main s=findLowest(score1,score2,score3,score4,score5); and also: function must be of :int,float...all those types except void which can't return anything. Savage Apr 23 '07 #22

 P: 67 Yup. And then when u call the function call it like: int s;//this is in main s=findLowest(score1,score2,score3,score4,score5); and also: function must be of :int,float...all those types except void which can't return anything. Savage Okay, thanks! Apr 23 '07 #23

 Expert 100+ P: 1,764 Okay, thanks! U are welcome!! Savage Apr 23 '07 #24

 P: 67 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: Expand|Select|Wrap|Line Numbers #include  #include  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 Apr 23 '07 #25

 Expert Mod 2.5K+ P: 4,677 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. Expand|Select|Wrap|Line Numbers 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. Apr 23 '07 #26

 P: 67 Okay, this is what I came up with: Expand|Select|Wrap|Line Numbers 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;         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??? Apr 24 '07 #27

 Expert 100+ P: 1,764 [quote=namcintosh]Okay, this is what I came up with: void calcAverage (int) It looks like u are missing semicolumn. Savage Apr 24 '07 #28

 Expert 2.5K+ P: 3,652 It looks like you are missing a semicolon after the declaration. Apr 24 '07 #29

 Expert 100+ P: 1,764 I can't belive that i writed semicolumn instead of semicolon.LOL Savage Apr 24 '07 #30

 P: 67 Alright, then, here is my program. I've already ran the program, but I still want you to run int and see what you think of it. Expand|Select|Wrap|Line Numbers #include  #include  using namespace std;   //Function prototype void getscore(int&, int&, int&, int&, int&); int findLowest (int, int, int, int, int, int); void calcAverage (int, int, int, int, int, int, int); int main() {         int average, score1, score2, score3, score4, score5, small;           getscore (score1, score2, score3, score4, score5);         calcAverage (average, score1, score2, score3, score4, score5, small);         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 score1, int score2, int score3, int score4, int score5, int small) {             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;  } Thanks for the encouragement, goys!! Apr 24 '07 #31

 Expert 100+ P: 1,764 Can't see any error. It should work,probably :D Savage Apr 24 '07 #32

 P: 67 Can't see any error. It should work,probably :D Savage Uh- oh, I think I ran into a problem: When I entered the values 100, 100, 100, 100, 100, it gave me an average of 125. Why did that happen??? Apr 24 '07 #33

 Expert 100+ P: 1,764 Uh- oh, I think I ran into a problem: When I entered the values 100, 100, 100, 100, 100, it gave me an average of 125. Why did that happen??? In main set average to 0 before calling the function. Apr 24 '07 #34

 P: 67 In main set average to 0 before calling the function. I tried that, and this is what happened: Expand|Select|Wrap|Line Numbers int main() {         int average = 0, score1, score2, score3, score4, score5, small;           getscore (score1, score2, score3, score4, score5);         calcAverage (average, score1, score2, score3, score4, score5, small);         getch();         return 0; } Enter first score: 100 Enter second score: 100 Enter third score: 100 Enter fourth score: 100 Enter fifth score: 100 125 It's still outputting 125. Apr 24 '07 #35

 Expert 2.5K+ P: 3,652 Hint: When, in your program, are you finding small? Apr 24 '07 #36

 P: 67 Hint: When, in your program, are you finding small? I'm finding small in my findLowest() function Apr 24 '07 #37

 Expert 100+ P: 1,764 Uh- oh, I think I ran into a problem: When I entered the values 100, 100, 100, 100, 100, it gave me an average of 125. Why did that happen??? U are missing caller for findLowest() inside main() so it's just seting it to 0: average=(100+100+100+100+100)-0)/4=125; This should fix that!! Savage Apr 24 '07 #38

 P: 67 U are missing caller for findLowest() inside main() so it's just seting it to 0: average=(100+100+100+100+100)-0)/4=125; This should fix that!! Savage I tried that, and the same thing happened. Actually, here are the instructions: void calcAverage()-should calculate and display the average of the four highest scores. This function should be called just once by main and should be passed the five scores. int findLowest()-should find and return the lowest of the five scores passed to it. It should be called by the calcAverage, who uses the function to determine which of the five scores to drop. So, what am I doing wrong??:-( Apr 24 '07 #39

 Expert 100+ P: 1,764 I tried that, and the same thing happened. Actually, here are the instructions: void calcAverage()-should calculate and display the average of the four highest scores. This function should be called just once by main and should be passed the five scores. int findLowest()-should find and return the lowest of the five scores passed to it. It should be called by the calcAverage, who uses the function to determine which of the five scores to drop. So, what am I doing wrong??:-( How did u added the caller was it like: small=findLowest(score1,score2,score3,score4,score 5); ??? Savage Apr 24 '07 #40

 P: 67 How did u added the caller was it like: small=findLowest(score1,score2,score3,score4,score 5); ??? Savage What do u mean, how did I add the caller Apr 24 '07 #41

 Expert 100+ P: 1,764 What do u mean, how did I add the caller Sorry,I was asking u how have u called function inside main.Was it called the same way as in my previous post? PS:I have need for sleep.(ZzzzzzzzzZzzzzzz) Savage Apr 24 '07 #42

 P: 67 Sorry,I was asking u how have u called function inside main.Was it called the same way as in my previous post? Savage Yes, it was, and I still ran crazy. Run my program, please and you will see what I mean. Apr 24 '07 #43

 Expert 100+ P: 1,764 Yes, it was, and I still ran crazy. Run my program, please and you will see what I mean. i fixed it: This is what I have changed: int findLowest (int, int, int, int, int);//there was six of them small is unecsasary. //main int average=0, score1=0, score2=0, score3=0, score4=0, score5, small=0; //getscore small=findLowest(score1,score2,score3,score4,score 5); //calcAverage //gethc and return and finaly ur function: int small =score1;//we don't pass small to the function therefore we create another variable. I have runed this and output is 100. Savage Apr 24 '07 #44

 P: 67 i fixed it: This is what I have changed: int findLowest (int, int, int, int, int);//there was six of them small is unecsasary. //main int average=0, score1=0, score2=0, score3=0, score4=0, score5, small=0; //getscore small=findLowest(score1,score2,score3,score4,score 5); //calcAverage //gethc and return and finaly ur function: int small =score1;//we don't pass small to the function therefore we create another variable. I have runed this and output is 100. Savage I hate to sound like a complete martian, but can you please explain to me what each line means??? Apr 25 '07 #45

 Expert 100+ P: 1,764 In my first line I have changed ur prototype dunction definition.It was like: findLowest(int,int,int,int,int,int) to findLowest(int,int,int,int,int); the sixth argument,small, is unecsessary and I thought it might represent a problem. In my secound line I have set all the variables to 0 value because that's neutral value for addition. In 3 line I added the call for findLowest. small=findLowest(score1,score2,score3,score4,score 5); and finaly in function, findLowest(), declaration I changed this: int findLowest(int score1,int score2,int score3,int score4,int score5,int small) to int findLowest(int score1,int score2,int score3,int score4,int score5) and then line after I chnaged this:small=score1; to int small=score1; Savage Apr 25 '07 #46

 P: 67 This is what I did: Expand|Select|Wrap|Line Numbers void getscore(int&, int&, int&, int&, int&); int findLowest (int, int, int, int, int); void calcAverage (int, int, int, int, int, int, int); int main() {         int average=0, score1=0, score2=0, score3=0, score4=0, score5=0, small=0;           getscore (score1, score2, score3, score4, score5);         calcAverage (average, score1, score2, score3, score4, score5, small);         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 score1, int score2, int score3, int score4, int score5, int small) {             average = ((score1 + score2 + score3 + score4 + score5)-small)/4;           cout << average;   } int findLowest(int score1, int score2, int score3, int score4, int score5) {         int small =score1;           if(small>score2)         {         small=score2;         }         if(small>score3)         {         small=score3;         }         if(small>score4)         {         small=score4;         }         if(small>score5)         {         small=score5;         }         return small;  } My output still came out to be 125. I don't know whether or not I put those things in the right place. Apr 25 '07 #47

 Expert 100+ P: 844 This is what I did: Expand|Select|Wrap|Line Numbers void getscore(int&, int&, int&, int&, int&); int findLowest (int, int, int, int, int); void calcAverage (int, int, int, int, int, int, int); int main() {         int average=0, score1=0, score2=0, score3=0, score4=0, score5=0, small=0;           getscore (score1, score2, score3, score4, score5);         calcAverage (average, score1, score2, score3, score4, score5, small);         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 score1, int score2, int score3, int score4, int score5, int small) {             average = ((score1 + score2 + score3 + score4 + score5)-small)/4;           cout << average;   } int findLowest(int score1, int score2, int score3, int score4, int score5) {         int small =score1;           if(small>score2)         {         small=score2;         }         if(small>score3)         {         small=score3;         }         if(small>score4)         {         small=score4;         }         if(small>score5)         {         small=score5;         }         return small;  } My output still came out to be 125. I don't know whether or not I put those things in the right place. You never called findLowest, so small is equal to 0. Call findLowest before you call calcavergae(). Apr 25 '07 #48

 Expert 100+ P: 1,764 Have patiance: Look at the caller order: //getscore small=findLowest(score1,score2,score3,score4,score 5); //calcAverage Have u seend this? Also ilikepython sayed the same thing. Read every post slowly word by word and ur problem will banish. Savage Apr 25 '07 #49

 P: 67 I am still having problems:-( I am following you all's suggestions, and it's still coming out to be 125. Apr 27 '07 #50

54 Replies 