Why does this code not do what it should

  • Thread starter Cinimod
  • Start date
  • Tags
    Code
In summary: But you are correct, I wasn't specific enough.In summary, the conversation discusses an issue with a code where the array elements are not being filled with evenly spaced values. The problem is identified as integer division and the solution is suggested to use casting or to declare the array as a float. Some compilers may implicitly cast the index of an array to an integer, causing the issue.
  • #1
Cinimod
34
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
  • #2
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.
 
  • #3
Presumably i and n are integers? Integer division will round down.
Use delx[x] = (float)i/(float)n;
 
  • #4
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:
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 

Related to Why does this code not do what it should

1. Why is my code not producing the expected output?

There could be several reasons for this issue. It could be due to a logical error in the code, incorrect syntax, or missing/incorrect input data. It is important to carefully review the code and check for any errors or bugs that could be causing the unexpected output.

2. How can I debug my code to find the issue?

One way to debug your code is by using print statements to track the flow of the program and see the values of variables at different points. You can also use a debugger tool to step through the code line by line and identify any errors or issues.

3. Could the issue be caused by an external factor?

Yes, sometimes the code may not work as expected due to external factors such as changes in the environment, incorrect dependencies, or issues with the system. It is important to check for these factors and make any necessary adjustments.

4. How can I prevent this issue from happening in the future?

To prevent similar issues in the future, it is important to follow best practices while coding, such as writing clean and organized code, testing the code thoroughly, and using version control. It is also helpful to document any changes made to the code for future reference.

5. Can I get help from others to troubleshoot this issue?

Yes, seeking help from others, such as colleagues or online communities, can be beneficial in troubleshooting code issues. They may be able to provide valuable insights and help identify the issue faster.

Similar threads

  • Programming and Computer Science
Replies
34
Views
3K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
985
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
11
Views
837
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
Back
Top