我创建了一个程序,它在列表中获取10个值,然后将其存储在另一个列表中,然后显示。现在问题是代码很长。你能告诉我如何使用for循环而不是声明和使用这些变量,然后再将它们复制到列表中。
这是我的代码:
#include <stdio.h>
int main() {
int marksList1 = 0;
int marksList2 = 0;
int marksList3 = 0;
int marksList4 = 0;
int marksList5 = 0;
int marksList6 = 0;
int marksList7 = 0;
int marksList8 = 0;
int marksList9 = 0;
int marksList10 = 0;
int correctedList1 = 0;
int correctedList2 = 0;
int correctedList3 = 0;
int correctedList4 = 0;
int correctedList5 = 0;
int correctedList6 = 0;
int correctedList7 = 0;
int correctedList8 = 0;
int correctedList9 = 0;
int correctedList10 = 0;
printf("Enter Marks for Students: ");
scanf("%d", &marksList1);
scanf("%d", &marksList2);
scanf("%d", &marksList3);
scanf("%d", &marksList4);
scanf("%d", &marksList5);
scanf("%d", &marksList6);
scanf("%d", &marksList7);
scanf("%d", &marksList8);
scanf("%d", &marksList9);
scanf("%d", &marksList10);
correctedList1 = marksList1;
correctedList2 = marksList2;
correctedList3 = marksList3;
correctedList4 = marksList4;
correctedList5 = marksList5;
correctedList6 = marksList6;
correctedList7 = marksList7;
correctedList8 = marksList8;
correctedList9 = marksList9;
correctedList10 = marksList10;
printf("\n");
printf("CorrectedList1: correctedList1);
printf("CorrectedList1: correctedList1);
printf("CorrectedList1: correctedList1);
printf("CorrectedList1: correctedList1);
printf("CorrectedList1: correctedList1);
printf("CorrectedList1: correctedList1);
printf("CorrectedList1: correctedList1);
printf("CorrectedList1: correctedList1);
printf("CorrectedList1: correctedList1);
printf("CorrectedList1: correctedList1);
}
答案 0 :(得分:2)
你应该像这样声明了标记列表和更正列表的列表:
int i, marksList[10] = {0};
int correctedList[10] = {0};
我将向您展示如何仅为第一个列表创建for循环,然后希望您可以对第二个列表执行相同操作并打印更正的List。这是markedList的for循环:
for ( i = 0; i < 10; i++) {
printf("Enter Marks for Student %d: ", i+1 );
scanf("%d", &marksList[i]);
}