Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #98007 > unrolled thread
| Started by | Potomac <ravine557@gmail.com> |
|---|---|
| First post | 2016-12-05 06:32 -0800 |
| Last post | 2016-12-12 04:18 +0000 |
| Articles | 20 on this page of 107 — 18 participants |
Back to article view | Back to comp.lang.c
Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Potomac <ravine557@gmail.com> - 2016-12-05 06:32 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-05 08:34 -0700
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-05 15:43 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-05 10:41 -0700
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-05 15:37 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops John Bode <jfbode1029@gmail.com> - 2016-12-05 10:43 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops potomac <ravine557@gmail.com> - 2016-12-07 01:13 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-07 10:55 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Philip Lantz <prl@canterey.us> - 2016-12-07 20:34 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-08 11:08 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Ian Collins <ian-news@hotmail.com> - 2016-12-08 08:23 +1300
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops potomac <ravine557@gmail.com> - 2016-12-07 01:07 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-07 08:46 -0700
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-07 16:23 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops potomac <ravine557@gmail.com> - 2016-12-07 01:12 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-07 08:49 -0700
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops John Bode <jfbode1029@gmail.com> - 2016-12-07 15:07 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops potomac <ravine557@gmail.com> - 2016-12-07 03:46 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-07 11:59 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-07 12:05 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Keith Thompson <kst-u@mib.org> - 2016-12-07 09:40 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-07 15:13 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-07 20:24 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-07 16:28 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-07 21:48 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-07 17:02 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-07 22:19 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-07 17:27 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-07 22:51 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-08 09:10 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-08 14:13 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops David Brown <david.brown@hesbynett.no> - 2016-12-08 15:57 +0100
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Ian Collins <ian-news@hotmail.com> - 2016-12-08 18:02 +1300
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Keith Thompson <kst-u@mib.org> - 2016-12-08 08:48 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops jameskuyper@verizon.net - 2016-12-07 15:33 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-08 09:13 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-08 14:22 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops David Brown <david.brown@hesbynett.no> - 2016-12-08 16:03 +0100
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-08 10:44 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Melzzzzz <Melzzzzz@zzzzz.com> - 2016-12-08 15:48 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-08 21:41 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-08 16:16 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-08 21:47 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops jameskuyper@verizon.net - 2016-12-08 20:16 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops David Brown <david.brown@hesbynett.no> - 2016-12-09 11:19 +0100
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops supercat@casperkitty.com - 2016-12-09 08:00 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Tim Rentsch <txr@alumni.caltech.edu> - 2016-12-13 13:10 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops jameskuyper@verizon.net - 2016-12-13 13:55 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Tim Rentsch <txr@alumni.caltech.edu> - 2016-12-15 01:13 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops jameskuyper@verizon.net - 2016-12-15 07:02 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Tim Rentsch <txr@alumni.caltech.edu> - 2016-12-15 19:56 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops supercat@casperkitty.com - 2016-12-18 10:00 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops supercat@casperkitty.com - 2016-12-14 11:45 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Keith Thompson <kst-u@mib.org> - 2016-12-14 12:14 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Tim Rentsch <txr@alumni.caltech.edu> - 2016-12-15 01:24 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops supercat@casperkitty.com - 2016-12-15 08:02 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Keith Thompson <kst-u@mib.org> - 2016-12-15 12:10 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Philip Lantz <prl@canterey.us> - 2016-12-16 20:00 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-09 11:05 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-09 16:21 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops jameskuyper@verizon.net - 2016-12-09 13:10 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-09 23:44 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops supercat@casperkitty.com - 2016-12-10 10:18 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-11 16:18 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-09 08:26 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-09 11:15 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-09 16:31 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-09 23:47 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-11 16:05 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-11 16:21 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-11 23:15 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-11 18:57 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-12 00:05 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-11 21:09 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-12 08:25 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Tim Rentsch <txr@alumni.caltech.edu> - 2016-12-10 07:59 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops jameskuyper@verizon.net - 2016-12-08 13:51 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops jameskuyper@verizon.net - 2016-12-08 13:34 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-08 21:52 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops jameskuyper@verizon.net - 2016-12-08 19:33 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-09 11:20 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops jameskuyper@verizon.net - 2016-12-09 13:28 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-09 23:51 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-09 16:46 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-09 13:55 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-07 20:17 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Keith Thompson <kst-u@mib.org> - 2016-12-07 12:23 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops twalker470@googlemail.com - 2016-12-07 05:50 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-07 08:55 -0700
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-07 16:23 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-07 10:56 -0700
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops potomac <ravine557@gmail.com> - 2016-12-07 04:14 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Barry Schwarz <schwarzb@dqel.com> - 2016-12-07 11:03 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops John Bode <jfbode1029@gmail.com> - 2016-12-09 08:06 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Keith Thompson <kst-u@mib.org> - 2016-12-09 09:23 -0800
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-09 17:38 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-09 14:01 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Richard Heathfield <rjh@cpax.org.uk> - 2016-12-09 19:09 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-12-09 12:38 -0700
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-10 00:08 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-10 00:16 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-09 23:59 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-10 11:51 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-11 16:22 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 21:52 +0000
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Jerry Stuckle <jstucklex@attglobal.net> - 2016-12-11 22:32 -0500
Re: Stumped on the role of a particular statement in a program with Arrays & Nested For Loops Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-12 04:18 +0000
Page 1 of 6 [1] 2 3 4 5 6 Next page →
| From | Potomac <ravine557@gmail.com> |
|---|---|
| Date | 2016-12-05 06:32 -0800 |
| Subject | Stumped on the role of a particular statement in a program with Arrays & Nested For Loops |
| Message-ID | <f59dc04a-dbf6-4012-a97b-13d3ef9909a0@googlegroups.com> |
I'm teaching myself C with this book: https://www.amazon.com/C-Programming-Modern-Approach-2nd/dp/0393979504
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 below, 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?
PICTURE 1: http://i.cubeupload.com/lQ5fzp.jpg
PICTURE 2: http://i.cubeupload.com/QRarFT.jpg
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.
The author alludes to the purpose in BOLD-FACED TEXT 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 author says says: "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."
Any help would be much appreciated!
I included the text from the book and the source code below
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
http://i.cubeupload.com/zb9tJ8.png
Stumped on role of a particular statement in a program with Arrays & Nested For Loops-chart-png
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.
----------------------------------------------------------------------
#include <stdio.h>
#define NUM_RATES (sizeof(value)/sizeof(value[0])) /* NUM_RATES is a macro that finds the length of the array
based on the SIZE OF THE ARRAY in bytes ( size of(value) ) DIVIDED BY the SIZE OF EACH ELEMENT ( sizeof(value[0]) ) */
#define INITIAL_BALANCE 100.00
main()
{
int low_rate; /*User input --- the lowest interest rate*/
int num_years; /*User input --- the number of years*/
int i;
int year;
float value[5]; /*Remember that the number in the brackets represents the number of elements --- 1 for each of the 5 year timeline in this case */
printf("Enter interest rate: ");
scanf_s("%d", &low_rate);
printf("Enter number of years: ");
scanf_s("%d", &num_years);
printf("\nYears"); /*This statement prints out the 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 a macro for (sizeof(value)/sizeof(value[0]))
This macro measures the LENGTH of the array by dividing the array size in bytes by the element size in bytes
The macro format makes it easy for the loops 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 the loop below */
/* This loop prints the numbers IN THE YEARS COLUMN. Counts from 1 to the number of years entered by the user */
for (year = 1; year <= num_years; year++) {
printf("%3d ", year);
/* This NESTED loop increments the interest rate 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 value to be placed on the next line*/
}
return 0;
}
[toc] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2016-12-05 08:34 -0700 |
| Message-ID | <1bvauybdau.fsf@pfeifferfamily.net> |
| In reply to | #98007 |
Potomac <ravine557@gmail.com> writes:
> I'm teaching myself C with this book: https://www.amazon.com/C-Programming-Modern-Approach-2nd/dp/0393979504
>
> 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 below, 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?
>
> PICTURE 1: http://i.cubeupload.com/lQ5fzp.jpg
>
> PICTURE 2: http://i.cubeupload.com/QRarFT.jpg
>
> 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.
The program is calculating the interest on a given amount of money at
different interest rates. In all cases, the given amount of money is
$100.00.
The #define statement is setting that up by defining a constant that is
100.00. It's used later, in the loop, to assign the initial value to
each element of the value[] array.
The reason to do this (instead of just using 100.00 in the code) is that
in a larger program it's clearer. Rather than having some obscure
constant, you use a (hopefully) meaningful symbol.
I notice you also underlined 100.0 later -- that's a completely
different use of 100.00 from the first one; it's because low_rate+i is a
percentage, so you need to divide by 100 to get the actual amount to
add.
This is a good example of why a defined constant was used above it -- if
the author had just used 100.00 in-line, and had decided to change the
program to start at $1000.00 instead, it would be easy to use a global
search-and-replace to change the starting amount to 1000 and
accidentally change this second 100.00 to 1000.00 as well.
>
> The author alludes to the purpose in BOLD-FACED TEXT 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 author says says: "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."
>
> Any help would be much appreciated!
>
> I included the text from the book and the source code below
>
> 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
>
> http://i.cubeupload.com/zb9tJ8.png
>
> Stumped on role of a particular statement in a program with Arrays & Nested For Loops-chart-png
>
> 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.
>
>
>
> ----------------------------------------------------------------------
>
>
> #include <stdio.h>
>
>
>
> #define NUM_RATES (sizeof(value)/sizeof(value[0])) /* NUM_RATES is a macro that finds the length of the array
> based on the SIZE OF THE ARRAY in bytes ( size of(value) ) DIVIDED BY the SIZE OF EACH ELEMENT ( sizeof(value[0]) ) */
> #define INITIAL_BALANCE 100.00
>
>
>
> main()
> {
> int low_rate; /*User input --- the lowest interest rate*/
> int num_years; /*User input --- the number of years*/
> int i;
> int year;
> float value[5]; /*Remember that the number in the brackets represents the number of elements --- 1 for each of the 5 year timeline in this case */
>
> printf("Enter interest rate: ");
> scanf_s("%d", &low_rate);
> printf("Enter number of years: ");
> scanf_s("%d", &num_years);
>
>
> printf("\nYears"); /*This statement prints out the 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 a macro for (sizeof(value)/sizeof(value[0]))
> This macro measures the LENGTH of the array by dividing the array size in bytes by the element size in bytes
> The macro format makes it easy for the loops 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 the loop below */
>
>
>
> /* This loop prints the numbers IN THE YEARS COLUMN. Counts from 1 to the number of years entered by the user */
>
> for (year = 1; year <= num_years; year++) {
> printf("%3d ", year);
>
>
>
> /* This NESTED loop increments the interest rate 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 value to be placed on the next line*/
> }
>
> return 0;
>
> }
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2016-12-05 15:43 +0000 |
| Message-ID | <o241rm$ldt$1@dont-email.me> |
| In reply to | #98013 |
On 05/12/16 15:34, Joe Pfeiffer wrote: > Potomac <ravine557@gmail.com> writes: > >> 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. > <snip> > > The reason to do this (instead of just using 100.00 in the code) is that > in a larger program it's clearer. Rather than having some obscure > constant, you use a (hopefully) meaningful symbol. Potomac, you will observe that Joe and I read the same article, and gave you two completely different answers. In fact, we answered different questions, even though you only posted one. I'm not sure whether Joe has answered your question, or whether I have, but in any case you might reasonably conclude that the question wasn't as clear as it might have been! It doesn't actually matter, of course, which one of us interpreted your question correctly, as long as one of us did, but there is a lesson to be drawn: asking good questions is not easy, and it does take some thought and care if you are not going to be misunderstood. -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2016-12-05 10:41 -0700 |
| Message-ID | <1br35mb7dp.fsf@pfeifferfamily.net> |
| In reply to | #98016 |
Richard Heathfield <rjh@cpax.org.uk> writes: > On 05/12/16 15:34, Joe Pfeiffer wrote: >> Potomac <ravine557@gmail.com> writes: >> >>> 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. >> > <snip> >> >> The reason to do this (instead of just using 100.00 in the code) is that >> in a larger program it's clearer. Rather than having some obscure >> constant, you use a (hopefully) meaningful symbol. > > Potomac, you will observe that Joe and I read the same article, and > gave you two completely different answers. In fact, we answered > different questions, even though you only posted one. > > I'm not sure whether Joe has answered your question, or whether I > have, but in any case you might reasonably conclude that the question > wasn't as clear as it might have been! > > It doesn't actually matter, of course, which one of us interpreted > your question correctly, as long as one of us did, but there is a > lesson to be drawn: asking good questions is not easy, and it does > take some thought and care if you are not going to be misunderstood. Ah, I saw he'd circled the #define statement and thought that was the actual source of his confusion. Let's hope at least one of us managed to help him out!
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2016-12-05 15:37 +0000 |
| Message-ID | <o241gf$k3l$1@dont-email.me> |
| In reply to | #98007 |
On 05/12/16 14:32, Potomac wrote:
> I'm teaching myself C with this book:
> https://www.amazon.com/C-Programming-Modern-Approach-2nd/dp/0393979504
>
> 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
> below, 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?
>
> PICTURE 1: http://i.cubeupload.com/lQ5fzp.jpg
>
> PICTURE 2: http://i.cubeupload.com/QRarFT.jpg
It's easier just to paste the code into your article.
> Within the context of that first loop's brackets, the statement in
> red seems unnecessary.
Uh, it's essential. See below.
> 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.
The program shows the result of applying a number of different interest
rates to an initial balance over the given number of years. The program
has to keep track of current balances for each of those interest rates,
and it uses the value[] array for that purpose. Each element in that
array must be given the starting balance. If you don't do this, the
elements of the value[] array will have indeterminate values, and the
program will not work as intended.
> The author alludes to the purpose in BOLD-FACED TEXT 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
Yes, he's using the loop for two purposes. It might have confused you
less if he'd done this instead:
/* set initial value for each rate */
for(i = 0; i < numrates; i++)
{
value[i] = INITIAL_BALANCE;
}
/* print column titles */
for(i = 0; i < numrates; i++)
{
printf("%6d", low_rate + i);
}
but it would have done exactly the same thing.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | John Bode <jfbode1029@gmail.com> |
|---|---|
| Date | 2016-12-05 10:43 -0800 |
| Message-ID | <9a9287d4-63ef-4f69-af18-dabb00ed7e67@googlegroups.com> |
| In reply to | #98007 |
On Monday, December 5, 2016 at 8:32:38 AM UTC-6, potomac wrote:
> I'm teaching myself C with this book: https://www.amazon.com/C-Programming-Modern-Approach-2nd/dp/0393979504
>
> 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 below,
> 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?
>
> PICTURE 1: http://i.cubeupload.com/lQ5fzp.jpg
The directive
#define INITIAL_BALANCE 100.0
creates the *macro* INITIAL_BALANCE; when your source code is run through
the preprocessor, all instances of the text INITIAL_BALANCE will be
replaced with the text 100.0 before the code is fed to the compiler
proper. So after preprocessing, the line
value[i] = INITIAL_BALANCE;
becomes
value[i] = 100.0;
The author is basically using the macro as a symbolic constant for
the value 100.0. This serves several purposes:
1. Ease of maintenance - if you want to change the initial balance for
each value[i] from 100.0 to some other value, you only have to change
it in that one place, as opposed to changing multiple occurrences of
100.0. This especially saves your bacon if you're using a naked 100.0
for something *other* than your initial balance.
2. Guard against typos - the compiler isn't psychic; if I type 10.0 when I
meant 100.0, the compiler can't tell me I've goofed up (from its
perspective, both values are equally valid in the same context). If I
use the symbolic constant, I'll always get the right value. And if I
misspell the symbolic constant name, the compiler will yell at me.
>
> PICTURE 2: http://i.cubeupload.com/QRarFT.jpg
>
> 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.
The reason the line
value[i] = INITIAL_BALANCE;
exists is that the author wants each value[i] to initially be 100.0.
Without this explicit initialization, the contents of each value[i] are
indeterminate (basically, garbage).
Without that initialization step, the statement
value[i] = value[i] + ( ((low_rate + i) / 100.0) * value[i]);
computes interest on what is essentially a random value, and you get
garbage output.
The author chose to combine the array initialization with printing out the
rate labels simply because they took the same number of steps. I wouldn't
do this in production code (I'd factor the two operations out into their own
functions like printRateLabels() and initValues()), but that would
complicate the presentation of the code. The author is trying to get as
much across in as little page real estate as possible, so he's combining
operations that might otherwise be separated.
[snip remainder]
[toc] | [prev] | [next] | [standalone]
| From | potomac <ravine557@gmail.com> |
|---|---|
| Date | 2016-12-07 01:13 -0800 |
| Message-ID | <35638bb3-897e-4434-9dac-8116d0a1d5ed@googlegroups.com> |
| In reply to | #98037 |
Thanks John, Richard and Joe. Very helpful responses
I understand the purpose of the initalizer now.
John, I liked your idea of putting the array initalizer in its own function with its own loop. It didn't seem logical to me at first why he put it in the same loop as the Interest Rates. I get now that he was just trying to conserve space on the book page.
But why not go a step further and initalize the array at the top like this? Why do you need a loop at all to do this?
float value[5] = {100};
Is this bad style because it makes it harder to modify the program by doing away with the macro?
I checked the following code and it gave the same output
#include <stdio.h>
#define NUM_RATES (sizeof(value)/sizeof(value[0]))
#define INITIAL_BALANCE 100.00
main()
{
int i, low_rate, num_years, year;
float value[5] = {100};
printf("Enter interest rate: ");
scanf_s("%d", &low_rate);
printf("Enter number of years: ");
scanf_s("%d", &num_years);
printf("\nYears");
for (i = 0; i < NUM_RATES; i++) {
printf("%6d", low_rate + i);
}
printf("\n");
for (year = 1; year <= num_years; year++) {
printf("%3d ", year);
for (i = 0; i < NUM_RATES; i++) {
value[i] += (low_rate + i) / 100.0 * value[i];
printf("%7.2f", value[i]);
}
printf("\n");
}
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-12-07 10:55 +0000 |
| Message-ID | <87lgvsf1p0.fsf@bsb.me.uk> |
| In reply to | #98238 |
potomac <ravine557@gmail.com> writes:
<snip>
> But why not go a step further and initalize the array at the top like
> this? Why do you need a loop at all to do this?
>
> float value[5] = {100};
This sets values[0] to 100 and all the other four elements to 0. The
loop sets them all to 100 which seems to be what was wanted (four
columns of zeros are not very informative!).
<snip>
> I checked the following code and it gave the same output
Not, I think, as the version that sets all of the elements of values to
the same initial balance.
<snip>
> printf("Enter interest rate: ");
> scanf_s("%d", &low_rate);
> printf("Enter number of years: ");
> scanf_s("%d", &num_years);
I don't think you gain much be using scanf_s here and it makes the code
dependent on functions that are not universally available.
> printf("\nYears");
> for (i = 0; i < NUM_RATES; i++) {
> printf("%6d", low_rate + i);
This width of 6 means the headings don't line up with the data columns
printed later. And using %d is very odd given that the value being
printed is a floating point one. I'd use %7.2f here just as below and
I'd probably put that in a macro as well so I could change the width in
one place:
#define FLOAT_FMT "%7.2f"
printf(FLOAT_FMT, low_rate + i);
<snip>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Philip Lantz <prl@canterey.us> |
|---|---|
| Date | 2016-12-07 20:34 -0800 |
| Message-ID | <MPG.32b282c5c5d1251c128@news.eternal-september.org> |
| In reply to | #98242 |
Ben Bacarisse wrote:
> potomac writes:
> > printf("\nYears");
> > for (i = 0; i < NUM_RATES; i++) {
> > printf("%6d", low_rate + i);
>
> This width of 6 means the headings don't line up with the data columns
> printed later. And using %d is very odd given that the value being
> printed is a floating point one.
low_rate and i are both declared as int.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-12-08 11:08 +0000 |
| Message-ID | <877f7ad6g3.fsf@bsb.me.uk> |
| In reply to | #98335 |
Philip Lantz <prl@canterey.us> writes:
> Ben Bacarisse wrote:
>> potomac writes:
>> > printf("\nYears");
>> > for (i = 0; i < NUM_RATES; i++) {
>> > printf("%6d", low_rate + i);
>>
>> This width of 6 means the headings don't line up with the data columns
>> printed later. And using %d is very odd given that the value being
>> printed is a floating point one.
>
> low_rate and i are both declared as int.
Ah, yes, so I see. Thanks. Had I looked properly, I would have made a
different point -- the low interest rate used for the range should
probably /not/ be an integer, but if, for some as yet unknown reason, it
should remain an int, the best way to line up the columns is still by
using the same format but now with a cast:
printf("%7.2f", (double)(low_rate + i));
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2016-12-08 08:23 +1300 |
| Message-ID | <ear5trF5s31U1@mid.individual.net> |
| In reply to | #98037 |
On 12/ 6/16 07:43 AM, John Bode wrote: > > The directive > > #define INITIAL_BALANCE 100.0 > > creates the *macro* INITIAL_BALANCE; when your source code is run through > the preprocessor, all instances of the text INITIAL_BALANCE will be > replaced with the text 100.0 before the code is fed to the compiler > proper. So after preprocessing, the line > > value[i] = INITIAL_BALANCE; > > becomes > > value[i] = 100.0; > > The author is basically using the macro as a symbolic constant for > the value 100.0. This serves several purposes: In this case all of these would be better served with a const float rather than a macro. > 1. Ease of maintenance - if you want to change the initial balance for > each value[i] from 100.0 to some other value, you only have to change > it in that one place, as opposed to changing multiple occurrences of > 100.0. This especially saves your bacon if you're using a naked 100.0 > for something *other* than your initial balance. > > 2. Guard against typos - the compiler isn't psychic; if I type 10.0 when I > meant 100.0, the compiler can't tell me I've goofed up (from its > perspective, both values are equally valid in the same context). If I > use the symbolic constant, I'll always get the right value. And if I > misspell the symbolic constant name, the compiler will yell at me. -- Ian
[toc] | [prev] | [next] | [standalone]
| From | potomac <ravine557@gmail.com> |
|---|---|
| Date | 2016-12-07 01:07 -0800 |
| Message-ID | <0ec906d1-40ac-4817-a3fb-584fa11c15c5@googlegroups.com> |
| In reply to | #98007 |
Thanks Joe, Richard and John. Very helpful responses!
I get the purpose of the array initializer now.
John, your insight on why the author put the array initializer in the same loop at the interest rates makes sense. He really does seem to be cramming real estate.
I like the idea of putting that array initalizer in its own function for production code. Makes more sense to me.
But why do you need a loop at all to do this? Why not go a step further and just initalize the array at the top by saying this at the top?
float value[5] = { 100 };
I guess the effect of that would be less flexibility to modify the array since you lose the macro?
I did check the following program and it gives the same output as the original version provided by the author:
----------------------------------------------------------------------------
#include <stdio.h>
#define NUM_RATES (sizeof(value)/sizeof(value[0]))
#define INITIAL_BALANCE 100.00
main()
{
int i, low_rate, num_years, year;
float value[5] = {100};
printf("Enter interest rate: ");
scanf_s("%d", &low_rate);
printf("Enter number of years: ");
scanf_s("%d", &num_years);
printf("\nYears");
for (i = 0; i < NUM_RATES; i++) {
printf("%6d", low_rate + i);
}
printf("\n");
for (year = 1; year <= num_years; year++) {
printf("%3d ", year);
for (i = 0; i < NUM_RATES; i++) {
value[i] += (low_rate + i) / 100.0 * value[i];
printf("%7.2f", value[i]);
}
printf("\n");
}
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2016-12-07 08:46 -0700 |
| Message-ID | <1ba8c7pwrv.fsf@pfeifferfamily.net> |
| In reply to | #98236 |
potomac <ravine557@gmail.com> writes:
> Thanks Joe, Richard and John. Very helpful responses!
>
> I get the purpose of the array initializer now.
>
> John, your insight on why the author put the array initializer in the same loop at the interest rates makes sense. He really does seem to be cramming real estate.
>
> I like the idea of putting that array initalizer in its own function for production code. Makes more sense to me.
>
> But why do you need a loop at all to do this? Why not go a step further and just initalize the array at the top by saying this at the top?
>
> float value[5] = { 100 };
Because that would initialize value[0] to 100.0, and value[1] through value[4]
to 0 (if you don't provide an initializer to an array at all, it isn't
initialized. If you initialize any values, the ones you specify are set
to the values you specify, and the others are set to 0). You could
initialize with
float value[5] = { 100.0, 100.0, 100.0, 100.0, 100.0 };
but that would be *really* prone to errors (for instance, if you changed
the size of the array and missed changing the initializer list to match)
> I guess the effect of that would be less flexibility to modify the array since you lose the macro?
>
> I did check the following program and it gives the same output as the original version provided by the author:
<snip>
Excellent! One thing I noticed while looking at the code is that if it
were my program I'd use a different definition of NUM_RATES and
declaration of the array:
#define NUM_RATES 5
and
float array[NUM_RATES};
That makes the number of rates explicit, and gets rid of the "magic
constant" in the array declaration.
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2016-12-07 16:23 +0000 |
| Message-ID | <o29cur$pls$1@dont-email.me> |
| In reply to | #98267 |
On 07/12/16 15:46, Joe Pfeiffer wrote: <snip> > > #define NUM_RATES 5 > > and > > float array[NUM_RATES}; ITYM float array[NUM_RATES]; -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | potomac <ravine557@gmail.com> |
|---|---|
| Date | 2016-12-07 01:12 -0800 |
| Message-ID | <d9edc7c4-f041-40bb-b671-6b3d89337093@googlegroups.com> |
| In reply to | #98007 |
Thanks John, Richard and Joe. Very helpful responses
I understand the purpose of the initalizer now.
John, I liked your idea of putting the array initalizer in its own function with its own loop. It didn't seem logical to me at first why he put it in the same loop as the Interest Rates. I get now that he was just trying to conserve space on the book page.
But why not go a step further and initalize the array at the top like this? Why do you need a loop at all to do this?
float value[5] = {100};
Is this bad style because it makes it harder to modify the program by doing away with the macro?
I checked the following code and it gave the same output
#include <stdio.h>
#define NUM_RATES (sizeof(value)/sizeof(value[0]))
#define INITIAL_BALANCE 100.00
main()
{
int i, low_rate, num_years, year;
float value[5] = {100};
printf("Enter interest rate: ");
scanf_s("%d", &low_rate);
printf("Enter number of years: ");
scanf_s("%d", &num_years);
printf("\nYears");
for (i = 0; i < NUM_RATES; i++) {
printf("%6d", low_rate + i);
}
printf("\n");
for (year = 1; year <= num_years; year++) {
printf("%3d ", year);
for (i = 0; i < NUM_RATES; i++) {
value[i] += (low_rate + i) / 100.0 * value[i];
printf("%7.2f", value[i]);
}
printf("\n");
}
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2016-12-07 08:49 -0700 |
| Message-ID | <1b60mvpwm6.fsf@pfeifferfamily.net> |
| In reply to | #98237 |
potomac <ravine557@gmail.com> writes:
> Thanks John, Richard and Joe. Very helpful responses
>
> I understand the purpose of the initalizer now.
>
> John, I liked your idea of putting the array initalizer in its own function with its own loop. It didn't seem logical to me at first why he put it in the same loop as the Interest Rates. I get now that he was just trying to conserve space on the book page.
>
> But why not go a step further and initalize the array at the top like this? Why do you need a loop at all to do this?
>
> float value[5] = {100};
>
>
> Is this bad style because it makes it harder to modify the program by doing away with the macro?
>
> I checked the following code and it gave the same output
>
> #include <stdio.h>
>
> #define NUM_RATES (sizeof(value)/sizeof(value[0]))
> #define INITIAL_BALANCE 100.00
>
> main()
> {
> int i, low_rate, num_years, year;
> float value[5] = {100};
>
> printf("Enter interest rate: ");
> scanf_s("%d", &low_rate);
> printf("Enter number of years: ");
> scanf_s("%d", &num_years);
>
> printf("\nYears");
> for (i = 0; i < NUM_RATES; i++) {
> printf("%6d", low_rate + i);
> }
> printf("\n");
>
> for (year = 1; year <= num_years; year++) {
> printf("%3d ", year);
> for (i = 0; i < NUM_RATES; i++) {
> value[i] += (low_rate + i) / 100.0 * value[i];
> printf("%7.2f", value[i]);
> }
> printf("\n");
> }
>
> return 0;
>
> }
Umm... it shouldn't have. All of the columns after the first one
should have been all 0's. I changed the scanf_s calls to scanf calls
for my Linux system and it printed
Enter interest rate: 5
Enter number of years: 10
Years 5 6 7 8 9
1 105.00 0.00 0.00 0.00 0.00
2 110.25 0.00 0.00 0.00 0.00
3 115.76 0.00 0.00 0.00 0.00
4 121.55 0.00 0.00 0.00 0.00
5 127.63 0.00 0.00 0.00 0.00
6 134.01 0.00 0.00 0.00 0.00
7 140.71 0.00 0.00 0.00 0.00
8 147.75 0.00 0.00 0.00 0.00
9 155.13 0.00 0.00 0.00 0.00
10 162.89 0.00 0.00 0.00 0.00
as I would expect.
[toc] | [prev] | [next] | [standalone]
| From | John Bode <jfbode1029@gmail.com> |
|---|---|
| Date | 2016-12-07 15:07 -0800 |
| Message-ID | <2eeb55d3-6d3f-4a60-bee3-954f039a08c5@googlegroups.com> |
| In reply to | #98237 |
On Wednesday, December 7, 2016 at 3:13:02 AM UTC-6, potomac wrote:
> Thanks John, Richard and Joe. Very helpful responses
>
> I understand the purpose of the initalizer now.
>
> John, I liked your idea of putting the array initalizer in its own
> function with its own loop. It didn't seem logical to me at first why he
> put it in the same loop as the Interest Rates. I get now that he was just
> trying to conserve space on the book page.
>
> But why not go a step further and initalize the array at the top like
> this? Why do you need a loop at all to do this?
>
> float value[5] = {100};
>
That only initializes the first element to 100; the remaining elements
will be initialized to 0. From http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
(online pre-publication draft of the C 2011 standard), section 6.7.9,
para 21:
> If there are fewer initializers in a brace-enclosed list than there are
> elements or members of an aggregate, or fewer characters in a string
> literal used to initialize an array of known size than there are elements
> in the array, the remainder of the aggregate shall be initialized
> implicitly the same as objects that have static storage duration.
Rules for initializing objects with static storage duration are given
in paragraph 10 of the same section:
> If an object that has automatic storage duration is not initialized
> explicitly, its value is indeterminate. If an object that has static or
> thread storage duration is not initialized explicitly, then:
>
> — if it has pointer type, it is initialized to a null pointer;
>
> — if it has arithmetic type, it is initialized to (positive or
> unsigned) zero;
>
> — if it is an aggregate, every member is initialized (recursively)
> according to these rules, and any padding is initialized to zero
> bits;
>
> — if it is a union, the first named member is initialized
> (recursively) according to these rules, and any padding is
> initialized to zero bits;
You'd have to write
float value[5] = {100.0, 100.0, 100.0, 100.0, 100.0};
to initialize *all* of the elements to 100.0.
Which is fine if you know you're never going to have more than a handful
of values in that array. However, if there's a possibility that the size
of that array could change in the future to something like, say, 500
entries, or if it's something that wouldn't be known until runtime, then
you'd definitely want to initialize it in a separate operation:
int main( void )
{
...
size_t numValues = getNumValues();
...
float *values = malloc( sizeof *values * numValues );
if ( !values )
{
fprintf( stderr, "FATAL: could not allocate memory for values\n" );
exit( EXIT_FAILURE );
}
initValues( values, numValues, INITIAL_VALUE );
...
}
void initValues( float *values, size_t numValues, float initialValue )
{
for ( size_t i = 0; i < numValues; i++ )
values[i] = initialValue;
}
>
> Is this bad style because it makes it harder to modify the program by
> doing away with the macro?
>
I'd at least use the macro in the initialization:
float value[5] = { INITIAL_BALANCE,
INITIAL_BALANCE,
INITIAL_BALANCE,
INITIAL_BALANCE,
INITIAL_BALANCE };
If you ever find yourself using the same value more than once, it's usually
a good idea to use a symbolic constant for it. Again, would you rather
change the value in one place or five?
[toc] | [prev] | [next] | [standalone]
| From | potomac <ravine557@gmail.com> |
|---|---|
| Date | 2016-12-07 03:46 -0800 |
| Message-ID | <920aa5e5-445e-4d98-90a7-cc4c1bc89865@googlegroups.com> |
| In reply to | #98007 |
Whoops, I meant to type: float value[5] = { 100, 100, 100, 100, 100 };
Even though that definitely gives the same output, there must be some good reason the author chose the other method of initializing. Is it because the macro affords flexibility?
Ben, as for scanf_s, I keep using that it my code because I haven't yet figured out a way to make the error message go away in Visual Studio 2015 and won't let me run the program. I understand that scan_s is for security against buffer attacks and that it's proprietary to Visual Studio
Someone told me to put this line before the statements, but it doesn't make the error message go away. What would you suggest?
#define _CRT_SECURE_NO_DEPRECATE
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-12-07 11:59 +0000 |
| Message-ID | <87a8c8eyqp.fsf@bsb.me.uk> |
| In reply to | #98246 |
potomac <ravine557@gmail.com> writes:
> Whoops, I meant to type: float value[5] = { 100, 100, 100, 100, 100 };
>
> Even though that definitely gives the same output, there must be some
> good reason the author chose the other method of initializing. Is it
> because the macro affords flexibility?
I think you mean loop not macro. The original code set the initial
value in the loop that prints the headers.
It's hard to know the best way to do this in the small program, but a
loop is certainly more flexible than writing out the initial value five
times.
> Ben, as for scanf_s, I keep using that it my code because I haven't
> yet figured out a way to make the error message go away in Visual
> Studio 2015 and won't let me run the program. I understand that scan_s
> is for security against buffer attacks and that it's proprietary to
> Visual Studio
They are not proprietary -- they are an optional annex in the latest C
standard. They are not universally implemented, but that's not really
the same as proprietary.
> Someone told me to put this line before the statements, but it doesn't
> make the error message go away. What would you suggest?
>
> #define _CRT_SECURE_NO_DEPRECATE
Sorry, I don't know much about VS, but someone here must know.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2016-12-07 12:05 +0000 |
| Message-ID | <o28tro$4v8$1@dont-email.me> |
| In reply to | #98246 |
On 07/12/16 11:46, potomac wrote:
> Whoops, I meant to type: float value[5] = { 100, 100, 100, 100,
> 100 };
>
> Even though that definitely gives the same output, there must be some
> good reason the author chose the other method of initializing. Is it
> because the macro affords flexibility?
The macro reduces the cost of maintenance. So does the loop.
Let's say you decide that you actually want the values to start from
123.45 instead of 100.00.
With the version as written, you have to make one change:
#define what was it, INITIAL_BALANCE? Well, let's use that.
#define INITIAL_BALANCE 100.0
becomes
#define INITIAL_BALANCE 123.45
And that's the only change.
Your way, you have to change this:
float value[5] = { 100, 100, 100, 100, 100 };
to this:
float value[5] = { 123.45, 123.45, 123.45, 123.45, 123.45 };
Okay, let's say we want thirty interest rates instead of five. You have
to change this:
float value[5] = { 100, 100, 100, 100, 100 };
to this:
float value[30] = { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100 };
Oh, but wait! We wanted 123.45, not 100. Well, I'll let you fix all
those yourself.
Do you see the point?
> Ben, as for scanf_s, I keep using that it my code because I haven't
> yet figured out a way to make the error message go away in Visual
> Studio 2015 and won't let me run the program.
Visual Studio is *required* to support scanf if it is to be a conforming
C compiler. There is an ugly fix:
#pragma warning(disable:4996)
but it shouldn't be necessary. Complain to your vendor if they advertise
the product as a conforming C compiler but don't support scanf.
> I understand that
> scan_s is for security against buffer attacks and that it's
> proprietary to Visual Studio
Well, to make a scanf call vulnerable to a buffer overrun attack it is
necessary to send it bad information. It is even easier to send bad
information to scanf_s because there is more information to send.
> Someone told me to put this line before the statements, but it
> doesn't make the error message go away. What would you suggest?
>
> #define _CRT_SECURE_NO_DEPRECATE
That used to work. The pragma still works. But neither of them should be
necessary --- the compiler is /required/ to support scanf without adding
anything special to the source code. Otherwise, if you bought it
believing it to be a conforming C compiler, and if it were advertised to
you as such, and if that conformance was a reason for your selecting the
product, it should be reasonably easy to get your money back.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
Page 1 of 6 [1] 2 3 4 5 6 Next page →
Back to top | Article view | comp.lang.c
csiph-web