1

The program is about getting the average of all laboratory exercise. I want to round off my decimals 7.7778 to the whole number. I tried the ceilf and roundf() its not working, is there any problem with my codes? any help will be appreciate thank you!

My codes

#include<stdio.h>
#include<conio.h>
#include <math.h>
int main()
{
    int numberof_laboratory,lab_exercises,total_numberof_laboratory,average,roundoff;
    char answer;
    do
    {
        printf("\nEnter number of laboratory:");
        scanf("%d",&numberof_laboratory);
        for(int i=1;i<=numberof_laboratory;++i)
        {
            printf("\nEnter laboratory exercise %d:",i);
            scanf("%d",&lab_exercises);
            total_numberof_laboratory += lab_exercises;
            average = total_numberof_laboratory / 3;
            roundoff = ceilf(average * 100) / 100;
        }
        printf("The average laboratory exercise grade is %d",roundoff);
        printf("\nDo you want to continue(Y/N)?");
        scanf(" %c", &answer);
    }
    while(answer != 'N' && answer != 'n');
    printf("Goodbye!");
}

Example

Enter number of laboratory:3

Lab exercise no 1: 10

Lab exercise no 2: 7

Lab exercise no 1: 6

The average laboratory exercise grade is: 7 // it should be 8

Harald
  • 3,110
  • 1
  • 24
  • 35
Holow
  • 31
  • 1
  • 7

2 Answers2

3

Your roundoff = ceilf(average * 100) / 100; is wrong

average is declared as int.

You should declare it as follows:

float average;
...
average = total_numberof_laboratory / 3.0f; // thanks to Ed Heal
roundoff = roundf(average);

It seems you want the following:

#include <stdio.h>
#include <math.h>
int main()
{
    int numberof_laboratory, lab_exercises, total_numberof_laboratory, roundoff;
    float average; /* see here */
    char answer;
    do
    {
        printf("\nEnter number of laboratory:");
        scanf("%d",&numberof_laboratory);
        total_numberof_laboratory = 0; /* see here important */
        for(int i=1;i<=numberof_laboratory;++i)
        {
            printf("\nEnter laboratory exercise %d:",i);
            scanf("%d",&lab_exercises);
            total_numberof_laboratory += lab_exercises;
        }
        average = total_numberof_laboratory / (float)numberof_laboratory; /* see here */
        roundoff = roundf(average); /* see here */
        printf("The average laboratory exercise grade is %d",roundoff);
        printf("\nDo you want to continue(Y/N)?");
        scanf(" %c", &answer);
    }
    while(answer != 'N' && answer != 'n');
    printf("Goodbye!");
}

Compile in gcc as

gcc -o main main.c -lm  -Wall -pedantic
1

You should use the following code to find the required average-

#include<stdio.h>
#include<conio.h>
#include <math.h>
int main()
{
int i,numberof_laboratory, lab_exercises, total_numberof_laboratory=0,roundoff;
float average;
char answer;
do
{
    printf("\nEnter number of laboratory:");
    scanf("%d",&numberof_laboratory);
    for(i=1;i<=numberof_laboratory;++i)
    {
        printf("\nEnter laboratory exercise %d:",i);
        scanf("%d",&lab_exercises);
        total_numberof_laboratory += lab_exercises;
    }
    average = (float)total_numberof_laboratory /numberof_laboratory ;
    roundoff = ceil(average);
    printf("The average laboratory exercise grade is %d",roundoff);
    printf("\nDo you want to continue(Y/N)?");
    scanf(" %c", &answer);
}
while(answer != 'N' && answer != 'n');
printf("Goodbye!");
}

You should go through it clearly and notice the changes made.

  1. If the total_numberof_laboratory counts the total sum of lab_exercises then it must be initialized with a 0 value
  2. average should be computed after the sum has been evaluated
  3. average should be declared float
  4. The average is calculated by average = (float)total_numberof_laboratory /numberof_laboratory ; and average = (float)total_numberof_laboratory / 3 ; won't make it
  5. Lastly only ceil is sufficient no need for ceilf

Happy to help ;-]