-
help with the basic's
Howdy,
I am just learning the c languge from Sam's Teach yourself C.
I am on to Understanding Arrays, there is a question I had that was not answered in the book.
The question was write a program to display the total memory space taken by the array(I understand this part), what I was wondering if you had a array of floating numbers to print the array out would I use a for loop or a while loop, because I cant figure out how to get it to work with a for loop, If i change i to a double or float it still wont work here is my code, And thank you all for your time and your suggestions.
Code:
#include <stdio.h>
main() {
double list_data[6] = {
1.12345,
2.12345,
3.12345,
4.12345,
5.12345,
6.12345};
int i, size;
size = sizeof(list_data);
printf("The size of list_data is %d\n", size);
for (i=0; i<list_data[i]; i++)
printf(" %.5f\n", list_data[i]);
return (0);
}
-
So the normal structure for a for loop is:
Code:
for(i=0; i<ARRAY_SIZE; ++i)
in your case array size is 6.
Similarly to get the ith element in the array for printing, you use list_data[i]
-
If you know how to use while loop than is simple to use a for loop instead of the while loop. The similarities between this 2 loop are this:
*while loop:
Code:
int contor = 0;
while(cnd) {
/* do smthg */
contor++;
}
*for loop:
Code:
int contor;
for (contor = 0; cnd; contor++) {
/* do smthg */
}
The 2 of them are equivalent.