Early C programming... I'm creating an array with exactly 5 elements. I am trying to sum the elements, print the array, and find the mean. I am so completely lost. Any guidance would be helpful! Thank you- Bunko
Printable View
Early C programming... I'm creating an array with exactly 5 elements. I am trying to sum the elements, print the array, and find the mean. I am so completely lost. Any guidance would be helpful! Thank you- Bunko
Well, there are a few good tutorials on this website.
To get you started, printing all the elements of an array
Note: You can either define the size of the array (ie, #define ARRAY_SIZE 5) or compute it at compile time. Look up the sizeof operator if you're unsure what it does or how it works. Basically in this case, we divide the total space occupied by the array by how much each element takes up -- which gives us 5 in this case.Code:size_t i = 0;
int array[5] = {1, 2, 3, 4, 5};
size_t noElements = sizeof(array) / sizeof(array[0]); /* See note below */
for(i = 0; i < noElements; i++)
{
printf("%d\n", array[i]);
}
I'd recommend reading the tutorials or a good book though.