Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C++ Programming Code Examples

C++ > Arrays and Matrices Code Examples

Insert update delete element in array

/* Insert update delete element in array 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. 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. 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 */ #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; }

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

Execute system command. Invokes the command processor to execute a command. If command is a null pointer, the function only checks whether a command processor is available through this function, without invoking any command. The effects of invoking a command depend on the system and library implementation, and may cause a program to behave in a non-standard manner or to terminate.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C++ programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C++ array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

A predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< '\n' << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

This Loop would never end as 'decrementing' the value of i which is 1 so the condition i<=6 would never return false. A "While Loop" that never stops is said to be "Infinite While Loop",