Write a c++ program which manages data in array having insert, update and delete value functions.
Program should be menu based properly asking for user to enter an option and on wrong or invalid input program should display proper message and ask user to enter value again.
You can use any C++ compiler.
Cpp code should contain
Program should be menu based properly asking for user to enter an option and on wrong or invalid input program should display proper message and ask user to enter value again.
You can use any C++ compiler.
Cpp code should contain
- Proper Menu Option Display. Hint use do while loop for menu
- Insert Values Function
- Update Values Function
- Delete Values Function
- Proper message to every user action
- cpp array values management
- Loops
- functions
- Recursive function calling
- if else statement
Code Explanation
cpp code:
sample input outputs (codeBocks Console Screen Shots)
Program Menu Display
Update Array Values
Exit Option Selection
This program is helpful to manage a menu base database using array and shows how to prevent from a wrong input. Its is of very basic level to introduce the simplest database management using array. It is recommended to copy the code make changes in it and examine output.
See more simple examples here. Cpp Code Example
- This code is managing a small database in array like insert, update and delete integer values in array
- Program has global array declaration of size 10. Global declaration allows it accessible inside any function
- A function name "Default values" is initializing all array indexes by default value of -1
- A function name "Display Array" displays the array values using for loop
- To manage the menu option do while is used and few if else statement within the loop to call a specific function on user selection
- To restrict user to enter valid option recursion is used in two functions so if user enter invalid option For example array size is 10 and user select index number 11 then a message will be displayed function will call itself and user has to enter the option again
cpp code:
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- //Global Array Declaration
- int array[10];
- void DisplayArray(){
- for (int i=0;i<10;i++)
- cout<< "Array [ "<<i<<" ] = "<<array[i]<<endl;
- }
- void SetDefaultValues(){
- cout<<"Defalult Values :"<<endl;
- for(int i=0;i<10;i++)
- {
- array[i]=-1;
- cout<<"array ["<<i<<"]"<<"= "<<array[i]<<endl;
- }
- }
- void InsertValues(){
- cout<<"Enter 10 Values "<<endl;
- for(int i=0;i<10;i++)
- {
- cin>>array[i];
- }
- cout<<"\n\t\t\tArray Values Inserted... Successfully "<<endl;
- }
- void DeleteValues(){
- cout<<"Enter the Index Number To Delete Value :";
- int index;
- cin>>index;
- if(index>9||index<0)
- {
- cout<<"Invalid Index Entered-> Valid Range(0-9)"<<endl;
- DeleteValues();// Recall The Function it self
- }
- else
- {
- array[index]=-1;
- }
- cout<<"\n\t\t\tArray Value Deleted... Successfully "<<endl;
- }
- void UpdateValues(){
- cout<<"Enter Index Number to Update Value :";
- int index;
- cin>>index;
- if(index>9||index<0)
- {
- cout<<"Invalid Index Entered-> Valid Range(0-9)"<<endl;
- UpdateValues();// Recall The Function it self
- }
- else
- {
- cout<<"Enter the New Value For Index array[ "<<index<<" ] = ";
- cin>>array[index];
- cout<<"\n\t\t\tArray Updated... Successfully "<<endl;
- }
- }
- int main()
- {
- char option;
- SetDefaultValues();
- do
- {
- cout<<"\t\t\tEnter 1 to Enter Values\n\t\t\tEnter 2 to Update Values\n\t\t\tEnter 3 to Delete Values\n\n\t\t\t or Enter E to EXIT\n\n\t\t\t Enter Option: -> ";
- cin>>option;
- if(option=='1')
- {
- cout<<"Insert Function Called"<<endl;
- InsertValues();
- cout<<"Inserted Values :"<<endl;
- DisplayArray();
- }
- else if(option=='2')
- {
- UpdateValues();
- cout<<"Updated Array :"<<endl;
- DisplayArray();
- }
- else if(option=='3')
- {
- DeleteValues();
- cout<<"Array After Deleting Values :"<<endl;
- DisplayArray();
- }
- else if(option!='e'&&option!='E')
- {
- cout<<"\n\n\t\t\tSelect A Valid Option From Below\n\n";
- }
- }while(option!='e'&&option!='E');
- system("cls");// To Clear The Screen
- cout<<"\n\n\n\n\n\n\n\n\n\n\t\tProgram Ended Press Any Key To Exit Screen.....\n\n\n\n\n\n\n\n\n\n\n\n"<<endl;
- return 0;
- }
sample input outputs (codeBocks Console Screen Shots)
Program Menu Display
Update Array Values
Exit Option Selection
This program is helpful to manage a menu base database using array and shows how to prevent from a wrong input. Its is of very basic level to introduce the simplest database management using array. It is recommended to copy the code make changes in it and examine output.
See more simple examples here. Cpp Code Example
this example is very good. Not really heavy, without OOP but good.
ReplyDelete//program to delete value
ReplyDelete#include
#include
#include
using namespace std;
main()
{
int arr[5];
for(int i=0;i<5;i++)
{
cin>>arr[i];
}
for(int i=0;i<5;i++)
{
cout<>index_to_delete;
cout<<endl;
for(int i=0;i<5;i++)
{
if(arr[i]!=arr[index_to_delete])
{
cout<<arr[i];
}
}
}
// C++ program to implement binary search in sorted array
ReplyDelete#include
using namespace std;
int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2; /*low + (high - low)/2;*/
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
/* Driver code */
int main()
{
// Let us search 3 in below array
int arr[] = { 5, 6, 7, 8, 9, 10 };
int n, key;
n = sizeof(arr) / sizeof(arr[0]);
key = 10;
cout << "Index: " << binarySearch(arr, 0, n - 1, key)
<< endl;
return 0;
}
// This code is contributed by NamrataSrivastava1