-4

My C program does not show the output.

My code in the C language:

int x = 0; // Installment
int y = 0  // Balance

for (i=1; i<=installment;i++)
        {
            printf("%d %d %d\n", i, x=totalFee/installment, y = totalFee-(totalFee/instalment));
        }

Correct output:

Total fees: 300
Month  Installment  Balance
  1      100          200
  2      100          100
  3      100            0

My output:

Total fees: 300
Month  Installment  Balance
  1      100          200
  2      100          200
  3      100          200

This is just part of the code. Because this is the part where I have an issue. Other parts are fine.

Addison
  • 1
  • 1
  • 2
    If you program in C, why did you add the C++ tag? – Some programmer dude Nov 07 '16 at 07:36
  • @Someprogrammerdude Boy are we in sync. Just edited it too. – Right leg Nov 07 '16 at 07:36
  • 1
    Maybe you should deduct from the current balance... – Klas Lindbäck Nov 07 '16 at 07:38
  • 6
    It is weird to expect different outputs with a loop that print always same values.... I mean `totalFee`, `installment` and `x` are constants inside the loop so.... – LPs Nov 07 '16 at 07:38
  • 4
    And fyi, you can *not* assume the evaluation+assignment for `x` happens before `y` in your function call. [Order of parameter eval is not guaranteed](https://stackoverflow.com/questions/21600108/c-function-parameters-order-of-evaluation). – WhozCraig Nov 07 '16 at 07:39
  • 1
    `int y = 0` does not terminate with a semicolon. `installment` is not defined. `totalFee` is not defined. – siavashk Nov 07 '16 at 07:41
  • If I change the for loop into the below: for (i=1; i<=installment;i++) { printf("%d %d %d\n", i, x=totalFee/installment, y = totalFee-(totalFee/installment,)); } – Addison Nov 07 '16 at 07:41
  • With the new edit, neither the question nor any of the posted answers make any sense. Voting to close this as unclear. – Lundin Nov 07 '16 at 07:46

4 Answers4

2

Try this:

for (i=1; i<=installment;i++)
{
    x = totalFee/installment;
    y = totalFee-x;
    printf("%d %d %d\n", i, x, y);
}

In C/C++, the compiler decides in which order it evaluates the parameters when a function is called. There is absolutely no guarantee that the order will be from first to last parameter. So most likely it evaluated totalFee-x before x = totalFee/installment which is not what you expected.

See Compilers and argument order of evaluation in C++ or Order of evaluation in C++ function parameters or even function parameter evaluation order. Specially, check this answer.

Now you updated your post and replaced by y = totalFee-x by y = totalFee-(totalFee/instalment). This last one should work as y assignment does not rely on x. If it does not work for you, it's just that you are doing your operations the wrong way. Use a debugger to see what's going on.

Community
  • 1
  • 1
jpo38
  • 20,821
  • 10
  • 70
  • 151
1

The problem is likely related to order of evaluation of function parameters. You cannot know or assume that x=totalFee/installment is executed before y = totalFee-x. Also, it is generally bad practice to use assignment inside expressions.

Try to change your loop body to:

x = totalFee/installment;
y = totalFee-x;
printf("%d %d %d\n", i, x, y);
Lundin
  • 195,001
  • 40
  • 254
  • 396
0

maybe you don`t understand your loop

let`s step it.

First, i = 1, x = 300/3 = 100, y = 300 - 100 = 200

Second, i = 2, x= 300/3 = 100, y = 300 - 100 = 200

third, i = 2, x= 300/3 = 100, y = 300 - 100 = 200

the x you make it 100

Xiangxuan Qu
  • 153
  • 1
  • 12
0

The program for your required output

for (i=1; i<=installment;i++)
{
    x = totalFee/installment;
    y = totalFee-(x * i);
    printf("%d %d %d\n", i, x, y);
}
Lineesh K Mohan
  • 1,702
  • 14
  • 18