Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help with this problem, in Visual C# How to Program by Paul and Harvey Deitel.

(Find the largest number) The process of finding the maximum value is used frequently in computer applications. For example, an app that determines the winner of a sales contest would input the number of units sold by each salesperson. The salesperson who sells the most units wins the contest. Write pseudocode, then a C# app that inputs a series of 10 integers, then determines and displays the largest integer. Your app should use at least the following three variables: counter, number and largest.

Here's what I've got so far
C#
int highestnumber;
int number;
int counter;


Console.WriteLine("Enter number of sales: ");
number = Convert.ToInt32(Console.ReadLine());
highestnumber = number;
counter = 1;
while (counter < 10)
{
    counter = counter + 1;

    if (number > highestnumber)
        number = highestnumber;

    Console.ReadLine();

}

Console.WriteLine("The highest number of sales is: {0}", number);
Console.ReadLine();

My teacher has shown how to do the loop stuff, but the largest number thing I cannot understand. Every time it executes the "highest number of sales is..." it outputs the first value that I entered.
Posted
Updated 25-Oct-15 12:29pm
v3

Firstly I think your choice of using a while loop is wrong. I would use a for loop because you know up front how many iterations you need. I would use a while loop if the number of iterations depended on the user input - for example if you prompted "Type X to stop the input"

Most importantly you are only capturing the input of the first number ... you have a
Console.ReadLine();
but you are not assigning the number to anything. number is only ever set on the first input
C#
number = Convert.ToInt32(Console.ReadLine());
it needs to be captured within the loop on each iteration.

I really hate Convert.ToInt32, especially if it is used with user-input. int.TryParse (and similar functions) is far better (less chance to have an exception thrown)

For example:

C#
static void Main(string[] args)
{
    int highestnumber = 0;

    const int NoOfSalesToCompare = 10;

    for (int counter = 1; counter <= NoOfSalesToCompare; counter++)
    {
        int number;
        Console.WriteLine("Enter number of sales for salesperson {0}:", counter);
        if(int.TryParse(Console.ReadLine(), out number))
            if(number > highestnumber) highestnumber = number;
    }

    Console.WriteLine("The highest number of sales is: {0}", highestnumber);
    Console.ReadLine();

}


Lastly, if you learn to use the debugger you would have probably spotted this for yourself. Stepping through lines of code that don't work is an excellent way of improving your knowledge. Actually, stepping through lines of code that *do* work that you've found somewhere is also a good way of improving your knowledge.

I strongly recommend that you step through the code that I've given you and thoroughly understand why it works, but yours didn't. Don't forget ... your teacher probably uses this site too.
 
Share this answer
 
Comments
Dragomundo 25-Oct-15 23:08pm    
Oh crap duh, I made it work by putting the number = Convert.ToInt inside the loop and setting highestnumber and number = 0 when I declare the variable, took out the Console.ReadLine..AND did what the poster below you said so here's the working finished product:
int highestnumber = 0;
int number = 0;
int counter;

counter = 1;
while (counter <= 10)
{
Console.Write("Enter sales amount: ");
if (number > highestnumber)
highestnumber = number;
number = Convert.ToInt32(Console.ReadLine());
counter = counter + 1;
}

Console.WriteLine("The highest number of sales is: {0}", highestnumber);
Console.ReadLine();

I also copy and pasted your code and did the debugger thing you were talking about and learned a little bit from your example. Too bad that the teacher wanted us to use our current knowledge in programming to do the assignment, otherwise I would have used your code, that is until I read your warning about the teacher probably using this site.

Thanks a bunch!
George Jonsson 26-Oct-15 2:20am    
Good teaching.
+5
Well the problem in your code is:
C#
if (number > highestnumber)
       number = highestnumber;


this bit.

You need to write this:

C#
if (number > highestnumber)
       highestnumber= number;


translated into english:

If current number that I'm processing is bigger than highestNumber until now than set higestNumber to this currentnumber.

Example:
First loop:
number = 3
hisghest - 0;
result: highest is 3

Second loop:
number = 1
highest = 3
result: highest = 3

thisrd loop
number = 5
highest = 3
result: highest = 5

etc.....
 
Share this answer
 
Comments
CHill60 25-Oct-15 19:21pm    
I'll confess that I hadn't noticed that particular problem - however the main problem is that the OP only ever reads one number from the input stream.
But even if the OP was reading numbers in the loop that line of code would mess things up royally. Good spot!
Dragomundo 25-Oct-15 23:14pm    
I used your advice along with the advice the poster above gave me to fix my homework. Thanks a lot!
There is a few bugs in your code, see comments for corrections
C++
int highestnumber;
int number;
int counter;

Console.WriteLine("Enter number of sales: ");
number = Convert.ToInt32(Console.ReadLine());
highestnumber = number;
counter = 1;
while (counter < 10)
{
    counter = counter + 1;
 
    if (number > highestnumber)
        //  number = highestnumber;
        // you did it the wrong way
        highestnumber = number;
 
    //  Console.ReadLine();
    // you forgot to store the new reading in the variable
    number = Convert.ToInt32(Console.ReadLine());
 
}
 
//  Console.WriteLine("The highest number of sales is: {0}", number);
// you look for highest number
Console.WriteLine("The highest number of sales is: {0}", highestnumber);
Console.ReadLine();


To learn what is doing your code, I recommend to use the debugger, it permit you to see your code executing step by step and inspect variables as your code execute. It is very educative.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900