所以我正在尝试编写一个程序,提示用户输入用户希望拥有的数据集,即将要有多少数组。然后,它会提示用户输入每个数据集中的值以及值。最后,它为用户提供了在所需数据集上运行的选项列表。
当我运行我的代码并选择我想要使用的数据集时,似乎总是想出最后一个数据集并且似乎没有集合中的所有值。我只是想知道是否有人能让我知道我做错了什么,或者至少让我走上正轨。我已经多次浏览过代码,无法解决问题。
#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;
}
}
}
答案 0 :(得分:0)
您的代码中存在未定义的行为。在这一行:
int i = 1, j, sets[1][num_sets], sum, a;
为每个num_sets
数据集中的一个元素分配足够的空间。这不是你真正需要的。您可能希望num_sets
作为第一个索引,而另一个索引则需要更大的值。
您可能需要进行动态内存分配。如果做不到这一点,你需要设置数组大小的上限(可能是100),并拒绝创建更大数组的尝试。然后你可以使用:
enum { MAX_ARR_SIZE = 100 };
int sets[num_sets][MAX_ARR_SIZE];
表示变量。您的数据输入循环已经采用了这种索引方法。您可能需要记录每行中每行的条目数,以避免使用未初始化的数据。
int set_size[num_sets];
答案 1 :(得分:0)
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;
}