使用用户定义的数组大小 returns 意外结果查找数组的众数的 C 程序
C program to find the Majority Element of Array using a user defined array size returns unexpected results
我正在尝试编写一个 c 程序,提示用户输入 1 到 100 之间的数组 (N) 的大小。然后提示用户输入大小为 N 的数组元素。之后我想向用户显示上述元素,但是,在 运行 时间内随机显示,或者显示似乎是随机数的内容。我不确定这是否是由于将 malloc 用于用户定义的数组大小或其他原因。如果您对问题可能有任何见解,我将不胜感激。
节目:
int main()
{
int N;
int dW;
int *inputArray;
int counter;
do {
// Prompts user to input array size up to 100
printf("\nEnter the value of N up to 100: \n");
scanf("%d", &N);
// Uses pointer "inputArray" to pacify compiler and allocates size of N in memory for array
inputArray = (int *)malloc(N * sizeof(int));
// Checks if temp is greater than 1 and less than 100, if yes prompts user to reenter N
if (N >= 1 & N <= 100)
{
// Prompts user to input array numbers to the size of N
for (counter = 0; counter < N; counter++)
{
printf("\nEnter the element %d: \n", counter);
scanf("%d", &inputArray[counter]);
}
// displays numbers provided by user
for (counter = 0; counter < N; counter++);
{
printf("%d\n", inputArray[counter]);
}
dW = 0;
}
else
{
printf("\nIllegal Entry, enter a value between 1 and 100\n");
dW = 1;
}
} while (dW >= 1);
return 0;
}
输出:
Enter the value of N up to 100:
5
Enter the element 0:
1
Enter the element 1:
2
Enter the element 2:
3
Enter the element 3:
4
Enter the element 4:
5
00200000
您的代码存在一些问题:
您应该将 inputArray = (int *)malloc(N * sizeof(int));
移到 if 块中,以便您仅在进入该块时分配内存(并且 N
保证在 1 到 100 之间) .此外,无需将 malloc()
转换为 (int *)
.
您的第二个 for 循环末尾有一个分号:for (counter = 0; counter < N; counter++);
。如果你把它留在那里,for 循环将递增 counter
直到 counter == N
,然后 printf("%d\n", inputArray[counter]);
将在之后被调用,但只有一次并且索引错误(因为 counter == N
, 这会让你出界)。
你用完后忘记了free(inputArray)
。您可以在 dW = 0;
.
之后立即执行此操作
您的问题在这一行:
for (counter = 0; counter < N; counter++);
看到那个分号了吗?这实质上导致此行执行与以下内容相同的操作:
for (counter = 0; counter < N; counter++) { /* do nothing */ }
你的 printf
块然后运行,但只运行一次,并且计数器等于 N
,所以你读取恰好在内存中恰好超过 [=14] 末尾的任何随机数据=] 在堆中。
解决方案:去掉那个分号。
我正在尝试编写一个 c 程序,提示用户输入 1 到 100 之间的数组 (N) 的大小。然后提示用户输入大小为 N 的数组元素。之后我想向用户显示上述元素,但是,在 运行 时间内随机显示,或者显示似乎是随机数的内容。我不确定这是否是由于将 malloc 用于用户定义的数组大小或其他原因。如果您对问题可能有任何见解,我将不胜感激。
节目:
int main()
{
int N;
int dW;
int *inputArray;
int counter;
do {
// Prompts user to input array size up to 100
printf("\nEnter the value of N up to 100: \n");
scanf("%d", &N);
// Uses pointer "inputArray" to pacify compiler and allocates size of N in memory for array
inputArray = (int *)malloc(N * sizeof(int));
// Checks if temp is greater than 1 and less than 100, if yes prompts user to reenter N
if (N >= 1 & N <= 100)
{
// Prompts user to input array numbers to the size of N
for (counter = 0; counter < N; counter++)
{
printf("\nEnter the element %d: \n", counter);
scanf("%d", &inputArray[counter]);
}
// displays numbers provided by user
for (counter = 0; counter < N; counter++);
{
printf("%d\n", inputArray[counter]);
}
dW = 0;
}
else
{
printf("\nIllegal Entry, enter a value between 1 and 100\n");
dW = 1;
}
} while (dW >= 1);
return 0;
}
输出:
Enter the value of N up to 100: 5
Enter the element 0: 1
Enter the element 1: 2
Enter the element 2: 3
Enter the element 3: 4
Enter the element 4: 5
00200000
您的代码存在一些问题:
您应该将
inputArray = (int *)malloc(N * sizeof(int));
移到 if 块中,以便您仅在进入该块时分配内存(并且N
保证在 1 到 100 之间) .此外,无需将malloc()
转换为(int *)
.您的第二个 for 循环末尾有一个分号:
for (counter = 0; counter < N; counter++);
。如果你把它留在那里,for 循环将递增counter
直到counter == N
,然后printf("%d\n", inputArray[counter]);
将在之后被调用,但只有一次并且索引错误(因为counter == N
, 这会让你出界)。你用完后忘记了
free(inputArray)
。您可以在dW = 0;
. 之后立即执行此操作
您的问题在这一行:
for (counter = 0; counter < N; counter++);
看到那个分号了吗?这实质上导致此行执行与以下内容相同的操作:
for (counter = 0; counter < N; counter++) { /* do nothing */ }
你的 printf
块然后运行,但只运行一次,并且计数器等于 N
,所以你读取恰好在内存中恰好超过 [=14] 末尾的任何随机数据=] 在堆中。
解决方案:去掉那个分号。