Look at this please

This is a discussion on Look at this please within the C Programming forums, part of the General Programming Boards category; This is a very basic program i wrote. At first i had the total=0 and it was giving the incorrect ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Sentinels

    This is a very basic program i wrote. At first i had the total=0 and it was giving the incorrect answer so i changed the total to =1 and now it works fine can some one tell me why the total would be set to 1 at the start of the code. Thnx.

    #include<stdio.h>
    #include<conio.h>

    void main(void)
    {
    int count;
    float salary, total, average;

    count=0;
    total=1;
    salary=0;

    printf("THIS PROGRAM ACCEPTS A NUMBER OF SALARIES");
    printf("\nAND PRINTS THE AVERAGE SALARY TOGETHER");
    printf("\nWITH HOW MANY SALARIES WERE ENTERED\n");
    while( salary != -1)
    {
    printf("\nENTER SALARY:");
    scanf("%f",&salary);
    total=total+salary;
    count = count + 1;
    }
    count = count -1;
    average= total / count;
    printf("\nNUMBER OF SALARIES ENTERED ARE: %d",count);
    printf("\nAVERAGE SALARIES %5.2f",average);
    getch();
    }
    Last edited by Nightstalker8; 03-21-2003 at 06:54 AM.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Few things, about your code:
    >>void main(void)
    main always returns int, so change it to int main() and then returns an value at the end of the program

    >>total=total+salary;
    Well, this is correct but don't work hard, you have the += operator, so total += salary is correct

    >>count = count + 1;
    same here you can an operator called ++, you can do this count++;

    >>count = count -1;
    same here you can use the -= operator, so you can do this count -= 1;

    your program works even if total is set to zero, try it by yourself

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main(void)
    {
    int count;
    float salary, total, average;
    
    count=0;
    total=0;
    salary=0;
    
    printf("THIS PROGRAM ACCEPTS A NUMBER OF SALARIES");
    printf("\nAND PRINTS THE AVERAGE SALARY TOGETHER");
    printf("\nWITH HOW MANY SALARIES WERE ENTERED\n");
    while( salary != -1)
    {
    printf("\nENTER SALARY:");
    scanf("%f",&salary);
    total += salary;
    count++;
    }
    
    count = count -1;
    average= total / count;
    printf("\nNUMBER OF SALARIES ENTERED ARE: %d",count);
    printf("\nAVERAGE SALARIES %5.2f",average);
    getch();
    return 0;
    }
    Also, try don't using conio.h it's not standard.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    3
    Thnx for the help Vber. For some reason when i run it with the total =0, just say i enter 2.00 five times it outputs the average as 1.80

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Ahh this is the problem
    add <math.h> in your document by using #include <math.h>
    and change this line of code:
    Code:
    printf("\nAVERAGE SALARIES %5.2f",ceil(average));

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    3
    Thnx again for the help its running much better.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21