Insert integer in middle of pointer string in C - c

I have a string named pos, and I want to insert integer in middle of string. I am new to C.
My code is
char *pos;
int i = 1;
pos = "(%d)",i;
printf("%s",pos);
I get this output (%d)
Whereas I want (1)
Correct me if my approach is wrong.
I know I can directly print it but I want to use the variable in program.
Edit: I have switched from C++, I haven't read much about pointers till yet
The function definition is defined in int program_list(char *programs[],int program_len,int progress[],int progress_len)
by the way my entire program is
#include <stdlib.h> // exit()
#include <stdio.h>
#include <string.h>
#include <conio.h> // clrscr(), getch()
#define width 68
#define array_size(arr) ((sizeof(arr))/(sizeof(*arr)))
void ln();
int program_list(char *programs[],int program_len,int progress[],int progress_len);
void get_elements(int arr[],int size);
void display_elements(int arr[],int size);
int insert_element(int arr[],int *size,int pos,int item);
int delete_element(int arr[],int *size,int pos);
int linear_search(int arr[],int beg,int last,int item);
int binary_search(int arr[],int beg,int last,int item);
void swap(int *a,int *b);
int bubble_sort(int arr[],int size);
int main() {
int item,opt,size,pos,arr[20];
int progress[] = {6,7,11,14,15,16};
char *programs[] = {"GCD of 2 nos. using recursion",
"Fibonacci series using recursion",
"Length of string, concatenate 2 strings using pointer",
"Copy a string, extract substring from string using pointers",
"Tower of Hanoi with 3 discs using recursion",
"Insert integer in array",
"Delete integer from array",
"Create linked list and display it",
"Sort N nos. using insertion sort",
"Sort N nos. using selection sort",
"Sort N nos. using bubble sort",
"Sort N nos. using merge sort",
"Sort N nos. using quick sort",
"Binary Search using recursion",
"Linear Search using recursion",
"EXIT"
};
do{
opt = program_list(programs,array_size(programs),progress,array_size(progress));
switch(opt){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
printf("\nEnter size of array : ");
scanf("%d",&size);
printf("Enter elements of array -:");
get_elements(arr,size);
printf("Elements of array -:");
display_elements(arr,size);
printf("At which index you want to insert? : ");
scanf("%d",&pos);
printf("Enter value which you want to insert : ");
scanf("%d",&item);
*arr = insert_element(arr,&size,pos,item);
printf("Elements of array after insertion -:");
display_elements(arr,size);
break;
case 7:
printf("\nEnter size of array : ");
scanf("%d",&size);
printf("Enter elements of array -:");
get_elements(arr,size);
printf("Elements of array -:");
display_elements(arr,size);
printf("Enter index which you want to delete : ");
scanf("%d",&pos);
*arr = delete_element(arr,&size,pos);
printf("Elements of array after deletion -:");
display_elements(arr,size);
break;
case 8:
break;
case 9:
break;
case 10:
break;
case 11:
printf("\nEnter size of array : ");
scanf("%d",&size);
printf("Enter elements of array -:");
get_elements(arr,size);
printf("Elements of array -:");
display_elements(arr,size);
*arr = bubble_sort(arr,size);
printf("Sorted array using bubble sort -:");
display_elements(arr,size);
break;
case 12:
break;
case 13:
break;
case 14:
printf("\nEnter size of array : ");
scanf("%d",&size);
printf("Enter elements of array -:");
get_elements(arr,size);
printf("Elements of array -:");
display_elements(arr,size);
printf("Enter value which you want to search : ");
scanf("%d",&item);
pos = binary_search(arr,0,size-1,item);
if(pos==-1){
printf("%d not found using binary search\n",item);
}
else{
printf("arr[%d] = %d (found using binary search)\n",pos,item);
}
break;
case 15:
printf("\nEnter size of array : ");
scanf("%d",&size);
printf("Enter elements of array -:");
get_elements(arr,size);
printf("Elements of array -:");
display_elements(arr,size);
printf("Enter value which you want to search : ");
scanf("%d",&item);
pos = linear_search(arr,0,size-1,item);
if(pos==-1){
printf("%d not found using linear search\n",item);
}
else{
printf("arr[%d] = %d (found using linear search)\n",pos,item);
}
break;
case 16:
exit(EXIT_SUCCESS);
break;
default:
printf("Enter valid choice!\n");
opt=0;
}
printf("Program finished! Press enter to restart");
getch(); // use getch() two times for linux based OS (modified by me)
getch();
}while( opt>=0 && opt<=array_size(programs) );
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ln(){
printf("\n");
} // function to leave a line
int program_list(char *programs[],int program_len,int progress[],int progress_len){
clrscr();
int opt,i=0,j,k;
char *text,*status,*pos;
while(i>=0 && i<=2){
switch(i){
case 1:
for(j=0;j<program_len;j++){
for(k=0;k<=width;k++){
if(k==0 || k==width) printf("|");
else{
if(linear_search(progress,0,progress_len,j+1)==-1) status=" ";
else status="->";
pos = "(%d) ",(j+1);
text = programs[j];
printf("%s%s%s",status,pos,text);
k=strlen(status)+strlen(pos)+strlen(text);
while(k<width-1){
printf(" ");
k++;
}
}
}
ln();
}
break;
default:
for(j=0;j<=width;j++){
if(j==0 || j==width) printf("+");
else printf("-");
}
ln();
}
i++;
}
printf("\nYour option : ");
scanf("%d",&opt);
return opt;
} // function to display list of programs in beautiful manner
void get_elements(int arr[],int size){
int i;
ln();
for(i=0;i<size;i++){
printf("arr[%d] -> ",i);
scanf("%d",&arr[i]);
}
ln();
} // function to get elements in array
void display_elements(int arr[],int size){
int i;
ln();
for(i=0;i<size;i++){
printf("arr[%d] = %d\n",i,arr[i]);
}
ln();
} // function to display elements of array
int insert_element(int arr[],int *size,int pos,int item){
if (pos>*size){
printf("OVERFLOW\n");
}
else{
int i;
for(i=*size;i>pos;i--){
arr[i]=arr[i-1];
}
arr[pos] = item;
*size = *size+1;
}
return *arr;
} // function to insert element in array
int delete_element(int arr[],int *size,int pos){
if (pos>*size){
printf("OVERFLOW\n");
}
else{
*size = *size-1;
int i;
for(i=pos;i<*size;i++){
arr[i]=arr[i+1];
}
}
return *arr;
} // function to delete element from array
int linear_search(int arr[],int beg,int last,int item){
if (beg<=last){
if(arr[beg]==item){
return beg;
}
return linear_search(arr,beg+1,last,item);
}
return -1;
} // function to search elements using linear search
int binary_search(int arr[],int beg,int last,int item){
if (beg<=last){
int mid;
mid = (beg+last)/2;
if(arr[mid]==item){
return mid;
}
if(arr[mid]>item){
return binary_search(arr,beg,mid-1,item);
}
return binary_search(arr,mid+1,last,item);
}
return -1;
} // function to search elements using binary search
void swap(int *a,int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
} // function to swap numbers
int bubble_sort(int arr[],int size){
int i=0,j;
while(i<size-1){
j=0;
while(j<size-1-i){
if(arr[j]>arr[j+1]){
swap(&arr[j],&arr[j+1]);
}
j++;
}
i++;
}
return *arr;
} // function to sort array using bubble sorting
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
My output

You don't need the pos variable. Just put what you want in the printf() format string.
To get the length that was printed, use the %n format specifier rather than adding strlen(). See What is the use of the %n format specifier in C?
for(j=0;j<program_len;j++){
for(k=0;k<=width;k++){
if(k==0 || k==width) printf("|");
else{
if(linear_search(progress,0,progress_len,j+1)==-1) status=" ";
else status="->";
text = programs[j];
printf("%s(%d)%s%n",status,j+1,text, &k);
while(k<width-1){
printf(" ");
k++;
}
}
}
ln();
}
But if you really want another variable, declare it as an array, not a pointer. Then use sprintf() to format it.
for(j=0;j<program_len;j++){
for(k=0;k<=width;k++){
if(k==0 || k==width) printf("|");
else{
if(linear_search(progress,0,progress_len,j+1)==-1) status=" ";
else status="->";
text = programs[j];
char pos[20];
sprintf(pos, "(%d)", i+1);
printf("%s%s%s%n",status,pos,text, &k);
while(k<width-1){
printf(" ");
k++;
}
}
}
ln();
}

Related

Segmentation fault in C for Stack implementation using arrays with pointers

I have this code for stack implementation using arrays with pointers which performs lot of operations like push, peep, pop, destroy.
I already done this by declaring globally Stack and Stacktop that time it worked but now I am want to use pointer for this implementation , so here is my code :
#include <stdio.h>
int Full(int Stack[], int *StackTop, int *StackSize){
if (*StackTop == *StackSize-1){
return 1 ;
}
else{
return 0;
}
}
int Empty(int Stack[], int *StackTop){
if (*StackTop == -1){
return 1 ;
}
else{
return 0;
}
}
void PushStack(int ele, int Stack[], int *StackTop, int *StackSize){
if (!Full(Stack, &StackTop, &StackSize)){
Stack[++(*StackTop)]= ele ;
}
else{
printf("Error: Stack is full");
}
}
int PopStack(int Stack[], int *StackTop){
if(!Empty(Stack, &StackTop)){
printf("%d popped !",Stack[*StackTop--]);
}
else{
printf("Error : Stack is Empty");
}
}
void PeepStack(int Stack[], int *StackTop){
if(!Empty(Stack, &StackTop)){
printf("%d", Stack[*StackTop]) ;
}
else{
printf("Error : Stack is Empty");
}
}
int DestroyStack(int Stack[], int *StackTop){
printf("Destroying Stack\n");
if (!Empty(Stack, &StackTop)){
while(!Empty(Stack, &StackTop)){
PopStack(Stack, &StackTop);
printf("\n");
}
}
else{
printf("Stack is already Empty");
}
}
int DisplayStack(int Stack[], int *StackTop){
int i ;
if(Empty(Stack, &StackTop)){
printf("Stack is Empty");
}
else{
printf("Displaying Stack ....\n");
for(i=StackTop; i>=0; --i){
printf("| %d |\n",Stack[i]);
}
}
}
int main(void) {
int StackSize = 5 ;
int Stack[5] ;
int StackTop = -1 ;
int *Top = &StackTop ;
int *Size = &StackSize ;
while(1){
int option, ele ;
printf("\n Options : \n");
printf("1. Push \n");
printf("2. Pop \n");
printf("3. Peep \n");
printf("4. Destroy \n");
printf("5. Display \n");
printf("6. Exit \n");
printf("Enter Option number : ");
scanf("%d", &option);
switch(option){
case 1 :
printf("Enter element you want to push : ");
scanf("%d", &ele);
PushStack(ele,&Stack,&Top, &Size);
break;
case 2 :
PopStack(Stack, &Top);
break;
case 3 :
PeepStack(Stack, &Top);
break;
case 4 :
DestroyStack(Stack, &Top);
printf("Stack Destroyed succesfully !");
break ;
case 5 :
DisplayStack(Stack, &Top);
break;
case 6 :
break ;
default:
printf("Invalid option");
}
printf("\n");
if(option==6){
break;
}
}
return 0;
}
When I execute this on repl.it, I am able to give first input i.e. option number but then it gives me a segmentation fault but when I execute this on codeblocks, I get process returned with some numbers and one hexadecimal code.
So what's wrong in this code ?
because of which line I am getting this error ?
You're using the & operator on a pointer, which makes is a pointer to a pointer.
Basically the pattern you have is this:
void Foo(int *bar)
{
*bar = 123;
}
int main()
{
int thing;
int *p = &thing; // p points to thing
Foo(&p);
printf("%d", &p); // your expected output is: 123
}
But instead of:
Foo(&p);
you want:
Foo(p);
Because p is already a pointer.
But if you want to use thing you need to write:
Foo(&thing);
because &thing points to thing just as p points to thing.

Im trying to put return at the end of the code but they keep messing me up with while before return

Like i want to put the "return 0" at the right place, everything in the code is working normal.
complie the code and they keep saying about "while before return", i tried to put outside of the code and inside
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100
int menu(){
printf("Menu:\n");
printf("1- Add a value\n");
printf("2- Search a value\n");
printf("3- Print out the array\n");
printf("4- Print out values in a range\n");
printf("5- Print out the array in ascending order\n");
printf("6- Quit?\n");
printf("Enter your operation: ");
int choice;
scanf("%d", &choice);
return choice;
}
int isFul (int*a, int n){
return n==MAXN;
}
int isEmpty (int*a, int n){
return n==0;
}
void add(int value, int*a, int*pn){
a[*pn] = value;
(*pn)++;
}
int search(int x, int *a, int n){
int i;
for (i=0; i<n; i++) if (a[i]==x) return i;
return -1;
}
void printvalueinrange (int*a, int n){
int i, min, max;
printf("\nEnter min value: ");scanf("%d", &min);
printf("\nEnter max value: ");scanf("%d", &max);
for(i=0; i<sizeof(a); i++)
if(a[i]>=min&&a[i]<=max) printf("%d", a[i]);
}
void printarray(int*a, int n){
int i;
for (i=0;i<n;i++) printf("%d", a[i]);
}
void printAsc(int*a, int n){
int** adds =(int**)calloc(n, sizeof(int*));
int i,j;
for(i=0; i<n; i++) adds[i]= &a[i];
int* t;
for (i=0;i<n-1; i++)
for(j=n-1; j>i; j--)
if (*adds[j]< *adds[j-i]){
t=adds[j];
adds[j]=adds[j-1];
adds[j-1]=t;
}
for (i=0;i<n; i++) printf("%d ", *adds[i]);
free(adds);
}
int main(){
int a[MAXN];
int n=0;
int value;
int choice;
do
{ choice= menu();
switch(choice){
case 1:{
if (isFull(a,n)) printf("\n Sorry! The array is full.\n");
else
{ printf ("Input an added value:");
scanf("%d", &value);
add(value, a, &n);
printf("Added!\n");
}
break;
}
case 2:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{ printf ("Input the searched value:");
scanf("%d", &value);
int pos = search(value, a, n);
if (pos<0) printf("Not found!\n");
else printf("Postion is found: %d\n", pos);
} break;
}
case 3:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printarray(a,n);
} break;
}
case 4:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printvalueinrange(a,n);
} break;
}
case 5:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printAsc(a,n);
} break;
}
default: printf ("Goodbye!");
break;
}
while (choice>0 && choice<7);
getchar();
}
return 0;
}
I just adding some word to fit with the requirement :"D
And if u find out any kind of error or mistake that in my code, just points out for me to improve them xd
In this part of main
}
while (choice>0 && choice<7);
getchar();
}
return 0;
}
the while construction occupies a wrong place.
There should be
}
} while (choice>0 && choice<7);
getchar();
return 0;
}
Also in this statement
if (isFull(a,n)) printf("\n Sorry! The array is full.\n");
^^^^^^
there is a typo. There should be
if (isFul(a,n)) printf("\n Sorry! The array is full.\n");

unknown error in c. this program not going to void Linear_Search() function

/int arr[n]; int n;/
void Linear_Search() {
int i, q, flag = 0, num, n;
int arr[n];
printf("Enter the number of array\n");
scanf("%d", n);
printf("Enter the numbers from which searched\n");
for (i = 0; i <= n; i++); {
scanf("%d", & arr[i]);
}
printf("enter the number to be searched\n");
scanf("%d", & q);
for (i = 0; i <= n; i++) {
if (q == arr[i]); {
num = q;
flag = 1;
}
}
if (flag == 1) printf("found! number is %d", num);
else printf("number not present in group\n");
getch();
}
void main() {
printf("ALL SEARCHING TECHNIQUE\n");
printf("Choices\n");
printf("1.Linear Search\n2.Binary Search\n3.Interpolation Search\n4.Jump Search\n");
void Linear_Search();
int select, l = 1;
scanf("%d", & select);
switch (select) {
case 1:
printf("This is Linear Search\n");
void Linear_Search();
break;
}
/case 2: printf("This is Binary Search\n"); void Binary_Search(); break; case 3: printf("This is Interpolation Search\n"); void Interpolation_Search(); break; case 4: printf("This is Jump Search\n"); void Jump_Search(); break; } printf("To continue PRESS 1 or PRESS ANY KEY"); scanf("%d",&l);/
getch();
}
You are including the return type of the function at the places where you are trying to call them. This turns them from a function call to a function prototype declaration. Remove the return type (void in these cases) at the places where the function shall be called.

Simple array operation in C

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 i.e 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

Menu driven program in c to perform various operations on an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Following is my c program that performs various operations on an array.When i delete any element the delete_ele function deletes and displays the array correctly, but the traverse function displays some garbage value. Eg. If initially i have 4 elements and delete the first element, the delete_ele function will display the remaining 3 elements but the traverse function displays 4 elements(remaining 3 elements along with a garbage value at the end). Please tell me the error.
#include<stdio.h>
#include<conio.h>
void insert_ele(int i,int ar[],int len){
int ele, loc;
printf("\nEnter element to be inserted : ");
scanf("%d",&ele);
printf("\nEnter location : ");
scanf("%d",&loc);
if(i==19){
printf("\nOverflow");
}
else{
while(i>=loc){
ar[i+1]=ar[i];
i--;
}
ar[loc]=ele;
len++;
printf("\nNew Array : ");
for(i=0;i<len;i++){
printf("\t%d",ar[i]);
}
}
}
void delete_ele(int i,int ar[],int len){
int loc;
printf("\nEnter location of element to be deleted : ");
scanf("%d",&loc);
if(len==0){
printf("\nUnderflow");
}
else{
for(i=loc;i<len;i++){
ar[i]=ar[i+1];
}
len--;
printf("\nNew Array : ");
for(i=0;i<len;i++){
printf("\t%d",ar[i]);
}
}
}
void find_ele(int i,int ar[],int len){
int ele, count=0;
printf("\nEnter element to be searched : ");
scanf("%d",&ele);
for(i=0;i<len;i++){
if(ar[i]==ele){
count++;
}
}
if(count == 0){
printf("\nElement does not exist");
}
else{
printf("\nElement found %d times",count);
}
}
void traverse(int i, int ar[], int len){
printf("\nTotal number of Elements are : %d",len);
printf("\nElements are : ");
for(i=0; i<len; i++){
printf("\t%d",ar[i]);
}
}
void main(){
int ar[20], i=0, len=0, ch;
char choice;
clrscr();
printf("\nEnter number of elements you want to insert : ");
scanf("%d",&len);
for(i=0; i<len; i++){
printf("Enter element %d : ",i);
scanf("%d",&ar[i]);
}
i--;
label:
printf("\nPress 1 to insert, 2 to delete, 3 to find any element");
printf("\n or 4 to traverse the array : ");
scanf("%d",&ch);
switch(ch){
case 1:insert_ele(i, ar, len);
break;
case 2:delete_ele(i, ar, len);
break;
case 3:find_ele(i, ar, len);
break;
case 4:traverse(i, ar, len);
break;
default:printf("\nInvalid Option");
break;
}
printf("\nPress y to continue or any other key to quit : ");
scanf("%s",&choice);
if(choice=='y' || choice=='Y'){
goto label;
}
getch();
}
Because you are decrementing a local variable (len) in delete_ele()
Try:
void delete_ele(int i, int ar[], int *len){
int loc;
printf("\nEnter location of element to be deleted : ");
scanf("%d", &loc);
if(*len == 0){
printf("\nUnderflow");
}
else{
for(i=loc; i< (*len - 1); i++){
ar[i] = ar[i + 1];
}
(*len)--;
printf("\nNew Array : ");
for(i=0; i<*len; i++){
printf("\t%d", ar[i]);
}
}
}
in order to share len.
Same for insert_ele()
The problem is that len in main doesn't get updated by delete_ele and insert_ele.
You pass len to those functions by value. The local copy is then updated inside the respective function, but len in main remains unchanged.
One way to correct the problem is to pass len by reference (as a pointer) to delete_ele and insert_ele.
One bug is: delete_ele() function calling undefined behavior due to array index out of range in for-loop when len value is 20 (read comments):
for(i = loc; i < len; i++){
ar[i] = ar[i + 1];
} // ^^^^^^^ out of index for `i = len - 1`
// max index can be `len - 1` but when `i = len - 1`
// then ar[i + 1] == ar[len] that causes array out-of-index
Should be:
for(i = loc; i < len-1; i++){ # replaced `len` by `len - 1`
ar[i] = ar[i + 1];
}
And looks similar bug you have in other functions too.

Resources