counter-controlled repetition - help - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

counter-controlled repetition - help

raeikoraeiko Posts: 17Member
Hello everybody,
i need your help with an exercise from Dietel & Dietel book "C How to program.

The text of the exercise states:

The process for finding the largest number (i.e. the maximum of a group of numbers) is used frequently in computer applications. For example, a program that determines the winner of a sales contest would input the number of units sold by each person. The salesperson who sold the most units wins the contest. Write a pseudocode program and then a program that inputs a series of 10 numbers and determines and prints the largest of the numbers

[Hint: Your program should use three variables as follows:
Counter: A counter to count to 10 (i.e. to keep track of how many numbers have been input and to determine when all 10 numbers have been processed
Number: The current number input to the program
Largest : The largest number so far]

Here is what i have done so far

[code]#include

int main( void ) /* function main begins program execution */
{
int counter, number, largest; /* variables */

/* initialization phase */
counter = 1; /* initialize loop counter */

/* processing phase */
printf( "Enter first number:" ); /* prompt for imput */
scanf_s( "%d", &number ); /* read number from user */

largest = number; /* store number value to largest */

/* process ten numbers using counter-controlled loop */
while ( counter < 10 ) /* loop 10 times */
{
printf( "Enter the next number:" ); /* prompt user to imput next number */
scanf_s( "%d", &number ); /* read number from user */

if ( number > largest ) /* test condition */
largest == number; /* assign value to largest */

counter = counter + 1; /* increment counter */
} /* end of while */

printf( "The largest number is:", number ); /* displays largest number */

return 0; /* indicate that program ended succesfully */

} /* end of function main */[/code]


Could you please help me to understand what i'm doing wrong? I'm going crazy...

Thanks a lot,

raeiko

Comments

  • VinayKhareVinayKhare Posts: 69Member
    [code]#include

    int main( void ) /* function main begins program execution */
    {
    int counter, number, largest; /* variables */

    /* initialization phase */
    counter = 1; /* initialize loop counter */

    /* processing phase */
    printf( "Enter first number:" ); /* prompt for imput */
    scanf_s( "%d", &number ); /* read number from user */

    largest = number; /* store number value to largest */

    /* process ten numbers using counter-controlled loop */
    while ( counter < 10 ) /* loop 10 times */
    {
    printf( "Enter the next number:" ); /* prompt user to imput next number */
    scanf_s( "%d", &number ); /* read number from user */

    if ( number > largest ) /* test condition */
    largest [color=Red]==[/color] number; /* assign value to largest */

    counter = counter + 1; /* increment counter */
    } /* end of while */

    printf( "The largest number is:", number ); /* displays largest number */

    return 0; /* indicate that program ended succesfully */

    } /* end of function main */[/code]

    red marked portion might be the error...according to me.

    you are using '==' for assigning value to the largest.
    you should use '='.

    and if some more problem then specify the behviour of your program.

  • mybpmybp Posts: 1Member
    largest == number; /* assign value to largest */

    This is wrong, the variable NUMBER should be store to LARGEST but
    in code these assign together.
    correct:
    largest = number; /* store value to largest */
Sign In or Register to comment.