所以我试图编写一个程序,提示用户输入用户希望拥有多少数据集,也就是将有多少数组然后它会提示用户输入每个数据集中有多少个值以及值是什么最后,它为用户提供了在所需数据集上运行的选项列表。
当我运行代码并选择要使用的数据集时,它似乎总是会产生最后一个数据集,并且似乎没有集合中的所有值我只是想知道是否有人能让我知道我做错了什么,或者至少让我走上正轨我已经一遍又一遍地检查了代码,但还是搞不清楚。
#include <stdio.h>
int main()
{
unsigned short int num_sets, set_size, set_desired, command = 0;
printf("Enter the number of data sets you would like to store: ");
scanf(" %hu", &num_sets);
int i = 1, j, sets[1][num_sets], sum, a;
while(i <= num_sets)
{
j = 1;
printf("Enter the number of elements in data set %hu: ", i);
scanf(" %hu", &set_size);
printf("Enter the data for set %hu: ", i);
while(j < set_size)
{
scanf(" %d", &sets[i - 1][j - 1]);
j++;
}
i++;
}
printf("Which set would you like to use?: ");
scanf(" %hu", &set_desired);
while(set_desired > num_sets){
printf("There aren't that many data sets, try again: ");
scanf(" %hu", &set_desired);
}
printf("Set #%hu: %hu\n", num_sets, *sets[num_sets - 1]);
while(command != 7){
printf("Choose what you would like to do:\n");
printf("1. Find the minimum value.\n");
printf("2. Find the maximum value.\n");
printf("3. Calculate the sum of all the values.\n");
printf("4. Calculate the average of all the values.\n");
printf("5. Sort the values in ascending order.\n");
printf("6. Select a different data set.\n");
printf("7. Exit the program.\n");
scanf(" %hu", &command);
if(command == 1){
printf("You picked 1!");
}
if(command == 2){
printf("You picked 2!");
}
if(command == 3){
/*printf("You picked 3!");
for(a = 0; a < set_size; a++){
sum = sum + *sets[a];
}
printf("%d\n", sum);*/
printf("You picked 3!");
}
if(command == 4){
printf("You picked 4!");
}
if(command == 5){
printf("You picked 5!");
}
if(command == 6){
printf("You picked 6!");
}
if(command == 7){
break;
}
}
}
最佳答案
If you are trying to store values in different sets than you need to maintain the number of items in each set separately as you don't know how many elements are there is each set.
The design should be such that:
Set 1 : Number of Elements : Actual values in the array.(Here I am storing the number of items in the set as part of the same array. It will be the first element in each set)
The memory allocation can be done dynamically as you are giving the option to the user to set the number of items per set.
#include <stdio.h>
#include<stdlib.h>
int main()
{
unsigned short int num_sets, set_size, set_desired, command = 0;
printf("Enter the number of data sets you would like to store: \n");
scanf(" %hu", &num_sets);
int *sets[num_sets];
int i = 0, k=0,j,sum, a;
while(i < num_sets)
{
j = 1;
printf("Enter the number of elements in data set %hu: \n", i+1);
scanf(" %hu", &set_size);
sets[i] = (int *) malloc((sizeof(int)*set_size));
*sets[i] = set_size;
printf("Enter the values for set %hu\n", i+1);
while(j <= set_size)
{
scanf(" %d", &sets[i][j]);
j++;
}
i++;
}
printf("Which set would you like to use?: \n");
scanf(" %hu", &set_desired);
while(set_desired > num_sets){
printf("There aren't that many data sets, try again: \n");
scanf(" %hu", &set_desired);
}
for(k=1;k<=(*sets[set_desired-1]);k++)
{
printf("Set #%hu: %hu \n", set_desired, *(sets[set_desired-1] + k));
}
while(command != 7){
printf("Choose what you would like to do:\n");
printf("1. Find the minimum value.\n");
printf("2. Find the maximum value.\n");
printf("3. Calculate the sum of all the values.\n");
printf("4. Calculate the average of all the values.\n");
printf("5. Sort the values in ascending order.\n");
printf("6. Select a different data set.\n");
printf("7. Exit the program.\n");
scanf(" %hu", &command);
if(command == 1){
printf("You picked 1!");
}
if(command == 2){
printf("You picked 2!");
}
if(command == 3){
/*printf("You picked 3!");
* for(a = 0; a < set_size; a++){
* sum = sum + *sets[a];
* }
* printf("%d\n", sum);*/
printf("You picked 3!");
}
if(command == 4){
printf("You picked 4!");
}
if(command == 5){
printf("You picked 5!");
}
if(command == 6){
printf("You picked 6!");
}
if(command == 7){
break;
}
}
return 0;
}
关于c - C将数字列表读入多个数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26029514/