First of all, here is my program: - #include <iostream>
-
#include <conio>
-
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" <<LO <<endl;
-
}
-
-
else if (score2 < LO)
-
{
-
LO = score2;
-
cout << "The lowest score is" <<LO <<endl;
-
}
-
else if (score3 < LO)
-
{
-
LO = score3;
-
cout << "The lowest score is" <<LO <<endl;
-
}
-
else if (score4 < LO)
-
{
-
LO = score4;
-
cout << "The lowest score is" <<LO <<endl;
-
}
-
else if (score5 < LO)
-
{
-
LO = score5;
-
cout << "The lowest score is" <<LO <<endl;
-
}
-
}
Okay, I got one part of the program to work right. The only problem is finding the lowest score.
I declared some if/else if statements and also assigned LO to a large number, such as 1000. Here's my output: - Enter first score: 100
-
Enter second score: 75
-
Enter third score: 55
-
Enter fourth score: 100
-
Enter fifth score: 85
- The lowest score is100
Do you see what I mean? It declares 100 as the lowest number. Why is it doing that?
54 6638 sicarie 4,677
Recognized Expert Moderator Specialist
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.
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???
sicarie 4,677
Recognized Expert Moderator Specialist
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?
sicarie 4,677
Recognized Expert Moderator Specialist
Can you please show me in my program where that goes???
I'd recommend sticking it here: -
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" <<LO <<endl;
-
}
-
-
else if (score2 < LO)
-
{
-
LO = score2;
-
cout << "The lowest score is" <<LO <<endl;
-
}
-
else if (score3 < LO)
-
{
-
LO = score3;
-
cout << "The lowest score is" <<LO <<endl;
-
}
-
else if (score4 < LO)
-
{
-
LO = score4;
-
cout << "The lowest score is" <<LO <<endl;
-
}
-
else if (score5 < LO)
-
{
-
LO = score5;
-
cout << "The lowest score is" <<LO <<endl;
-
}
-
}
Then you could change the function definition to return an int - if you loop until you're sure and return it, you can print it out in your main, not have to worry about IO in your function.
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
sicarie 4,677
Recognized Expert Moderator Specialist
I tried a while loop, but I kept getting an error message
What's the error?
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.
Savage 1,764
Recognized Expert Top Contributor
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: - if(score2<S) S=score2,i++;continue;
-
if(score3<S) S=score3,i++;continue;
-
if(score4<S) S=score4,i++;continue;
This way code is smaller and will execute more faster because all those if-else can realy slow down.
Now lets test it:
score1=20;
score2=15;
score3=8;
score4=13;
S=20;
1)15<20 yes,S=15;
2)15<15 no
8<15 yes,S=8;
3) 15<8 no.
8<8 no.
13<8 no.
As we can see smallest number is 8 which is correct.
Savage
Why are there four scores instead of five??? There should be five scores
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Han |
last post by:
Determining the pattern below has got my stumped.
I have a page of HTML and need to find all occurrences of the following
pattern:
score=9999999999&
The number shown can be 5-10 characters in length. I would like to extract
only the number, stripping off the "score=" and "&".
|
by: Porthos |
last post by:
Hi All,
I'm trying to find the minimum value of a set of data (see below).
I want to compare the lengths of these attribute values and display
the lowest one.
This would be simple if I could re-assign values to a variable,
but from what I gather I can't do that. How do I keep track of the
lowest value as I loop through? My XSL document only finds the length
of each string and prints it out (for now). I can write a template
|
by: andreas.maurer1971 |
last post by:
Hi all,
since a few years I use the following statement to find duplicate
entries in a table:
SELECT t1.id, t2.id,...
FROM table AS t1 INNER JOIN table AS t2
ON t1.field = t2.field
WHERE t1.id < t2.id
|
by: Matt Chwastek |
last post by:
Anyone who can help,
I am curretnly attempting to write some code that will allow iteration
using a vector<intfrom the highest possilbe degree of a combination
of ones & zeros (111, 110, 101, 011, 100, 010, 001, 000). The ordering
of element containing the same number of ones is not important to the
code, just the fact that the highest number of ones are iterated first.
I would prefer not to use a binary string as eventually the
|
by: sonic |
last post by:
Does anyone know what I can do to this function to get it to drop the lowest value? Thanks for any insight.
void sort(double* score, int size)
{
int startScan;
int minIndex;
double minValue;
| |
by: davenet |
last post by:
Hi,
I'm new to Python and working on a school assignment.
I have setup a dictionary where the keys point to an object. Each
object has two member variables. I need to find the smallest value
contained in this group of objects.
The objects are defined as follows:
|
by: Flanders |
last post by:
I have developed a small arcade game with the help of a few VB books. A
scoring system was implemented in the design of the game but I as hoping
that some one would be able to instruct me on how display the score at the
end of the game. At the moment when a player finishes the game I have a
window displaying "Puzzle Solved." If I could display the players score
under this in the same window would be great.
This is a quick copy and paste...
|
by: marybrown |
last post by:
i will write the complete problem i am facing. Here is the input file i am using.
sxoght: #query hit score probability qstart qend qorientation tstart tend matches mismatches gapOpening gaps
@SNPSTER4_104_308EFAAXX:1:1:1694:128
GGGATAAGAGAGGTGCATGTTGGTATTTAAGGTAGT
1 alignment(s) -- reports limited to 10 alignment(s)
sxoght: SNPSTER4_104_308EFAAXX:1:1:1694:128 gi|122939163|ref|NM_000165.3| -10 ...
|
by: hamishmcgee |
last post by:
Ok, so for a project at university I have to create a High Score table in C++ with Visual Studio. Just to let you know this is my first year at university and also my first time ever learning C++.
So far I have a working menu, which lets you enter in your name, highscore and date which then gets saved to a file and is outputted via another option on the menu. I had a lot of trouble gettin the user info to save to a file in the first place, then...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |