Solution for Exercise 12-6
ex1206
#include <stdio.h> int main() { float marketclose[] = { 24164.95, 24107.08, 24643.63, 24400.93, 23728.53 }; int day; puts("Stock Market Close"); for(day=0;day<5;day++) printf("Day %d: %.2f\n",day+1,marketclose[day]); return(0); }
Output
Stock Market Close
Day 1: 24164.95
Day 2: 24107.08
Day 3: 24643.63
Day 4: 24400.93
Day 5: 23728.53
Notes
* Another popular way to initialize an array is to place its elements on lines by themselves. For example:
* The final curly bracket can go on a line by itself if you like, just remember not to place a comma after the final value in the array or the compiler thinks you goofed up and missed a value:
* It's also common to organize array elements into rows and columns when they're initialized:
* I hope you remembered to set marketclose[]
as an array of float values.
* In Line 10 (the printf() statement), you see the day+1
construction, which outputs the looping value using the human method of starting at 1 instead of zero.
Copyright © 1997-2024 by QPBC.
All rights reserved