Solution for Exercise 19-11
ex1911
#include <stdio.h>
int main()
{
enum weekdays { mon, tues, wed, thurs, fri };
float temps[5] = { 18.7, 22.8, 25.0, 23.3, 23.2 };
float *t;
t = temps; /* initialize the pointer */
printf("The temperature on Tuesday was %.1f\n",
*(t+tues) );
printf("The temperature on Friday was %.1f\n",
*(t+fri) );
return(0);
}
Output
The temperature on Tuesday was 22.8
The temperature on Friday was 23.2
Notes
* Here is a way to express the solution without using enumerated constants:
#include <stdio.h>
int main()
{
float temps[5] = { 18.7, 22.8, 25.0, 23.3, 23.2 };
float *t;
t = temps; /* initialize the pointer */
printf("The temperature on Tuesday was %.1f\n",
*(t+1) );
printf("The temperature on Friday was %.1f\n",
*(t+4) );
return(0);
}
* Here is a literal solution to the problem:
#include <stdio.h> int main() { float temps[5] = { 58.7, 62.8, 65.0, 63.3, 63.2 }; printf("The temperature on Tuesday will be %.1f\n", *(temps+1)); printf("The temperature on Friday will be %.1f\n", *(temps+4)); return(0); }
* I don't recommend mixing pointer notation with an array variable, as shown above, but it works.
Copyright © 1997-2024 by QPBC.
All rights reserved