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
  • C LANGUAGE I just need the void push() function, which inserts new node to the front...

    C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • #include<stdio.h> #include <stdlib.h> void read(struct Employee *e); struct Employee { int id; int age; }; int...

    #include<stdio.h> #include <stdlib.h> void read(struct Employee *e); struct Employee { int id; int age; }; int main(){ struct Employee e; read(&e); } void read(struct Employee *e){ int a,b; printf("Enter the id employee\n"); scanf("%d",&a); printf("Enter the age employee\n"); scanf("%d",&b); e->id=a; e->age=b; } Question: Declare a pointer variable of type Employee and place the address of the variable created in the above problem in that pointer variable.

  • C PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct...

    C PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct nodet *makeAnode(int val) { struct nodet *box; box = malloc(sizeof(struct nodet) ); box->data = val; box->link = NULL; return box; } void printList(struct nodet *L) { struct nodet = *mov; mov = L; while(mov != NULL) { printf("%d ", mov->data); mov = mov->link; } printf("\n"); } // THIS SHOULD COUNT HOW MANY ITEMS (NODES) ARE IN THE LIST. int listLen(struct nodet **L) { int...

  • Convert the below C code to basic MIPS. Please leave comments for explanation #include <stdio.h> int main(void) {...

    Convert the below C code to basic MIPS. Please leave comments for explanation #include <stdio.h> int main(void) { printf("Insert two numbers\n"); int a,b; scanf("%d",&a); scanf("%d",&b); a=a<<2; b=b<<2; printf("%d&%d\n",a,b); return 0; }

  • /* I want to fix void make_flight(int counter, flight_t flights[]) Enter flight code> Enter departure info...

    /* I want to fix void make_flight(int counter, flight_t flights[]) Enter flight code> Enter departure info for the flight leaving SYD. Enter month, date, hour and minute separated by spaces> Enter arrival city code> Enter arrival info. Enter month, date, hour and minute separated by spaces> It should do all of this: 1-Flight - left aligned, MAX_FLIGHTCODE_LEN (i.e. 6) chars at most. 2-City - left aligned, MAX_CITYCODE_LEN 3 chars at most . For example( VA1 or LAX ) 3- Month,...

  • Convert the below C code to basic MIPS. Leave comments for explanation please #include <stdio.h> int main(void) {...

    Convert the below C code to basic MIPS. Leave comments for explanation please #include <stdio.h> int main(void) { printf("Insert two numbers\n"); int a,b,c; scanf("%d",&a); scanf("%d",&b); c=a|b; printf("%d|%d=%d\n",a,b,c); return 0; }

  • #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void...

    #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void insertionSort(int arr[], int n); void merge(int a[], int l1, int h1, int h2); void mergeSort(int a[], int l, int h) { int i, len=(h-l+1); //Using insertion sort for small sized array if (len<=5) { insertionSort(a+l, len); return; } pid_t lpid,rpid; lpid = fork(); if(lpid<0) { //Lchild proc not created perror("Left Child Proc. not created\n"); _exit(-1); } else if (lpid==0) { mergeSort(a,l,l+len/2-1); _exit(0); } else...

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