Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# How to use loops

Posted on 2011-09-17
Medium Priority
470 Views
Experts,
Details
Here is what I am trying to do, I have a program that needs to add the scores of 5 judges, subtract the highest and lowest scores / 3 and multiply by the level of difficulty (variable named =degreeDiff) and output the total score.

Problem
The variable called score only remembers the last score from the last judge (makes sense since it is being replaced by the score of the new judge). The math required to get the total score only happens based on the last number input by the user (i.e. judge)

Question
How to I keep all the scores from all the judges until the end of the loop so I can perform the correct calculations on it. Does each judges score need to be kept in a separate variable? Then add all the variables together to be placed into separate variable (ex. totalScore), then perform the calculations on that variable? Not sure how to get the desired result.

In addition, any idea why the input from the user is place on a separate line instead of next to the question? Example how it should look -  Divers name: Joe (see output image below)

Lastly, any idea how to start from Judge 1 instead of the program starting with Judge 0

Thank you very much for any help! Really appreciate it.

Output
Currently the total score variable outputs an incorrect number because it performs the calculations on the last known input.
``````using System;

namespace diver
{
internal class Program
{
private static void Main(string[] args)
{
string Name, city, processAnotherOneYoN;
double Score, highestScore, lowestScore, degreeDiff, totalScore;
int numberOfNames = 0;

#region Outer do while loop
do
{
Console.WriteLine("Enter a divers name: ");

Console.WriteLine("Enter a city: ");

Console.WriteLine("Enter a degree of diffculty: ");

while (degreeDiff < 1 || degreeDiff > 1.67)
{
Console.Write("Invalid degree of difficulty, please reenter (Valid Range: 1 – 1.67)\n");
Console.Write("Enter dive degree of difficulty: ");
}

highestScore = -1;
lowestScore = 11;
totalScore = 0;

#region for loop
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Enter the score given by judge {0}: ", i);

while (Score < 0 || Score > 10)
{
Console.WriteLine("Invalid score - Please reenter (Valid Range: 0 - 10)\n");
Console.WriteLine("Enter the score given by judge #{0}: ", i);
}
if (Score > highestScore)
highestScore = Score;
if (Score < lowestScore)
lowestScore = Score;

//Calculate Total Score
Score = Score - lowestScore;
Score = Score - highestScore;
Score = Score/3;
totalScore = Score*degreeDiff;
}

#endregion

Console.WriteLine();
Console.WriteLine("Name = {0}, Lowest Score  = {1:F}, Highest Score = {2:F}, Final Score = {3:F}", Name, lowestScore, highestScore, totalScore);

numberOfNames = numberOfNames + 1;

Console.WriteLine();
Console.Write("Do you want to process another diver? (Y/N)? ");
Console.WriteLine();
Console.WriteLine();
} while (processAnotherOneYoN == "Y");

#endregion

Console.WriteLine();
Console.WriteLine("Summary - Number of divers entered {0}, ", numberOfNames);
}
}
}
``````
0
Question by:Michael Reeve
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 8
• 7
• 4
• +1

LVL 34

Expert Comment

ID: 36554681
Use an array to store all the marks.

You can declare an array like this:
``````double[] scores = new double[5];
``````
Within the first loop you can use it like this to store the scores:
``````scores[i] = score;
``````
Which would go after the loop that checks a valid score has been entered.

To start at 0 either change the loop to this:
``````for (int i = 1; i < 6; i++)
``````
or change i to i+1 here,
``````Console.WriteLine("Enter the score given by judge #{0}: ", i+1);
``````
I don't quite follow how you get the final result I'm afraid.

If you just want to sum all the scores you could do that in the loop.

totalScore = totalScore+score*degreeDiff
0

Author Comment

ID: 36554761
Thank you for your reply imnorie, unfortunately I need to only use loops (not arrays - although I would agree that is a better route to go).  Here is the sample output I am trying to achieve:

Diver's name: Sue Jones
Diver's city: Dallas
Dive degree of difficulty: 1.9
Invalid degree of difficulty - Please reenter (Valid Range: 1 - 1.67)
Dive degree of difficulty: 2
Invalid degree of difficulty - Please reenter (Valid Range: 1 - 1.67)
Dive degree of difficulty: 1.2

Judge #1 score: 45
Invalid score - Please reenter (Valid Range: 0 - 10)
Judge #1 score: 3
Judge #2 score: 4.5
Judge #3 score 6.7
Judge #4 score 89
Invalid score - Please reenter (Valid Range: 0 - 10)
Judge #4 score 8
Judge #5 score: 9.2

Diver: Sue Jones
City: Dallas
Dive score: 7.68   <----this score needs to be the accumulation of all 5 judges scores minus the highest and lowest scores which are thrown out. I just can't continue to put the result of each judges score into a variable called score otherwise each time it will be erased when a new judges (the user) puts a records a new score. Could I use a counter to keep track? Not sure how that might work?
0

LVL 34

Expert Comment

ID: 36554788
I don't see how you can do this without an array unless you use multiple variables

I'm not even 100% sure how you would do it with multiple variables.

One thing I can't see you getting round is having to store all 5 scores until there in.

Then at the end calculate total score.

Maybe I'm missing something really obvious.

Why can't you use arrays anyway?
0

Author Comment

ID: 36554826
That is what I was thinking too, didn't know how to get around the issue without using multiple variables. I am new to C# and not familiar enough to use arrays and still get the outcome I am looking for. Not sure how to set it up in my current code so that it will store the values I need to output.
0

LVL 86

Expert Comment

ID: 36554917
Use Write() instead of WriteLine() to keep the cursor on the same line.

*Also, you may want to switch from Parse() to TryParse() so your app doesn't crash if bad input is typed in!

Refactor the loop like this to get your desired result:
``````for (int i = 1; i <= 5; i++)
{
Console.Write("Enter the score given by judge {0}: ", i);

while (Score < 0 || Score > 10)
{
Console.WriteLine("Invalid score - Please reenter (Valid Range: 0 - 10)\n");
Console.Write("Enter the score given by judge #{0}: ", i);
}
if (i == 1 || Score > highestScore)
highestScore = Score;
if (i == 1 || Score < lowestScore)
lowestScore = Score;
totalScore = totalScore + Score; // Just keep a running tally in the loop...
}
// Now subtract the low/high and do the calculation!
totalScore = totalScore - lowestScore;
totalScore = totalScore - highestScore;
totalScore = totalScore / (double)3;
totalScore = totalScore * degreeDiff;
``````
0

LVL 143

Expert Comment

ID: 36554932
totalscore: sum up on each "loop"
highest score: keep that value in 1 variable, and change to the score in the loop only if that one is higher than the current highest score
lowest score: same as above, just changing to lower value ...

that's it.
0

LVL 86

Expert Comment

ID: 36554933
Couldn't agree more...
0

LVL 34

Expert Comment

ID: 36554941
Knew I was missing something.
0

Author Comment

ID: 36555099
Thank you everyone! I still seem to be getting a negative number. If I am doing my math correctly it should (in this example - see attached file) be 6.5 as the answer. The totalScore variable is a negative number. Any ideas why it is giving me this answer?

0

LVL 86

Expert Comment

ID: 36555106

Also, why does your highest score say 6 when the highest entered was 10?
0

Author Comment

ID: 36555129
Good point. I think I am over working the code because is was working.
``````using System;

namespace diver
{
internal class Program
{
private static void Main(string[] args)
{
string Name, city, processAnotherOneYoN;
double Score, highestScore, lowestScore, degreeDiff, totalScore;
int numberOfNames = 0;

#region Outer do while loop
do
{
Console.Write("Enter a divers name: ");

Console.Write("Enter a city: ");

Console.Write("Enter a degree of diffculty: ");

while (degreeDiff < 1 || degreeDiff > 1.67)
{
Console.Write("Invalid degree of difficulty, please reenter (Valid Range: 1 – 1.67)\n");
Console.Write("Enter dive degree of difficulty: ");
}

highestScore = -1;
lowestScore = 11;
totalScore = 0;

#region for loop
for (int i = 0; i < 5; i++)
{
Console.Write("Enter the score given by judge {0}: ", i+1);

while (Score < 0 || Score > 10)
{
Console.WriteLine("Invalid score - Please reenter (Valid Range: 0 - 10)\n");
Console.Write("Enter the score given by judge #{0}: ", i);
}
if (i == 1 || Score > highestScore)
highestScore = Score;
if (i == 1 || Score < lowestScore)
lowestScore = Score;
totalScore = totalScore + Score;

// Now subtract the low/high and do the calculation!
totalScore = totalScore - lowestScore;
totalScore = totalScore - highestScore;
totalScore = totalScore / (double)3;
totalScore = totalScore * degreeDiff;
}

#endregion

Console.WriteLine();
Console.WriteLine("Name = {0}, Lowest Score  = {1:F}, Highest Score = {2:F}, Total Score = {3:F}", Name, lowestScore, highestScore, totalScore);

numberOfNames = numberOfNames + 1;

Console.WriteLine();
Console.Write("Do you want to process another diver? (Y/N)? ");
Console.WriteLine();
Console.WriteLine();

} while (processAnotherOneYoN == "Y");

#endregion

Console.WriteLine();
Console.WriteLine("Summary - Number of divers entered {0}, ", numberOfNames);
}
}
}
``````
0

Author Comment

ID: 36555151
The highest and lowest numbers work if I put this code back into it:

if (Score > highestScore)
highestScore = Score;
if (Score < lowestScore)
lowestScore = Score;

However, it is still coming back with a negative number as the total  score. Not sure why it would return a negative number?
0

LVL 34

Accepted Solution

Norie earned 2000 total points
ID: 36555156
You don't put the code to subtract the lowest and highest scores in the loop.

If you do then they will get subtract more than once and that could be why you get the negative nos.
``````                #region for loop
for (int i = 0; i < 5; i++)
{
Console.Write("Enter the score given by judge {0}: ", i+1);

while (Score < 0 || Score > 10)
{
Console.WriteLine("Invalid score - Please reenter (Valid Range: 0 - 10)\n");
Console.Write("Enter the score given by judge #{0}: ", i);
}
if (i == 1 || Score > highestScore)
highestScore = Score;
if (i == 1 || Score < lowestScore)
lowestScore = Score;
totalScore = totalScore + Score;

}

// loop finished

// Now subtract the low/high and do the calculation!
totalScore = totalScore - lowestScore;
totalScore = totalScore - highestScore;
totalScore = totalScore / (double)3;
totalScore = totalScore * degreeDiff;

#endregion
``````
0

LVL 86

Expert Comment

ID: 36555161
0

Author Comment

ID: 36555184
Idle Mind,
I appreciate you working on this with me, I had changed the if statement earlier per your post but the results were the same for the totalScore variable, still negative. Could it be that I had initialized the totalScore variable to zero instead of just declaring it as a double?

``````using System;

namespace diver
{
internal class Program
{
private static void Main(string[] args)
{
string Name, city, processAnotherOneYoN;
double Score, highestScore, lowestScore, degreeDiff, totalScore;
int numberOfNames = 0;

#region Outer do while loop
do
{
//Get input from the user
Console.Write("Enter a divers name: ");

Console.Write("Enter a city: ");

Console.Write("Enter a degree of diffculty: ");

//Check the degree of difficulty is within the set range
while (degreeDiff < 1 || degreeDiff > 1.67)
{
Console.Write("Invalid degree of difficulty, please reenter (Valid Range: 1 – 1.67)\n");
Console.Write("Enter dive degree of difficulty: ");
}

highestScore = -1;
lowestScore = 11;
totalScore = 0;

#region for loop
for (int i = 0; i < 5; i++)
{
Console.Write("Enter the score given by judge {0}: ", i+1);

while (Score < 0 || Score > 10)
{
Console.WriteLine("Invalid score - Please reenter (Valid Range: 0 - 10)\n");
Console.Write("Enter the score given by judge #{0}: ", i);
}
if (i == 1 || Score > highestScore)
highestScore = Score;
if (i == 1 || Score < lowestScore)
lowestScore = Score;
totalScore = totalScore + Score;

/*
if (Score > highestScore)
highestScore = Score;
if (Score < lowestScore)
lowestScore = Score;
*/

// subtract the low/high and do the calculation
totalScore = totalScore - lowestScore;
totalScore = totalScore - highestScore;
totalScore = totalScore / (double)3;
totalScore = totalScore * degreeDiff;
}

#endregion
//print the results to the screen
Console.WriteLine();
Console.WriteLine("Name = {0}, Lowest Score  = {1:F}, Highest Score = {2:F}, Final Score = {3:F}", Name, lowestScore, highestScore, totalScore);

numberOfNames = numberOfNames + 1;
//Get feedback from user to continue
Console.WriteLine();
Console.Write("Do you want to process another diver? (Y/N)? ");
Console.WriteLine();
Console.WriteLine();

} while (processAnotherOneYoN == "Y");

#endregion

Console.WriteLine();
Console.WriteLine("Summary - Number of divers entered {0}, ", numberOfNames);
}
}
}
``````
0

LVL 86

Expert Comment

ID: 36555197
As noted before, and just as it is in the examples, you need to move lines #65 thru #9 to OUTSIDE the loop at line #71.
0

LVL 86

Expert Comment

ID: 36555199
#9 should be #69.
0

Author Comment

ID: 36555244
Got it!! You were right, should have seen my mistake immediately as you noted earlier. Thanks for the patience.
0

Author Closing Comment

ID: 36555252
imnorie was extremely helpful and patient. He worked the solution with me until I understood my mistake. His knowledge and experience in this field was representative of an expert. Experts like this is what makes Experts Exchange so successful. Thank you!
0

LVL 86

Expert Comment

ID: 36555267
*sigh* Lol....
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
###### Suggested Courses
Course of the Month9 days, 3 hours left to enroll