gpt4 book ai didi

c - 对带有数组和嵌套 For 循环的程序中特定语句的作用感到困惑

转载 作者:行者123 更新时间:2023-11-30 19:10:46 26 4
gpt4 key购买 nike

我正在用 KN King 的书自学C

我一直在阅读作者提供的示例,并在 Visual Studio 中添加我自己的注释。

我了解了下面程序中包含的循环的“大局”,但我丢失了一些细节。

我希望您能启发我一下下面图片中红色圈出的语句的作用?它在做什么?

图 1 - 完整程序:

图 2 - 底部两个环被切掉,显示预期结果良好:

在第一个循环括号的上下文中,红色的语句似乎没有必要。但是当我删除它时,我得到的程序无法按预期运行。因此它必须连接到其下面的两个循环之一。我正在摸不着头脑它到底做了什么。

作者在下面的粗体文本中暗示了其目的,但他并没有 100% 明确地说明发生了什么。这很奇怪,因为红色语句处理包含初始余额的数组,但在循环中它包含在处理打印顶行的利率标签中。

作者说:

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

Note 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;

}

最佳答案

有问题的行:

value[i] = INITIAL_BALANCE;

将值数组中每个元素的值设置为常量值(即100.00)。否则,未初始化数组中的值可能是 0 或随机/垃圾值,具体取决于编译器。

参见this sample具有更新的输出。请注意在 main() 的函数声明之前添加的关键字 int,以及形式参数 argcargc>。 KN King 的 C 书应该解释这些,尽管您可以在网上找到许多资源,例如 The GNU C Programming Tutorial .

关于c - 对带有数组和嵌套 For 循环的程序中特定语句的作用感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40976830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com