编写一个循环,将每个数组元素设置为自身和下一个元素的总和,但最后一个元素保持不变。注意不要超出最后一个元素。例如: 初步分数:10,20,30,40 循环后得分:30,50,70,40 第一个元素是30或10 + 20,第二个元素是50或20 + 30,第三个元素是70或30 + 40.最后一个元素保持不变。 这是我需要帮助的功课问题。 我遇到的问题是,如果bonusScores是负数,它对我的代码使用的例子是-100,-200,-300,-400,-500。
include<stdio.h>
int main(void)
{
const int SCORES_SIZE = 4;
int bonusScores[SCORES_SIZE];
int i = 0;
bonusScores[0] = 10;
bonusScores[1] = 20;
bonusScores[2] = 30;
bonusScores[3] = 40;
/* Your solution goes here */
for (i = 0; i < SCORES_SIZE; i++)
{
if (( bonusScores[i] <= bonusScores[i +1] ) || (bonusScores[i] < bonusScores [i+1]))
{
bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);
}
else
{
bonusScores[i] = bonusScores[i];
}
}
for (i = 0; i < SCORES_SIZE; ++i)
{
printf("%d ", bonusScores[i]);
}
printf("\n");
return 0;
}
答案 0 :(得分:2)
通过在循环中添加if
条件,您将负数作为问题。这是多余的。您刚刚被要求获取数组并对其进行转换,以便元素i
代表原始数组中的a[i] + a[i+1]
。这并不需要对标志进行任何特殊处理。
另外,请注意,在您的代码中,您可能会引用元素i + 1
,这可能是超出数组范围的元素(假设为i = 3
)。当您修改代码以正确循环时,请确保避免在数组边界之外进行索引。
您可以通过在for
循环中使用适当的条件来完全避免检查。 i
不仅必须小于数组的大小,而i+1
也必须满足此条件。
答案 1 :(得分:2)
您应该只遍历数组SCORES_SIZE-1
次。在循环内部,只需添加current + next并将其存储到数组中,如下所示:
include<stdio.h>
int main(void)
{
const int SCORES_SIZE = 4;
int bonusScores[SCORES_SIZE];
int i = 0;
bonusScores[0] = 10;
bonusScores[1] = 20;
bonusScores[2] = 30;
bonusScores[3] = 40;
/* Your solution goes here */
for (i = 0; i < SCORES_SIZE-1; i++)
{
bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);
}
for (i = 0; i < SCORES_SIZE; ++i)
{
printf("%d ", bonusScores[i]);
}
printf("\n");
return 0;
}
答案 2 :(得分:1)
两个负面奖金的总和将是一个更大的负面奖金,所以只需继续添加数字而无需检查它们是否为正面。
你的循环应该只运行到你想要更新的数字。由于您不需要更新最后一个号码,所以不要去那里 除此之外,我认为你正在走上正轨。
答案 3 :(得分:1)
在作业中写有
小心不要超越最后一个元素
然而这个循环
for (i = 0; i < SCORES_SIZE; i++)
{
if (( bonusScores[i] <= bonusScores[i +1] ) || (bonusScores[i] < bonusScores [i+1]))
{
bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);
}
else
{
bonusScores[i] = bonusScores[i];
}
}
当i
等于SCORES_SIZE - 1
时,尝试使用超出最后一个元素的索引
关于这种情况的任务中没有任何说法
if (( bonusScores[i] <= bonusScores[i +1] ) || (bonusScores[i] < bonusScores [i+1]))
与
相同if (( bonusScores[i] <= bonusScores[i +1] ))
目前尚不清楚为什么要写这个条件。
循环可以按以下方式查看
for ( i = 1; i < SCORES_SIZE; ++i )
{
bonusScores[i-1] += bonusScores[i];
}
这是一个示范程序
#include <stdio.h>
int main( void )
{
const int SCORES_SIZE = 4;
int bonusScores[SCORES_SIZE];
bonusScores[0] = 10;
bonusScores[1] = 20;
bonusScores[2] = 30;
bonusScores[3] = 40;
for ( int i = 1; i < SCORES_SIZE; ++i )
{
bonusScores[i-1] += bonusScores[i];
}
for ( int i = 0; i < SCORES_SIZE; ++i )
{
printf( "%d ", bonusScores[i] );
}
printf( "\n" );
bonusScores[0] = -100;
bonusScores[1] = -200;
bonusScores[2] = -300;
bonusScores[3] = -400;
for ( int i = 1; i < SCORES_SIZE; ++i )
{
bonusScores[i-1] += bonusScores[i];
}
for ( int i = 0; i < SCORES_SIZE; ++i )
{
printf( "%d ", bonusScores[i] );
}
printf( "\n" );
}
它的输出是
30 50 70 40
-300 -500 -700 -400
答案 4 :(得分:0)
也许,这就是你所期待的:
function isNumber(txt, evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
var value = txt.value + String.fromCharCode(charCode);
if (/^\d+(?:\.\d{0,3})?$/.test(value)) {
return true;
}
return false;
}
</script>
Voici la sortie console:
#include <stdio.h>
int main(void) {
const int SCORES_SIZE = 4;
int bonusScores[SCORES_SIZE];
int i = 0;
bonusScores[0] = -10;
bonusScores[1] = -20;
bonusScores[2] = -30;
bonusScores[3] = -40;
printf("Negative scores:\n");
for (i = 0; i < SCORES_SIZE; i++)
{
if(i!=SCORES_SIZE-1)
bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);
}
for (i = 0; i < SCORES_SIZE; ++i) {
printf("%d ", bonusScores[i]);
}
printf("\n");
bonusScores[0] = 10;
bonusScores[1] = 20;
bonusScores[2] = 30;
bonusScores[3] = 40;
printf("Positive scores:\n");
for (i = 0; i < SCORES_SIZE; i++)
{
if(i!=SCORES_SIZE-1)
bonusScores[i] = (bonusScores [i] + bonusScores[i+1]);
}
for (i = 0; i < SCORES_SIZE; ++i) {
printf("%d ", bonusScores[i]);
}
return 0;
}
答案 5 :(得分:0)
//This is the Java version of your problem, if anyone is interested.
public class StudentScores {
public static void main (String [] args) {
final int SCORES_SIZE = 4;
int[] bonusScores = new int[SCORES_SIZE];
int i = 0;
bonusScores[0] = 10;
bonusScores[1] = 20;
bonusScores[2] = 30;
bonusScores[3] = 40;
for (i = 0; i < SCORES_SIZE - 1; ++i) {
bonusScores[i] = (bonusScores[i] + bonusScores[i+1]);
}
for (i = 0; i < SCORES_SIZE; ++i) {
System.out.print(bonusScores[i] + " ");
}
System.out.println();
return;
}
}
答案 6 :(得分:0)
简单循环:
/* Your solution goes here */
for (i = 0; i < bonusScores.length; ++i)
{
if (i == bonusScores.length -1)
{
break;
}
bonusScores[i] = (bonusScores[i] + bonusScores[i+1]);
}
for (i = 0; i < bonusScores.length; ++i)
{
System.out.print(bonusScores[i] + " ");
}
System.out.println();
答案 7 :(得分:0)
for(i = 0; i < bonusScores.length - 1; i++)
{
bonusScores[i] += bonusScores[i + 1];
}