【问题标题】:Code crashes when reading numbers into an array... C language将数字读入数组时代码崩溃... C语言
【发布时间】:2013-11-27 20:40:23
【问题描述】:

嘿,当我开始向数组输入数据时,我的代码崩溃了。该程序应该将数字读入一个数组,然后将一个新数字插入到数组中,最后它按升序排列所有内容。我不确定它有什么问题。有人有建议吗?

这是我的代码

#include <stdlib.h>
#include <stdio.h>
#define MAX 10000

int main() // Main
{

printf ("COP2220 - Project 5 - Manuel Ubau\n\n"); //Program Starts and Explains

printf ("This program inserts a number into an array in order of magnitude\n\n");

//Local Declarations
int i,j;
int n; //size of array
int x;
int arr[MAX]; //the arrays maximun 100 numbers
int pos;


printf("Please enter the size of the array: "); //Asks for size of array

scanf("%d",&n); //Reads size of array

   for(i=0;i<n;i++) // Reads values of the array
   {
    printf("Enter number for element #%d: ",i+1);
    scanf("%d",&arr[i]);
   }

printf("\nThe Original Array is:");  //Prints original Array for reference

    for(i=0;i<n;i++)
    {
      printf("\t%d",arr[i]);
    }


printf("\n\nEnter the value you want to insert into the arary: "); //Asks for X
scanf("%d",&x); //Reads number

     for(i=0;i<n;i++) // Determines position of the new number
     if(x<arr[i])
     {
       pos =i;
       break;
     }

     for(i=n;i>=pos;i--) //Displaces the array 1 space to the left so new number 
     arr[i]= arr[i-1];
     arr[pos]=x;       //Inserts the number into "pos" defined before

     for(i=0;i<=n;i++) // Arranges array
     {
      for(j=i;j<=n;j++)
         {
           if(arr[i] > arr[j])
             {
              int temp=arr[i];
              arr[i]=arr[j];
              arr[j]=temp;
             }
         }
      }

 printf("\nThe final array is:"); //Prints New array
   for(i=0;i<=n;i++)
   {
      printf("\t%d",arr[i]);
   }

printf("\n\nThanks!\n");
return 0; //Program Ends
}

提前致谢。哦,顺便说一句,程序不会在任何特定的输入数字中崩溃。这似乎更像是一次随机崩溃。

编辑:当我输入我键入的随机数时会发生崩溃。但如果我按顺序执行,它不会崩溃。例如,如果我做一个大小为 10 的数组,它的值为 1 2 3...10 它工作得很好,但是一旦我使用像 100 456 54... 等随机数,有时它会崩溃,有时它会工作。我还没有确定一个正确的顺序让它崩溃。并且没有输出,程序自动关闭并且不让我查看它是否打印了其他内容

【问题讨论】:

  • 它会给你一个错误信息吗?
  • 您能否格式化您的代码,使其更具可读性(尤其是缩进)?
  • 没有错误...它只是关闭循环并结束程序。我会尝试格式化它。这是我第一次在这里发帖抱歉...
  • 它在什么时候停止?你能指出哪条线吗?当它停止时你使用什么输入?它是否提供任何输出?
  • 当我开始输入进入原始数组的数字时它会停止。将数字扫描到数组中的 for 循环

标签: c arrays crash


【解决方案1】:

当您要求插入一个值时,当您设置一个大于任何其他值的值时,它会崩溃,因为pos 未初始化并且它包含一个随机值(实际上在这种情况下x&lt;arr[i] 永远不会满足)。 您只需要在继续之前正确初始化 pos(位置 n 是下一个可用的位置)

pos = n;
for(i=0;i<n;i++) { // Determines position of the new number
   if(x<arr[i])
   {
     pos =i;
     break;
   }
}

请注意,您还应该检查用户在与向量大小相关的问题中写入少于 10000(最大 99,因为您要求在之后添加一个数字)作为向量的大小,否则您将在那里出现缓冲区溢出

另一个错误在循环中。假设 pos=0 是正确的 pos。

for(i=n;i>=pos;i--) //Displaces the array 1 space to the left so new number 
   arr[i]= arr[i-1]; //pos (Copy pos-1)

如果 pos 等于 0,您尝试复制导致崩溃的 arr[0]=arr[-1]。您需要按如下方式执行循环:

for(i=n;i>pos;i--) //Displaces the array 1 space to the left so new number 
   arr[i]= arr[i-1]; //pos (Copy pos-1)

这样最后一个副本是安全的 (arr[1]=arr[0])。

【讨论】:

  • 我进行了您指示的更改,但它仍然崩溃......当我输入我输入的随机数时发生崩溃。但如果我按顺序执行,它不会崩溃。例如,如果我做一个大小为 10 的数组,它的值为 1 2 3...10 它工作得很好,但是一旦我使用像 100 456 54... 等随机数,有时它会崩溃,有时它会工作。我还没有确定一个正确的顺序让它崩溃。
  • @user3043425 例如,当您设置 1to10 然后添加 12 时,该修复程序有效,在这种情况下它也会崩溃,我尝试使用任何序列,但我无法在其他条件下重现错误。我现在看看算法
  • @user3043425 没有其他可能导致崩溃的错误。所有其他变量似乎都已正确分配和使用。
  • 我找到了可以让它崩溃的数字。数组大小为 25。输入为 43 23 12 65 43 67,然后在元素 #7 处崩溃
  • @user3043425 这是最终修复吗?
猜你喜欢
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-07
  • 1970-01-01
相关资源
最近更新 更多