Problem 6 How to round correctly

Back to Problem Solutions forum

SergioAlon     2020-03-30 11:06:43

Hello i am trying to solve the problem but there are some cases where the program works and others dont. Can someone help me??? Some of the numbers round and others dont

#include <stdio.h>

int main(void)
{
    int size;
    int i = 0;
    float f = 0.0;
    int pro = 0;
    scanf("%d", &size);

    int res[size];
    int arr1[size];
    int arr2[size];


    for (i = 0; i < size; i++)
    {
        scanf("%d ", &arr1[i]);
        scanf("%d ", &arr2[i]);
    }

    for (i = 0; i < size; i++)
    {
        f = arr1[i] / arr2[i];
        pro = f;
        f - pro;
        f * 10;

        if (f >= 5)
        {
            f + 1;
        }

        res[i] = f;

        printf("\n %d \n", res[i]);
    }


    return 0;
}
Rodion (admin)     2020-03-31 04:15:21
User avatar

Hi Friend!

Just check your code carefully. You wanted to do right thing but your code seems to have mistakes:

    f = arr1[i] / arr2[i];
    pro = f;
    f - pro;
    f * 10;

    if (f >= 5)
    {
        f + 1;
    }

In this part:

  • f - pro does nothing (you probably forgot assignment)
  • even if you put assignment here, it will give 0 because f equals to pro after line above;

Try debugging the code by including printfs for various variables, e.g.

printf("F=\%d, PRO=%d, F-PRO=%d\n", f, pro, f-pro);
Please login and solve 5 problems to be able to post at forum