Maximum daily temperature for reading (non-empty) sequences. Your program should first request the number of temperatures to read and use malloc() to dynamically allocate an array just big enough to hold the number of temperatures you read. You should then loop in the elements of the array. You should then print out the array elements in reverse order (from the most recent to the oldest) to decimal point 1. Print a newline character at the end of the output. I ran into a problem while I was testing, and here is my test case
Please enter the number of times you want to enter the temperature:
3
Please enter the temperatures:
1.5
2.6
3.7
0.0
3.7
2.6
1.5
Here is my code.
#include <stdio.h>
#include<stdlib.h> // for malloc
int main(void) {
int counts = 0; // set a variable named counts recored how many of the times
printf("Please enter the number of times you want to enter the temperature: \n");
scanf("%d",&counts);
float *temperature; // create an integral pointer
temperature = malloc(sizeof(float)*(counts 1)); // dynamic array here
printf("Please enter the temperatures: \n");
for(int i = 0; i < counts; i ) { // use for loop to read temperature
scanf("%f",&temperature[i]); // record the temperature
}
temperature[counts]= 0;
for (int j = counts; j>-1; j--) { // reverse print
printf("%.1f\n",temperature[j]);
}
free(temperature); // delete array
return 0;
}
I checked the malloc() on c reference, I notice they end with '\0' at the end, but should we use a similar approach for integer arrays, and how can I modify my code to eliminate 0.0 in the output.
Thank you all.
CodePudding user response:
You don't have to malloc count 1
elements. That will malloc memory for 4 elements when you only need 3.
And your problem is you are starting the output for
with the element count
.
If you enter 3 numbers, the last one will be temperatures[2]
, not temperatures[3]
. So, you should initialize j
with count-1
.
CodePudding user response:
I checked the malloc() on c reference, I notice they end with '\0' at the end, but should we use a similar approach for integer arrays [...]
C arrays do not carry information on their length, so you must keep track of that yourself. There are basically two ways of doing this:
- Keep track of the length in an additional variable. This is the most common approach. The array and the length belong together, so when you pass the array to a function, you should pass the length, too.
- Put a sentinel value – a special value that does not occur in the data itself – after the actual data. C strings do that by putting a null character
'\0'
after the actual string data. (That's what you have seen on the manual page.) The advantage is that the array is now self.contained and does not rely on extra data, but the big drawback is that you'll have to walk the whole array if you want to find out the length.
You already have asked the user for counts
, so use that.
[...] how can I modify my code to eliminate 0.0 in the output
If you array has the length n
, the valid indices are 0
through n - 1
. A typical forward loop through the array is
for (i = 0; i < n; i ) ...
The corresponding backwards loop that visits the same indices in reverse is
for (i = n; i-- > 0; ) ...
The update clause is empty, because you decrease the index before entering the body.
That may look awkward, but the convention in C is that a range is described by an inclusive lower bound and an exclusive upper bound: [lo, hi)
or [0, n)
, for example. When going forwards, you exclude the upper bound by enforcing i < n
. When going backwards, you test on i 1
and then decrease the index ´i`, so that it is in the valid range inside the loop.
This method has the advantage that it works with unsigned integers, which is a common way to represent counts and indices.