Answer to Question #244807 in C for Janu

Question #244807
Create a list which contains integer elements and then perform insert,delete,display, search and exit operations
1
Expert's answer
2021-09-30T07:23:29-0400
#include<stdio.h>




int main(){
	int i=0;
	int index=0;
	int size=0;
	int oddSum=0;
	int target=0;
	int elements[1000];




	int ch=-1;


	while(ch!=8){
		printf("1. Insert\n");
		printf("2. Delete\n");
		printf("3. Display\n");
		printf("4. Search\n");
		printf("5. Display the multiple of two\n");
		printf("6. Calculate the sum of odd elements\n");
		printf("7. Display the elements in the reverse order\n");
		printf("8. Exit\n");	
		scanf("%d",&ch);
		switch(ch){
		case 1:
			{
				printf("Enter element: ");
				scanf("%d",&elements[size]);
				size++;
			}
			break;
		case 2:
			{
				printf("Enter index of element to delete [0,%d]: ",(size-1));
				scanf("%d",&index);
				for(i=index;i<size-1;i++){
					elements[i]=elements[i+1];
				}
				size--;
				printf("\nThe element has been deleted.\n");
			}
			break;
		case 3:
			{
				printf("All elements:\n");
				for(i=0; i<size; i++){
					printf("%d ",elements[i]);
				}
				printf("\n");
			}
			break;
		case 4:
			{
				printf("Enter the element to search: ");
				scanf("%d",&target);
				for(i=0; i<size; i++){
					if(target==elements[i]){
						printf("Index %d = %d\n",i,target);
					}
				}
			}
			break;
		case 5:
			{
				for(i=0; i<size; i++){
					printf("%d * 2 = %d\n",elements[i],elements[i]*2);
				}
				printf("\n");
			}
			break;
		case 6:
			{
				for(i=0; i<size; i++){
					if(elements[i]%2!=0){
						oddSum+=elements[i];
					}
				}
				printf("The sum of odd numbers are: %d\n",oddSum);
			}
			break;
		case 7:
			{
				printf("All elements in the reverse order:\n");
				for(i=size-1; i>=0; i--){
					printf("%d ",elements[i]);
				}
				printf("\n");
			}
			break;
		case 8:
			{
				//exit
			}
			break;
		default:
			{
				printf("Wrong selection.\n");
			}
			break;


		}
	}
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS