I'm teaching myself C with this book: Amazon.com: C Programming: A Modern Approach, 2nd Edition (8601300250168): K. N. King: Books
I have been going through examples providing by the author, and adding my own notes in Visual Studio.
I get the "big picture" of the loops contained within the program, but some of the details are lost on me.
I was hoping you could enlighten me on the role of the statement that is circled in red below in the pictures? What is it doing?
Within the context of that first loop's brackets, the statement in red seems unnecessary. But when I delete it, I get a program that doesn't function as intended. So it must be tied to one of the two loops below it. I'm scratching my head what it actually does.
Any help would be much appreciated!
Hobbit, are you around?
I included the text from the book, some screenshots from Visual Studio containing my notes, and the source code below
The author alludes to the purpose in orange below but he isn't 100% explicit what is going on. It's strange because the statement in red deals with an array containing the Initial Balance, but in the loop it is contained in deals with the interest rate labels on the top row of the print
"The second row is a little trickier, since its values depend on the numbers in the first row. Our solution is to store the first row in an array as it's computed, then use the values in the array to compute the second row."
-----------------------------------------------------------------------------
Computing Interest
Our program prints a table showing the value of $100 invested at different rates of interest over a period of years. The user will enter an interest rate and the number of years the money will be invested. The table will show the value of the money at one-year intervals---at that interest rate and the next four higher rates---assuming that interest is compounded once a year. Here's what a session with the program looks like:
Enter interest rate: 6
Enter number of years: 5
Clearly, we can use a For Statement to print the first row.
The second row is a little trickier, since its values depend on the numbers in the first row. Our solution is to store the first row in an array as it's computed, then use the values in the array to compute the second row.
Of course, this process can be repeated for the third and later rows.
We will end up with two For Statements, one nested inside the other:
-The outer loop will count from 1 to the number of years requested by the user
-The inner loop will increment the interest rate from its lowest value to its highest value
Not the use of NUM_RATES to control two of the For Loops. If we later change the size of the array called value, the loops will adjust automatically.
-----------------------------------------------------------------------------------------------------------------------------------
CLICK FOR BIG PICTURES WITHOUT COMPRESSION
-----------------------------------------------------------------------------------------------------------------------------------
<img src="http://i.cubeupload.com/lQ5fzp.jpg">
Code:#include<stdio.h>Code:#define NUM_RATES(sizeof(value)/sizeof(value[0])) /* NUM_RATES is a macro thatfinds the length of the array based on the SIZE OF THE ARRAY in bytes ( size of(value) ) DIVIDED BYthe SIZE OF EACH ELEMENT ( sizeof(value[0]) ) */ #defineINITIAL_BALANCE 100.00 main() { intlow_rate; /*Userinput --- the lowest interest rate*/ intnum_years; /*Userinput --- the number of years*/ inti; intyear; floatvalue[5]; /*Remember that the number in the brackets represents the number ofelements --- 1 for each of the 5 year timeline in this case */ printf("Enterinterest rate: "); scanf("%d",&low_rate); printf("Enternumber of years: "); scanf("%d",&num_years); printf("\nYears"); /*This statement prints outthe label for the "YEAR" column on the left side of the chart */ /*This loop prints out the LABELS FOR THE INTEREST RATE ROW AT THE TOP. Right next to the "Year" */ for(i = 0; i < NUM_RATES; i++) { /* NUM_RATES is 5 here Remember that NUM_RATES, seen above, is amacro for (sizeof(value)/sizeof(value[0])) This macro measures the LENGTH of thearray by dividing the array size in bytes by the element size in bytes The macro format makes it easy for theloops to adjust if we need to CHANGE THE SIZE OF THE ARRAY */ printf("%6d",low_rate +i); /* Prints out the low rate + i according to the loop above */ value[i]= INITIAL_BALANCE; } printf("\n"); /* This statement starts a new line for theloop below */ /* This loop prints the numbers IN THE YEARSCOLUMN. Counts from 1 to the number ofyears entered by the user */ for(year = 1; year <= num_years; year++) { printf("%3d ", year); /* This NESTED loop increments the interestrate FROM ITS LOWEST VALUE TO ITS HIGHEST VALUE AND MULTIPLIES IT BY $100.Occurs right after the year column entry printed */ for(i = 0 ; i < NUM_RATES ; i++) { value[i]= value[i] + ( ((low_rate + i) / 100.0) * value[i]); printf("%7.2f",value[i]); } printf("\n"); /*This statement causes the valueto be placed on the next line*/ } return0; }