gpt4 book ai didi

c - 如何将数据集的值元素存储到c中的不同数组中

转载 作者:行者123 更新时间:2023-11-30 14:53:47 27 4
gpt4 key购买 nike

我有一个复杂的计划计划。

我希望将三个值的数据集存储在不同的数组中,即第一个值存储在第一个数组中,第二个值存储在第二个数组中,第三个值存储在第三个数组中。

例如:

"How often would you like to repeat the program"
2
"Enter the first value:"
1
"Enter the second value:"
2
"Enter the third value:"
3
"Enter the first value:"
4
"Enter the second value:"
5
"Enter the third value:"
6

输出应该是这样的

a_arr = [1 4]
b_arr = [2 5]
c_arr = [3 6]

我已经想出了这段代码,但我似乎无法让它工作。

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 255
//validation function
int a_arr[MAX_SIZE]; // Declare an array of MAX_SIZE
int b_arr[MAX_SIZE]; // Declare an array of MAX_SIZE
int c_arr[MAX_SIZE]; // Declare an array of MAX_SIZE
int i;
int checkInput0(void);
float checkInput1(void);
float checkInput2(void);
float checkInput3(void);
a_arr[] = checkInput1;
b_arr[] = checkInput2;
c_arr[] = checkInput3;

int main()
{
int repeats = 0, counter = 0;
//Amount of triangles
repeats = checkInput0();

// create arrays for all values
do {
i = counter;
for(i=0; i<repeats; i++)
{
scanf("%f", &a_arr[i]);
}
for(i=0; i<repeats; i++)
{
scanf("%f", &b_arr[i]);
}
for(i=0; i<repeats; i++)
{
scanf("%f", &c_arr[i]);
}

counter++;
}while(counter < repeats);

do {
printf("%f\n",a_arr );
printf("%f\n",b_arr );
printf("%f\n",c_arr );
counter++;
}while(counter < repeats);

return 0;
}

// Validate Value of a
float checkInput1(void){
float option1,check1;
char c;

do{
printf("Enter the first side of the triangle");

if(scanf("%f%c",&option1,&c) == 0 || c != '\n') {
while((check1 = getchar()) != 0 && check1 != '\n' && check1 != EOF);
printf("\t[ERR] Invalid number for the triplet.\n");
}else {
break;
}
}while(1);
// printf("returning the value of option, which is %f", option);
return option1;
}

// Validate Value of b
float checkInput2(void){
float option2,check2;
char c;

do{
printf("Enter the second side of the triangle");

if(scanf("%f%c",&option2,&c) == 0 || c != '\n'){
while((check2 = getchar()) != 0 && check2 != '\n' && check2 != EOF);
printf("\t[ERR] Invalid number for the triplet.\n");
}else{
break;
}
}while(1);
//printf("returning the value of option, which is %f", option2);
return option2;
}

// Validate Value of c
float checkInput3(void){
float option3,check3;
char c;

do{
printf("Enter the third side of the triangle");

if(scanf("%f%c",&option3,&c) == 0 || c != '\n'){
while((check3 = getchar()) != 0 && check3 != '\n' && check3 != EOF);
printf("\t[ERR] Invalid number for the triplet.\n");
}else{
break;
}
}while(1);
// printf("returning the value of option, which is %f", option);
return option3;
}

注意:这些函数用于验证,以便输入是数字。

非常感谢所有帮助

最佳答案

我认为你可能把这个问题复杂化了。你可以做这样的事情。

printf("How many...");
scanf("%d", &num); //num is the user input

int i;
for(i=0; i<num; i++)
{
printf("enter first number");
a_arr[i] = checkInput1();
printf("enter first number");
b_arr[i] = checkInput2();
printf("enter first number");
c_arr[i] = checkInput3();
}

关于c - 如何将数据集的值元素存储到c中的不同数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47032103/

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