Can you please tell me why when the program runs, it won't print the values for 'i' starting from 0?
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;clib.h%26gt;
#include %26lt;math.h%26gt;
int main(void)
{
float *ipArray = NULL;
int i;
ipArray = (float *)calloc(360, sizeof(float));
if (ipArray == NULL) {
printf("\nUnable to allocate Memory...");
printf("\n\t\t--Program Terminating --");
exit(1);
}
for (i = 0; i %26lt; 360; i++) {
ipArray[i] = sin(i);
}
for (i = 0; i %26lt; 360; i++) {
printf("\n\t the value %d is\t%6.3f", i, ipArray[i]);
}
free(ipArray);
return 0;
}
C programming - I have written this Code to do with allocating Memory...?
Actually it does work just fine (I had to remove clib.h)
Perhaps you need to pipe the output through 'more' or 'less' to see entire print-out.
Try this (Windows):
program.exe | more
or this (Unix):
./program | more
also, sin() function takes arguments in radians, not in degrees - you need to convert i to radians (i/57.2957795).
-- added --
Plato3 seems to be an editor integrated with external compilers. I am almost sure it just truncates the program's output (e.g. to the last 300 lines, which explains 61 as starting point).
There's nothing wrong with your program. Try to run it from bare command line, not from that environment.
Reply:calloc is Copy ALLOCate, don't use that one, use malloc (Memory ALLOCate) calloc is used if, for example, you are using an array, and you allocated x elements, and now you need x + 1. You would calloc the array to a larger size, which re-allocates the memory and copies the initial array.
Reply:Theres little wrong with this with a couple of possible exceptions... sin uses a double as the arg, main should not be void, int should be int main(int argc, char *argv[]) and perhaps calloc should be replaced with malloc(360 * sizeof(float)), thereby disposing of the need to include clib.h
Question is what compiler are you using %26amp; what messages are passed out ?
Change the line ipArray[i] = sin(i)to ipArray[i] = i and work from there.
and no... printf prints to stdout so it should be fine.
I am assuming here that you are compiling it as a terminal program, not a GUI program.
Reply:Maybe I don't understand the question, but the program seems to run just fine. I had to remove the #include of clib.h on my Linux box, but after doing so, the program compiled and ran fine, printing 0-359 and the sin(x) values for each.
$ gcc -o prog -lm prog.c
$ prog
the value 0 is 0.000
the value 1 is 0.841
the value 2 is 0.909
the value 3 is 0.141
...
the value 356 is -0.841
the value 357 is -0.909
the value 358 is -0.141
the value 359 is 0.757$
Now, sin() is supposed to take and return a "double". Your program doesn't do any explicit casting of the integer i to a double, but the compiler likely does that on your behalf. However, sin()'s input is in radians, not degrees (as your loop from 0 to 359 seems to indicate that your might think). Is that the source of your confusion?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment