Why does this code not do what it should

  • Thread starter Thread starter Cinimod
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion revolves around a code snippet that is intended to fill an array with evenly spaced values from 0 to n, but participants are trying to identify why the code does not produce the expected results. The focus is on programming concepts, specifically related to type casting and integer division in C.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes that the code compiles but does not work as intended, suggesting that all elements of the array are zero.
  • Another participant proposes that if both i and n are declared as integers, integer division could lead to unexpected results, and suggests using casting to float for the division.
  • A different participant reiterates the importance of casting, recommending the use of float for both i and n to avoid integer division issues.
  • One participant provides a corrected code example, emphasizing the need for the array to be of type float and discussing the implications of integer division.
  • Concerns are raised about the definitions of i and n, with one participant mentioning that some compilers might cast a float index to an int, which could lead to errors.
  • There is a discussion about the behavior of integer division, with one participant clarifying that the behavior is implementation-defined if the operands are not both positive.
  • Another participant mentions that truncation occurs in integer division, which may not align with the expected arithmetic rules.

Areas of Agreement / Disagreement

Participants generally agree that integer division and type casting are likely sources of the problem, but there is no consensus on the best approach to resolve the issue or on the specifics of how the variables are defined.

Contextual Notes

There are limitations regarding the definitions of variables i and n, and the implications of using integer types versus floating-point types in the context of array indexing and arithmetic operations.

Cinimod
Messages
34
Reaction score
0
I can't see what is wrong with this code, but for some reason it doesn't work. It compiles, just doesn't do what it should.

Code:
for(i=0; i<n; i++)
    {
             delx[i] = i/n;
    }

I have defined delx as an array, with n elements, and just want to fill the array with evenly spaced values from 0 to n. When I try running it, it says that all elements of the array are 0. Any chance someone could explain to me where the problem is.
 
Technology news on Phys.org
If i is declared type int, and n is declared type int, that would explain the behavior.

try this:

delx = float(i)/float(n);

this is called casting, when you force the the type of a variable to change for a particular calculation (I am not sure if float() is the correct syntax for C).

Alternatively I think you can declare n as a float, but I am no C expert. The problem though is that when you divide a small integer by a large one and get an integer result, the result has to be zero.
 
Presumably i and n are integers? Integer division will round down.
Use delx[x] = (float)i/(float)n;
 
The problem seems to be that you're not casting correctly (assuming array type int). The way I would do it is to make my array a float and then do a integer division and perform casting:

Code:
#include <stdio.h>

#define n 10

int main()
{
	float delx[n];
	int i=0;
	
	for(i=0; i<n; i++)
    {
             delx[i] = (float) i/n;
             printf("%f\n", delx[i]);
    }
return 0;
}
mgb_phys said:
Presumably i and n are integers? Integer division will round down.
Well if his array has n elements, then array size cannot be specified by a non-integer type. So I guess integer division is sort of implicit.
 
Last edited:
The code in the op doesn't define i or n, or say if n is the array size;
Some (naughty) compilers will cast a float index to an array to int.
 
mgb_phys said:
Presumably i and n are integers? Integer division will round down.
Only if both operands are positive, otherwise the behaviour is implementation defined.
 
KTC said:
Only if both operands are positive, otherwise the behaviour is implementation defined.
Yes, I mean't that they wouldn't round using the normal arithmetic rules which is what the OP expedcted. The word I was looking for was truncate.
 

Similar threads

  • · Replies 34 ·
2
Replies
34
Views
6K
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
Replies
47
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K