[英]I am making a Menu driven program in which i have to create, delete and insert an array
[英]Can anyone tell me why my program crashes when I try to call the Insert or Delete function in a menu driven array manipulation program?
我正在嘗試編寫一個菜單驅動程序來執行諸如創建、顯示、插入和刪除數組中的元素之類的操作。 create 和 Display 函數工作得很好,但是每當我嘗試調用插入或刪除函數時,程序都會崩潰並且消息讀取進程返回。 我在下面分享了我的代碼。 我意識到 create 功能並不理想,但我剛剛被告知要這樣做。 如果相關,我正在使用 Code::Blocks 20.03。
#include <stdio.h>
#define MAX 50
void create(int[],int);
void display(int[],int);
void insert(int[],int*,int,int);
void deletes(int[],int*,int);
int main()
{
int ch,n,a[MAX],ele,pos;
while(1)
{
printf("\nChoose an operation\n");
printf("1. Create\n2. Display\n3. Insert\n4. Delete\n5. Exit\n\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the no. of elements\n");
scanf("%d",&n);
create(a, n);
break;
case 2: display(a, n);
break;
case 3: printf("Enter the position at which you want to insert the element\n");
scanf("%d",&pos);
printf("Enter the element to be inserted\n");
scanf("%d",&ele);
insert(a, &n, pos, ele);
break;
case 4: printf("Enter the position of the element to be deleted\n");
scanf("%d",&pos);
deletes(a,&n,pos);
break;
case 5: exit(0);
default: printf("Invalid Input");
break;
}
}
}
void create(int a[], int n)
{
int temp;
printf("Please enter the elements\n");
for(int i=0; i<n ; i++)
{
scanf("%d",&temp);
a[i] = temp;
}
}
void display(int a[], int n)
{
printf("The array is \n");
for(int i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}
void insert(int a[], int*n, int pos, int ele)
{
if(n==MAX)
{
printf("Array Overflow. Cannot Insert element\n");
}
else if(pos>=0 && pos<=n)
{
for(int i=n-1; i>=pos; i--)
{
a[i+1] = a[i];
}
a[pos] = ele;
n++;
printf("Element inserted successfully\n");
}
else
{
printf("Enter a valid position\n");
}
}
void deletes(int *a,int*n, int pos)
{
if(pos<=n)
{
for(int i = pos-1; i<n;i++)
{
a[i] = a[i+1];
}
n--;
printf("The element has been deleted\n");
}
else
printf("Invalid position");
}
感謝您的幫助!
在您的insert
和deletes
函數中,您不會取消引用作為參數給出的指針 ( n
)。 您需要將*
運算符添加到此變量中以獲取或設置n
計數器的實際值。
對於insert
函數,更正后的代碼為:
void insert(int a[], int*n, int pos, int ele) // NOTE: "n" is a POINTER!
{
if(*n==MAX) // Dereference n here
{
printf("Array Overflow. Cannot Insert element\n");
}
else if(pos>=0 && pos<=*n) // ... and here
{
for(int i=*n-1; i>=pos; i--) // ... and here
{
a[i+1] = a[i];
}
a[pos] = ele;
(*n)++; // Note the brackets here - otherwise we increment the pointer!
printf("Element inserted successfully\n");
}
else
{
printf("Enter a valid position\n");
}
}
deletes
功能所需的更改非常相似。
發現問題, n
是一個 simpel int 不是數組,不需要將它作為地址發送,所以我在 switch case 中做了什么:
case 3: printf("Enter the position at which you want to insert the element\n");
scanf("%d", &pos);
printf("Enter the element to be inserted\n");
scanf("%d", &ele);
insert(a, n, pos, ele);
break;
case 4: printf("Enter the position of the element to be deleted\n");
scanf("%d", &pos);
deletes(a, n, pos);
break;
現在的功能看起來像這樣:
void insert(int a[], int n, int pos, int ele)
void deletes(int* a, int n, int pos)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.