How to Determine nad print the maximum number when 10 numbers are inpu

Determine and print the maximum number when 10 numbers are inputted

Page 1 of 1

4 Replies - 5227 Views - Last Post: 21 January 2007 - 10:57 AM Rate Topic: -----

#1 oflah171  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-January 07

How to Determine nad print the maximum number when 10 numbers are inpu

Posted 21 January 2007 - 06:50 AM

Hi all

I am just starting to learn C at the moment nad do not have currently have access to a compiler or the software to test it. I need to write a program that inputs a series of ten numbers and determines and prints the largest of these numbers. Below is my attempt but I am unsure it is correct. Can someone please advise. Thanks in advance.
Aoife

int main (void)
{
int counter = 1;
int largest = 0;
int number;

while (counter is <=10 ) {
   printf( ''Enter number: '' );
   scanf( ''%d'' , &number);
   largest = number /* store number in largest)
  ++counter
  } /*end while*/

 if (number > largest ) {
 largest = number;
 printf( ''largest number is %d\n'', largest );
 } /*end if*/

 return 0;

 }

  if (number > largest) {


Is This A Good Question/Topic? 0
  • +

Replies To: How to Determine nad print the maximum number when 10 numbers are inpu

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: How to Determine nad print the maximum number when 10 numbers are inpu

Posted 21 January 2007 - 08:08 AM

Your current program will only store the final number entered by the user. You have the correct idea that you must check if the number entered is larger that the largest number stored, but you'll have to do that in your loop, or else store all entered values in an array and compare them later. You also seem to have an if statement outside of your main function. This will cause problems.

Your loop can run like this:

While entered values are less than 10
User enters a number
If the number entered is larger than the one in storage, replace the number in storage with the entered value.
End Loop
Was This Post Helpful? 0
  • +
  • -

#3 oflah171  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-January 07

Re: How to Determine nad print the maximum number when 10 numbers are inpu

Posted 21 January 2007 - 09:51 AM

Thank you so much for your reply, I really appreciate it
Is this right?

while (counter is <=10 ) {
printf( ''Enter number: '' );
scanf( ''%d'' , &number);
largest = number /* store number in largest)
++counter
} /*end while*/

if (number > largest ) {
replace largest with number;
printf( ''largest number is %d\n'', largest );
} /*end if*/

return 0;

Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: How to Determine nad print the maximum number when 10 numbers are inpu

Posted 21 January 2007 - 10:01 AM

Again, this will result in only the final number entered by the user being stored. You are checking the to see if the value entered is larger than the value stored, but you are only doing so once...you need to do so each time a number is entered. The user is entering numbers in your while loop. That is where the check should be taking place as well. Try something like the following:
while (counter <=10 ) {
printf( "Enter number: ");
scanf( "%d": , &number);
if(number>largest)
{
   largest = number;
}
++counter;
} /*end while*/
printf( "largest number is %d\n", largest );


Was This Post Helpful? 0
  • +
  • -

#5 oflah171  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-January 07

Re: How to Determine nad print the maximum number when 10 numbers are inpu

Posted 21 January 2007 - 10:57 AM

Thanks a lot, makes more sence now!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1