Question

Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

Fix the errors in C code

#include <stdio.h>
#include <stdlib.h>


void insertAt(int *, int);
void Delete(int *);
void replaceAt(int *, int, int);
int isEmpty(int *, int);
int isFull(int *, int);
void removeAt(int *, int);
void printList(int *, int);

int main()
{
int *a;
int arraySize=0,l=0,loc=0;
int choice;


while(1)
{
printf("\n Main Menu");
printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of the list \n 7. Check if the list is empty \n 8. Check if the list is full \n 9. Print the current List \n 10. Exit\n ");
printf("\nEnter your Choice: ");
scanf("%d", &choice);
system("clear") ; //for clearing std. output/console.

switch(choice)
{
case 1: //Creating the list
{
printf("\n Enter the size of list you want to create:");
scanf("%d", &arraySize);
a = (int *)malloc(arraySize*(sizeof(int)));
printf("\nList created.");
break;
}

case 2: //Insert the element at given position
if(a!=NULL)
{
printf("\n Enter the position you want to insert: ");
scanf("%d", &l);
if(l>arraySize) //If user gives position which is out of bound
{
printf("\nInvalid input.\nList only contains %d elements.",arraySize);
break;
}
insertAt(a,l);
printf("\nInserted at location %d",l);
printList(a,arraySize);
break;
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 3: //Delete the list
if(a!=NULL)
{
Delete(a);
break;
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 4: //Remove element at given position
if(a!=NULL)
{
printf("\n Enter the position you want to delete: ");
scanf("%d", &l);
if(l > arraySize) //If user gives position which is out of bound
{
printf("\nInvalid input.\nList only contains %d elements.",arraySize);
break;
}
removeAt(a, l);
break;
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 5: //Replace At
if(a!=NULL)
{
printf("\n Enter the position you want to replace: ");
scanf("%d", &l);
printf("\n Enter the digit to replace with: ");
scanf("%d", &loc);
if(l > arraySize) //If user gives position which is out of bound
{
printf("\nInvalid input.\nList only contains %d elements.",arraySize);
break;
}
replaceAt(a,l,loc);
break;
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 6: //Check size of list
if(a!=NULL)
{
printf("\nSize of list is: %d",arraySize);
break;
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 7: // Check if empty
if(a!=NULL)
{
if(isEmpty(a,arraySize))
{
printf("\nList is empty.");
break;
}
else
{
printf("\nList is not empty.");
break;
}
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 8: // Check if full
if(a!=NULL)
{
if(isFull(a,arraySize))
{
printf("\nList is full.");
break;
}
else
{
printf("\nList is not full.");
break;
}
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 9: //Print list
printList(a, arraySize);
break;

case 10: exit(0);

default: printf("\nInvalid input\nEnter the correct choice.");
}
}
return 0;
}


//Functions


void insertAt(int *a, int l)
{
int b;
printf("\nEnter the no. you want to insert: ");
scanf("%d",&b);
a[l-1]=b;
}

void replaceAt(int *a, int l,int loc)
{
a[l-1]=loc;
}

void Delete(int *a)
{
free(a);
printf("\nList deleted.");
}

int isEmpty(int *a, int l)
{
int i=0;
while(i<l)
{
if(a[i++]!=0) return 0;
}
return 1;
}

int isFull(int *a, int l)
{
int i=0;
while(i<l)
{
if(a[i++]==0) return 0;
}
return 1;
}

void removeAt(int *a, int l)
{
a[l]=0;
printf("\nItem removed.");
}


void printList(int *a, int size)
{
printf("\n The Elements of The list are:");
for(int i=0;i<size;i++)
{
printf("\n%d. %d", i+1,a[i]);
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program After Removing Error. See Summary below about error

#include <stdio.h>
#include <stdlib.h>


void insertAt(int *, int);
void Delete(int *);
void replaceAt(int *, int, int);
int isEmpty(int *, int);
int isFull(int *, int);
void removeAt(int *, int);
void printList(int *, int);

int main()
{
int *a;
int arraySize=0,l=0,loc=0;
int choice;


while(1)
{
printf("\n Main Menu");
printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of the list \n 7. Check if the list is empty \n 8. Check if the list is full \n 9. Print the current List \n 10. Exit\n ");
printf("\nEnter your Choice: ");
scanf("%d", &choice);
system("clear") ; //for clearing std. output/console.

switch(choice)
{
case 1: //Creating the list
{
printf("\n Enter the size of list you want to create:");
scanf("%d", &arraySize);
a = (int *)malloc(arraySize*(sizeof(int)));
printf("\nList created.");
break;
}

case 2: //Insert the element at given position
if(a!=NULL)
{
printf("\n Enter the position you want to insert: ");
scanf("%d", &l);
if(l>arraySize) //If user gives position which is out of bound
{
printf("\nInvalid input.\nList only contains %d elements.",arraySize);
break;
}
insertAt(a,l);
printf("\nInserted at location %d",l);
printList(a,arraySize);
break;
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 3: //Delete the list
if(a!=NULL)
{
Delete(a);
break;
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 4: //Remove element at given position
if(a!=NULL)
{
printf("\n Enter the position you want to delete: ");
scanf("%d", &l);
if(l > arraySize) //If user gives position which is out of bound
{
printf("\nInvalid input.\nList only contains %d elements.",arraySize);
break;
}
removeAt(a, l);
break;
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 5: //Replace At
if(a!=NULL)
{
printf("\n Enter the position you want to replace: ");
scanf("%d", &l);
printf("\n Enter the digit to replace with: ");
scanf("%d", &loc);
if(l > arraySize) //If user gives position which is out of bound
{
printf("\nInvalid input.\nList only contains %d elements.",arraySize);
break;
}
replaceAt(a,l,loc);
break;
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 6: //Check size of list
if(a!=NULL)
{
printf("\nSize of list is: %d",arraySize);
break;
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 7: // Check if empty
if(a!=NULL)
{
if(isEmpty(a,arraySize))
{
printf("\nList is empty.");
break;
}
else
{
printf("\nList is not empty.");
break;
}
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 8: // Check if full
if(a!=NULL)
{
if(isFull(a,arraySize))
{
printf("\nList is full.");
break;
}
else
{
printf("\nList is not full.");
break;
}
}
else
{
printf("\nList doesn't exist.\nFirst create list.");
break;
}

case 9: //Print list
printList(a, arraySize);
break;

case 10: exit(0);

default: printf("\nInvalid input\nEnter the correct choice.");
}
}
return 0;
}


//Functions


void insertAt(int *a, int l)
{
int b;
printf("\nEnter the no. you want to insert: ");
scanf("%d",&b);
a[l-1]=b;
}

void replaceAt(int *a, int l,int loc)
{
a[l-1]=loc;
}

void Delete(int *a)
{
free(a);
printf("\nList deleted.");
}

int isEmpty(int *a, int l)
{
int i=0;
while(i<l)
{
if(a[i++]!=0) return 0;
}
return 1;
}

int isFull(int *a, int l)
{
int i=0;
while(i<l)
{
if(a[i++]==0) return 0;
}
return 1;
}

void removeAt(int *a, int l)
{
a[l]=0;
printf("\nItem removed.");
}


void printList(int *a, int size)
{
printf("\n The Elements of The list are:");
int i = 0;
for(i=0;i<size;i++) // You are getting error here I have just declared outside the loop
{
printf("\n%d. %d", i+1,a[i]);
}
}

NOTE : YOU ARE GETTING ERROR IN PRINTLIST()  FUNCTON I HAVE JUST DECLARED INT I OUTSIDE THE FOR LOOP IT WAS GIVING THE ERROR IN INITIALIZATION.

Add a comment
Know the answer?
Add Answer to:
Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT