gpt4 book ai didi

c - 根据第二个数组数据打印第一个数组数据的次数

转载 作者:太空宇宙 更新时间:2023-11-04 08:48:48 24 4
gpt4 key购买 nike

抱歉,标题结构不佳,想不出如何表达。

无论如何,当涉及到 C 中的数组和循环时,我遇到了一个问题。我需要让用户输入 5 个字母并将它们存储在第一个数组中。然后我需要他们输入 1 到 10 之间的 5 个数字并将它们存储在第二个数组中。然后我需要根据第二个数组中输入的相应数字打印第一个数组的数据次数。

例如,如果输入第一个数组的第一个字母是A,输入第二个数组的第一个数字是4,我需要打印出AAAA。

我知道我需要在某处使用 for 循环,我确信这会比 while 循环更好,因为我会知道循环中使用的所有数据。

到目前为止,这是我的代码,虽然不多,但我讨厌问一个连代码都没有的问题。

干杯!

#include <stdio.h>
#include <stdlib.h>

int main() {

char input1[5];
int input2[5], i;

printf("Please enter a letter to be stored in the first array: \n");
scanf(" %c", &input1[0]);
printf("Please enter another letter to be stored in the first array: \n");
scanf(" %c", &input1[1]);
printf("Please enter another letter to be stored in the first array: \n");
scanf(" %c", &input1[2]);
printf("Please enter another letter to be stored in the first array: \n");
scanf(" %c", &input1[3]);
printf("Please enter a final letter to be stored in the first array: \n");
scanf(" %c", &input1[4]);
printf("The first array contains these values: %c, %c, %c, %c and %c. \n", input1[0], input1[1], input1[2], input1[3], input1[4]);

printf("Please enter five numbers between 1 and 10 to be stored in a second array: \n");
scanf("%d", &input2[0]);
printf("Please enter five numbers between 1 and 10 to be stored in a second array: \n");
scanf("%d", &input2[1]);
printf("Please enter five numbers between 1 and 10 to be stored in a second array: \n");
scanf("%d", &input2[2]);
printf("Please enter five numbers between 1 and 10 to be stored in a second array: \n");
scanf("%d", &input2[3]);
printf("Please enter five numbers between 1 and 10 to be stored in a second array: \n");
scanf("%d", &input2[4]);
printf("The second array contains these values: %d, %d, %d, %d and %d \n", input2[0], input2[1], input2[2], input2[3], input26[4]);

最佳答案

for(i=0;i<5;i++){
for(j=0;j<input2[i];j++){
printf("%c",input1[i]);
}
}

关于c - 根据第二个数组数据打印第一个数组数据的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20467600/

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