Algorithm to create a menue driven program to perform various array operations.

Problem:Menue Driven Program to perform various array operations:
1.Creation
2.Dispaly
3.Insertion
4.Deletion
5.Exit
##Its a program to create a menue that will work with an array and it will perform whatever it is asked.
                                                                  


Source Code:

#include <iostream>
#include <stdlib.h>
using namespace std;
int i,j,k,l,n,a[10000],count,val;
int choice;
void creation();
void insertion();
void deletion();
void display();
int main(int argc, char const *argv[]) {

  do {
    std::cout << "---------Menue-----------" << '\n';
    std::cout << "1:Creation" << '\n';
    std::cout << "2:Display" << '\n';
    std::cout << "3.Insertion" << '\n';
    std::cout << "4.Deletion" << '\n';
    std::cout << "5.Exit" << '\n';
    std::cout << "Enter your choice: " ;
    std::cin >> choice;
    switch (choice) {
      case 1:
      creation();
      break;
      case 2:
      display();
      break;
      case 3:
      insertion();
      break;
      case 4:
      deletion();
      break;
      case 5:
      exit(0);
      break;
      default:
      std::cout << "Invalid Choice" << '\n';
      break;
    }

  } while(choice!=5);

  return 0;
}

void creation(/* arguments */) {
  std::cout << "How many elements: ";
  std::cin >> count;
  std::cout << "Enter the elements: " << '\n';
  for (size_t i = 0; i < count; i++) {
    std::cin >> a[i];
  }

}
void display(){
  std::cout << "The elements of the array:" << '\n';
  for (size_t i = 0; i < count; i++) {
    std::cout << a[i] << '\n';
  }
}

void insertion(){
  std::cout << "Which element you wanna insert: " ;
  std::cin >> n;
  std::cout << "In which position you wanna insert: " ;
  std::cin >> j;
  for(i=count-1;i>=j-1;i--){
    a[i+1]=a[i];
  }
  a[j-1]=n;
  count=count+1;
}
void deletion(){
  std::cout << "which position of the array you wanna delete: " << '\n';
  std::cin >> k;
  for(i=k-1;i<count-1;i++){
    a[i]=a[i+1];
  }
  count=count-1;

}





Sample Output:
---------Menue-----------
1:Creation
2:Display
3.Insertion
4.Deletion
5.Exit
Enter your choice: 1
How many elements: 4
Enter the elements:
1
2
3
4
---------Menue-----------
1:Creation
2:Display
3.Insertion
4.Deletion
5.Exit
Enter your choice: 3
Which element you wanna insert: 12
Which position you wanna insert: 2
---------Menue-----------
1:Creation
2:Display
3.Insertion
4.Deletion
5.Exit
Enter your choice: 2
The elements of the array:
1
12
2
3
4
---------Menue-----------
1:Creation
2:Display
3.Insertion
4.Deletion
5.Exit
Enter your choice: 4
Which position of the array you wanna delete:
2
---------Menue-----------
1:Creation
2:Display
3.Insertion
4.Deletion
5.Exit
Enter your choice: 2
The elements of the array:
1
2
3
4
---------Menue-----------
1:Creation
2:Display
3.Insertion
4.Deletion
5.Exit
Enter your choice: 5

Press any key to continue . . .













Explanation:


Here we have created a menue program using the syntax of C++.At first we declared all the variables Global as they were used in different scope.Then we used do-while loop to continue the menue until it exits.Then we used switch cases.Before starting main function,we did prototype for all the function to recognize the functions to the compiler.Then in the switch cases,we created all the functions that is needed to be performed such as deletion,insertion etc.Then outside the main function we have completed all the function that was called in the switch cases.The program will work until it is called for exit.




 



















No comments

Powered by Blogger.