如何在c语言中将数据集的值元素存储到不同的数组中
我有一个复杂的计划 我希望将三个值的数据集存储在不同的数组中,即第一个值存储在第一个数组中,第二个值存储在第二个数组中,第三个值存储在第三个数组中 例如:如何在c语言中将数据集的值元素存储到不同的数组中,c,arrays,C,Arrays,我有一个复杂的计划 我希望将三个值的数据集存储在不同的数组中,即第一个值存储在第一个数组中,第二个值存储在第二个数组中,第三个值存储在第三个数组中 例如: "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 s
"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;
}
#包括
#包括
#定义最大尺寸255
//验证函数
int a_arr[最大尺寸];//声明一个最大大小的数组
int b_arr[最大尺寸];//声明一个最大大小的数组
int c_arr[最大尺寸];//声明一个最大大小的数组
int i;
int checkInput0(无效);
浮动支票输入1(无效);
浮动支票输入2(无效);
浮动支票输入3(无效);
a_arr[]=checkInput1;
b_arr[]=checkInput2;
c_arr[]=checkInput3;
int main()
{
int repeats=0,计数器=0;
//三角形的数量
重复=检查输入0();
//为所有值创建数组
做{
i=计数器;
对于(i=0;imain之前的最后3个声明没有意义
回答这个问题,伪代码很简单。你有3个数组,所以当扫描的迭代器达到i=3n-1,n=0,1,2,3,4…,那么索引就增加了
例如,scanf 0、1和2都保存在各自数组的索引0中(2=3n-1,n为1);scanf 3、4和5都保存在各自数组的索引1中(5=3n-1,n为2),依此类推……我认为您可能已经将此复杂化了。
你可以这样做
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();
}
printf(“多少…”);
scanf(“%d”,&num);//num是用户输入
int i;
对于(i=0;i我认为静态地这样做会浪费大量内存,简单的malloc可以节省大量内存和时间(我指的是执行速度)
printf(“多少…”);
scanf(“%d”,&num);//num是用户输入
int*a_arr=(int*)malloc(num*sizeof(int));
int*b_arr=(int*)malloc(num*sizeof(int));
int*c_arr=(int*)malloc(num*sizeof(int));
整数计数;
对于(count=0;counta_arr[]=checkInput1;
…在文件范围内???您认为a_arr[]=checkInput1;
有什么作用?我想做的是以某种方式将checkInput返回的值存储到a_arr[]中,但找不到checkInput0()
的定义。我忘了添加checkInput(0):这里是int checkInput0(void){float option0,check0;char c;do{printf(“输入要检查的三角形数量:\n”);if(scanf(“%f%c”),&option0,&c)==0 | | c!='\n'){while((check0=getchar())!=0&&check0!='\n'&check0!=EOF);printf([ERR]三角形数量无效。\n”)}{break;}}while(1);//printf(“返回option的值,即%f”,option);返回option0;}似乎找不到使用checkinput方法/函数的任何原因,简单的scanf可能会do@ShreyanMehta是的,我同意。我只是根据OP的代码写了这个答案,希望它能以某种方式使main()
更具可读性。而且这些函数也不会造成伤害。虽然只有一个函数可以完成所有三个函数的工作!i
未初始化。您正在malloc()中使用它
。为什么?首先,i
在你的代码中意味着什么???@aditirawa注意到了,刚刚又编辑了一遍,谢谢你指出这一点
printf("How many...");
scanf("%d", &num); //num is the user input
int * a_arr = (int *)malloc( num * sizeof(int));
int * b_arr = (int *)malloc( num * sizeof(int));
int * c_arr = (int *)malloc( num * sizeof(int));
int count;
for(count=0; count<num; count++)
{
printf("enter first number");
scanf("%d",a_arr[count]);
printf("enter first number");
scanf("%d",b_arr[count]);
printf("enter first number");
scanf("%d",c_arr[count]);
}
//print or whatever you wanna do processing on it