I have just completed a simple array operation in C. I have done the programming consisting of a number of functions all performing the insert operation at various locations. The main problem that I'm facing is that the control is not returning to main after I have performed the operation with a function . I can't find the reason. Just explain why is this happening and provide me with a proper solution. My code is like this:
//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]);
}
}
PROBLEM 1 :
you should have done :
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);
otherwise , i
will always contain the same value ie among 1 , 2, 3, 4
, and while(i==1 || i==2 || i==3 || i==4);
will always be true , and so same function will be called indefinitely.
PROBLEM 2 : have a check on l
, so that it do not exceed 100
, size of your array .
What you think?
//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]);
}
}
I think below line is causing problem
for(i=l-1;i>=0;i--)
{a[i+1]=a[i];
}
It's like when i becomes 0 it will execute and then i-- will cause it to be -1 and negative numbers are stored in 2's complement so it will be positive number and condition i>=0 is satisfied every time.
Hope this might help you
Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.