Author Topic: A problem I'm having with my C homework.  (Read 814 times)

Hey guys, I have a problem with my homework that I can't solve.

Every time I run the program, b[n] equals 0, yet the numbers are entered correctly.
I don't know why it does it, can anyone help?
Code: [Select]
#include <stdio.h>
void main ()
{
int v,n,a[]={20,40,30,50,40,60};
float i,r,b[6];
i=r=0;
v=10;
for(n=0;n<6;n++)
{
b[n]=(10/a[n]);
printf("I%d = %fA\n",n,b[n]);
}
for(n=0;n<6;n++)
{
i=b[n]+i;
}
r=10/i;
printf("Rt= %f\n It= %f\n",r,i);
}
Edit: I tried adding the math.h library; no luck.
« Last Edit: December 21, 2009, 10:01:33 AM by Maor »

Try casting a as a float.

b[n]=(10/(float)a[n]);


Also, use more meaningful variable names.

Try casting a as a float.

b[n]=(10/(float)a[n]);


Also, use more meaningful variable names.
what does casting mean?


and the legend:

n= the current number (for the "for" loop)
a= the resistors
b= the current of the resistors
r = Resistor total
i = Current total

Casting is forcing a variable to be a specific variable type. Such as float/boolean/integer/rape/array etc.

Casting is forcing a variable to be a specific variable type. Such as float/boolean/integer/rape/array etc.
I'll try that, thanks.

Edit:

Hey it worked!, thanks alot skele and Ephi.

But I don't understand, why does it need to be forced if the output if already pre-defined as a float?
« Last Edit: December 21, 2009, 10:42:57 AM by Maor »

I wish I had a C class :(.

Well, I actually program my calculator in most classes I take.

Bump, can someone answer my question?

Because "a" is defined as an array, and the items within it would all default to the integer type so any divisions on that value would be turned into an integer (rounded down in your case). Setting it to a float tells the program it needs to store the remainder of the division as well.

You might also be able to define the array like so:

int v,n,a[]={20.0,40.0,30.0,50.0,40.0,60.0};

Which might force them to use the float type by default - But I'm not sure about that.

Because "a" is defined as an array, and the items within it would all default to the integer type so any divisions on that value would be turned into an integer (rounded down in your case). Setting it to a float tells the program it needs to store the remainder of the division as well.

You might also be able to define the array like so:

int v,n,a[]={20.0,40.0,30.0,50.0,40.0,60.0};

Which might force them to use the float type by default - But I'm not sure about that.
Hm...okay, Thanks.

Just to clarify, the type of the expression "(10/a[n])" is decided based on the types of the arguments within it. 10 is an integer and a[n] was an integer so the expression would be an integer (which can't hold floating point values) so by setting a[n] to a float, the resulting expression is also a float and therefore the result is able to represent a floating point value.

Just to clarify, the type of the expression "(10/a[n])" is decided based on the types of the arguments within it. 10 is an integer and a[n] was an integer so the expression would be an integer (which can't hold floating point values) so by setting a[n] to a float, the resulting expression is also a float and therefore the result is able to represent a floating point value.
So thats why I kept getting that b[n] equaled 0.

Right, because 10/20 = 0.5 and an integer can't hold the .5 bit so it floors the value to 0.

Right, because 10/20 = 0.5 and an integer can't hold the .5 bit so it floors the value to 0.
Got it, thanks; locking.