I have two questions.
How do you make a program that takes only an array of integers and evaluates each value without including array-length as an input?
How can you make that code output four different integers into four different variables?
Below I've included the code my instructor provided:
1 2 3 4 5 6 7 8 9 10 11
int main(void) {
float* t;
int n;
// array t of length n created and filled with temperatures from keyboard
int nC, nM, nW, nH;
// Replace this single line with a call to the tempdist function
cout << "There are " << nC << " cold days in the list" << endl;
cout << "There are " << nM << " mild days in the list" << endl;
// etc...
return 0;
}
Not all C-Style arrays are null-terminated, as this output shows:
enter array size:
10
enter temp 1 of 10
11
enter temp 2 of 10
12
enter temp 3 of 10
13
enter temp 4 of 10
14
enter temp 5 of 10
15
enter temp 6 of 10
16
enter temp 7 of 10
17
enter temp 8 of 10
18
enter temp 9 of 10
19
enter temp 10 of 10
20
11 12 13 14 15 16 17 18 19 20 3502.9 2.52902e-029 4.62395e-039
Some C-style arrays, usually character arrays, terminate with a null-character, '\0', if it’s added by the user or by some operation. For example, std::getline() adds a null-character at the end of the created string.
An other example is the assignment of a constant string to a char array or a constant char pointer: char mystring[] = "I'll be null-terminated";
However the dimension of an array in bytes is known:
1 2
int temperatures[] = {1, 2, 3, 4};
std::cout << "Size of 'temperatures': " << sizeof temperatures << '\n';
Output:
Size of 'temperatures': 16
To discover the number of ‘rows’ of an array, you just need to divide the whole dimension by the dimension of a single ‘row’:
1 2 3 4
int temperatures[] = {1, 2, 3, 4};
std::cout << "Size of 'temperatures': " << sizeof temperatures << '\n';
std::cout << "Number of 'rows' in 'temperatures': "
<< (sizeof temperatures) / (sizeof (int)) << '\n';
Output:
Size of 'temperatures': 16
Number of 'rows' in 'temperatures': 4
Enoizat: thanks, I seem to have made a dog's breakfast of this one. However note that the OP array is depicted as float* and sizeof the float* variable with 4 entries (or indeed any number of entires), as in your example, will still be 4 not 16. So I'm not sure if the following:
evaluates each value without including array-length as an input
… can even be done as without the array-length input there doesn't seem to a way to 'unpack' the array
@gunnerfunner, I’m afraid I must ask you to bear with my poor English: I’m not sure I understand correctly purplepain57’s assignment.
Doesn’t it say “...an array of integers...”? I can’t understand what that float pointer is there for.
What’s more, what does it mean: “How can you make that code output four different integers **into** four different variables?”
Output into variables?
Let alone int main(void) ... Have you ever used a void specification in the list of parameters instead of a couple of empty parenthesis?
You are right, of course, the size of a pointer is the size of an int, no matter what it points to. The only alternative I can see to find the ‘end’ of a pointer is to use such a lower value for the temperature that it can’t be found in nature, let’s say -1000.
The fact is I’m so unsure of what the exercise asks that I feel I can’t be of any help for it.