我刚刚在C中完成了一个简单的数组操作。我完成了由许多函数组成的编程,这些函数都在不同的位置执行插入操作。我面临的主要问题是在用函数执行操作后控件没有返回到main。我找不到原因。只是解释为什么会发生这种情况并为我提供适当的解决方案。我的代码是这样的:
//To insert an element into an already formed array
#include<stdio.h>
#include<stdlib.h>
int binsert(int[],int);
int minsert(int[],int,int);
int einsert(int[],int);
void print(int[],int);
void main()
{
int a[100],i,j,l,loc;
printf("Enter the length of the array\n");
scanf("%d",&l);
printf("Enter the numbers in the array\n");
for(j=0;j<l;j++)
{
scanf("%d",&a[j]);
}
printf("Enter the following options for the operation\n1:insert at the beginning\n2:insert at specific location\n3:insert at the end\n4:print the array\nAny other key to end the operation\n");
scanf("%d",&i);
do
{
switch(i)
{
case 1:
l=binsert(a,l);
break;
case 2:
printf("Enter the location at which you would want to insert(location starts from 1)\n");
scanf("%d",&loc);
l=minsert(a,loc,l);
break;
case 3:
l=einsert(a,l);
break;
case 4:
print(a,l);
break;
}
}while(i==1 || i==2 || i==3 || i==4);
printf("Operation terminated\n");
exit(0);
}
int binsert(int a[],int l)
{
int i;
for(i=l-1;i>=0;i--)
{
a[i+1]=a[i];
}
printf("Enter the number to insert\n");
scanf("%d",&a[0]);
return (l+1);
}
int minsert(int a[],int loc,int l)
{
int i;
for(i=l-1;i>=loc;i--)
{
a[i+1]=a[i];
}
printf("Enter the number to insert\n");
scanf("%d",&a[loc]);
return (l+1);
}
int einsert(int a[],int l)
{
printf("Enter the number to insert\n");
scanf("%d",&a[l+1]);
return (l+1);
}
void print(int a[],int l)
{
int i;
printf("The values in the array are as follows\n");
for(i=0;i<l;i++)
{
printf("%d\n",a[i]);
}
}
答案 0 :(得分:1)
问题1 :
你应该做的:
do
{
printf("Enter the following options for the operation\n1:insert at the beginning\n2:insert at specific location\n3:insert at the end\n4:print the array\nAny other key to end the operation\n");
scanf("%d",&i);
switch(i)
{
....
}
}while(i==1 || i==2 || i==3 || i==4);
否则,i
将始终包含相同的值,即1 , 2, 3, 4
,while(i==1 || i==2 || i==3 || i==4);
将始终为真,因此将无限期地调用相同的函数。
问题2 :对l
进行检查,确保其数量不超过100
。
答案 1 :(得分:1)
你的想法是什么?
//To insert an element into an already formed array
#include<stdio.h>
#include<stdlib.h>
int binsert(int[],int);
int minsert(int[],int,int);
int einsert(int[],int);
void print(int[],int);
void main()
{
int a[100],i,j,l,loc;
for(i=0; i < 100; i++)
a[i] = -1;
printf("Enter the length of the array\n");
scanf("%d%*c",&l);
for(j=0; j<l; j++)
{
system("cls");
printf("Enter the numbers in the array\n");
for(i = 0; i < j; i++)
printf("%d ", a[i]);
scanf("%d%*c",&a[j]);
}
do
{
system("cls");
printf("Array: ");
for(j = 0; j < (sizeof(a)/sizeof(a[0])) && a[j] != -1; j++)
printf("%d ", a[j]);
printf("\n\n\nEnter the following options for the operation\n\n\t1:insert at the beginning\n\t2:insert at specific location\n\t3:insert at the end\n\t4:print the array\n\tAny other key to end the operation\n\nOption: ");
scanf("%d%*c",&i);
switch(i)
{
case 1:
l=binsert(a,l);
break;
case 2:
printf("Enter the location at which you would want to insert(location starts from 1)\n");
scanf("%d",&loc);
l=minsert(a,loc,l);
break;
case 3:
l=einsert(a,l);
break;
case 4:
print(a,l);
break;
}
}
while(i==1 || i==2 || i==3 || i==4);
printf("\n\nOperation terminated\n");
getchar();
exit(0);
}
int binsert(int a[],int l)
{
int i;
for(i=l-1; i>=0; i--)
{
a[i+1]=a[i];
}
printf("\n\nEnter the number to insert\n");
scanf("%d",&a[0]);
return (l+1);
}
int minsert(int a[],int loc,int l)
{
int i;
for(i=l-1; i>=loc; i--)
{
a[i+1]=a[i];
}
printf("\n\nEnter the number to insert\n");
scanf("%d",&a[loc]);
return (l+1);
}
int einsert(int a[],int l)
{
printf("\n\nEnter the number to insert\n");
scanf("%d",&a[l+1]);
return (l+1);
}
void print(int a[],int l)
{
int i;
printf("\n\nThe values in the array are as follows\n");
for(i=0; i<l; i++)
{
printf("%d\n",a[i]);
}
}
答案 2 :(得分:-2)
我认为以下行引起了问题
for(i=l-1;i>=0;i--)
{a[i+1]=a[i];
}
就像当我变为0时它将执行然后i--将使它为-1并且负数存储在2的补码中,因此它将是正数且条件i> = 0每次都满足。< / p>
希望这可以帮到你
由于