Cpp Tutorial to reverse an array code

Leave a Comment
Write a program which takes some elements as input in integer array then pass this array to a function which reverse its elements using for loop and swapping. 
 Display the reversed array with index number. Loop should execute less than array size. Use any c compiler (codeBlocks recommended).

This tutorial contains the following concepts

Program explanation
  • Program declare an integer array of size five, initialize it using for loop
  • Pass size and array name to function 
  • Function uses for loop and swap array elements with in it
  • A for loop is used to display the final result

Compiler used: CodeBlocks C Compiler
  1. #include <iostream>
  2. using namespace std;
  3. void Reverse_Array(int array[],int size)
  4. {   int temp;
  5.     size--;
  6.     int loop_count=0;
  7.     for(int i=0;size>=i;size--,i++)
  8.     {
  9.         loop_count++;// Counts the iterations
  10.         temp=array[i];
  11.         array[i]=array[size];
  12.         array[size]=temp;
  13.     }
  14.     cout<<"Number of Iterations: "<<loop_count<<endl;
  15. }
  16. int main()
  17. {
  18.     int array[5],i;
  19.     cout<<"\nEnter 5 Integer Values in Array\n"<<endl;
  20.     for(i=0;i<5;i++)
  21.     {
  22.     cout<<"Enter Value For Index Number array [ "<<i<<" ] -> ";
  23.     cin>>array[i];
  24.     }
  25.     // Calling Reverse Array Values Function
  26.     Reverse_Array(array,5);
  27.     cout << "\n\n\t\t\tReversed Array Values" << endl;
  28.     for(i=0;i<=4;i++)
  29.         {
  30.          cout<<"\t\t\tarray ["<<i<<"]"<<"= "<<array[i]<<endl;
  31.         }
  32.     return 0;
  33. }

Sample Input  output on C compiler
cpp tutorial to reverse an integer array using function and for loop

This tutorial is helpful to understand the array indexing and how to swap the index values. Its is recommended for beginners to edit and do experiment with code. Best of luck for learning cpp programming language.
 Find More Basic Examples here
C++ simple examples

Insert update delete an element in array cpp code

1 comment
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
  • 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
This code includes the concept of

 Code Explanation
  • 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
 Compiler used: CodeBlocks C++ Compiler

cpp code:
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4. //Global Array Declaration
  5. int array[10];
  6. void DisplayArray(){
  7. for (int i=0;i<10;i++)
  8.      cout<< "Array [ "<<i<<" ] = "<<array[i]<<endl;
  9. }
  10. void SetDefaultValues(){
  11.     cout<<"Defalult Values :"<<endl;
  12. for(int i=0;i<10;i++)
  13.         {
  14.          array[i]=-1;
  15.      cout<<"array ["<<i<<"]"<<"= "<<array[i]<<endl;
  16.         }
  17. }
  18. void InsertValues(){
  19.     cout<<"Enter 10 Values "<<endl;
  20. for(int i=0;i<10;i++)
  21.         {
  22.         cin>>array[i];
  23.         }
  24.    cout<<"\n\t\t\tArray Values Inserted...  Successfully "<<endl;
  25. }
  26. void DeleteValues(){
  27.     cout<<"Enter the Index Number To Delete Value :";
  28.     int index;
  29.     cin>>index;
  30.     if(index>9||index<0)
  31.     {
  32.         cout<<"Invalid Index Entered-> Valid Range(0-9)"<<endl;
  33.         DeleteValues();// Recall The Function it self
  34.     }
  35.     else
  36.     {
  37.         array[index]=-1;
  38.     }
  39.     cout<<"\n\t\t\tArray Value Deleted...  Successfully "<<endl;
  40. }
  41. void UpdateValues(){
  42.     cout<<"Enter Index Number to Update Value :";
  43.     int index;
  44.     cin>>index;
  45.     if(index>9||index<0)
  46.     {
  47.         cout<<"Invalid Index Entered-> Valid Range(0-9)"<<endl;
  48.         UpdateValues();// Recall The Function it self
  49.     }
  50.     else
  51.     {
  52.  cout<<"Enter the New Value For Index array[ "<<index<<" ] = ";
  53.         cin>>array[index];
  54.   cout<<"\n\t\t\tArray Updated...  Successfully "<<endl;
  55.     }
  56. }
  57. int main()
  58. {
  59.     char option;
  60.     SetDefaultValues();
  62.     do
  63.     {
  64.     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: ->  ";
  65.     cin>>option;
  66.     if(option=='1')
  67.       {
  68.    cout<<"Insert Function Called"<<endl;
  69.        InsertValues();
  70.        cout<<"Inserted Values :"<<endl;
  71.        DisplayArray();
  72.       }
  73.     else if(option=='2')
  74.       {
  75.        UpdateValues();
  76.        cout<<"Updated Array :"<<endl;
  77.        DisplayArray();
  78.       }
  79.     else if(option=='3')
  80.       {
  81.        DeleteValues();
  82.        cout<<"Array After Deleting Values :"<<endl;
  83.        DisplayArray();
  84.       }
  85.     else if(option!='e'&&option!='E')
  86.       {
  87.    cout<<"\n\n\t\t\tSelect A Valid Option From Below\n\n";
  88.       }
  89.     }while(option!='e'&&option!='E');
  90.     system("cls");// To Clear The Screen
  91.     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;
  92.     return 0;
  93. }

sample input outputs (codeBocks Console Screen Shots)
Program Menu Display
simple array database cpp code with menu

Update Array Values
update value in array cpp
Exit Option Selection
insert update delete program end screen cpp code

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

Cpp Function to Find Largest and smallest number in array

1 comment
This Cpp tutorial contains
 Compiler used: CodeBlocks Cpp Compiler

Write a program which generates some random number and store in array after it program pass array to a function which calculates maximum and minimum number in array and display it use any Cpp Compiler to code.

How program works
  • Declare array of size 10
  • Using for loop assign array indexes with random values between 1 and thousand
  • Call the function and pass array and its size as argument
  • Function declares two integers max and min and assign both integers with arrays first index value
  • Then with in for loop there are two if condition first check is for minimum number and second check is for maximum number
  • Finally program display the output values of both integers min and max

      Cpp Code
  1.  #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4. void FindMaxMin(int *array, int size)
  5. {        int min,max;
  6.          min=max=array[0];
  7.          for(int i=0;i<size;i++)
  8.          {
  9.              if(array[i]<min)
  10.                min=array[i];
  11.             else if(array[i]>max)
  12.                max=array[i];
  13.          }
  14.  cout<<"Minimum Number  = "<<min<<endl;
  15.  cout<<"Maximum Number = "<<max<<endl;
  16. }
  17. int main()
  18. {
  19.     int array[10];
  20.     for(int i=0;i<=9;i++)
  21.         {
  22.          array[i]=rand()%1000+1;
  23.          cout<<"array ["<<i<<"]"<<"= "<<array[i]<<endl;
  24.         }
  25.     FindMaxMin(array,10);
  26. return 0;
  27. }
 Sample Output
Cpp Function To Find Largest And Smallest Number in Array

Another Example without Function more simple
Find Maximum and Minium Number in Array Cpp Code

More Cpp Program Tutorials
C++ Simple Examples

Passing array to a function cpp code

Leave a Comment
 This Cpp Tutorial contains
  • 3 ways to pass an array to a function
  • Each way and its usage
  • 3 c++ codes examples
  • How codes are working 
  • Sample output
  • Compiler used "C++ CodeBlocks Compiler"

Write a program which declares an array, initialize it then pass to a function. Function takes square of array values and update it. Show updated values in main function.

Concept used:
How programs works
  •   Declare array of size 10
  •   Initialize it using for loop
  •   Calling "UpdateArray" function and passing array as           argument
  •   Take squares of all values and update it
  •   Display final result in main function
Note: Function return type is void why? 
          As we know array name is constant pointer memory location if we get this location we can update it after updating if we access this memory location any where in the program we will get updated values. So no necessarily needs to return array or pointer.

First way
As we know array name is a constant pointer if we can get the this pointer we can access all values of array.
declaring  array in function parameter
Prototype: void  UpdateArray(int array[] );

 c++ code

  1.  #include <iostream>
  2.  using namespace std;
  3.  void UpdateArray(int array[])
  4.  {
  5.      for(int i=0;i<=9;i++)
  6.           array[i]=array[i]*array[i];
  7.  }
  8.  int main()
  9.  {
  10.      int array[10];
  11.      for(int i=0;i<=9;i++)
  12.           array[i]=i+1;
  13.      UpdateArray(array);
  14.  // Updated Values of Array
  15.      for(int i=0;i<=9;i++)
  16.           cout<<array[i]<<endl;
  17.      return 0;
  18.  }

Second way
Prototype: void  UpdateArray(int *array );
Declaring an integer pointer in function parameter
This pointer will receive arrays name(constant pointer) and we can access all values and update too.

  1.   #include <iostream>
  2.  using namespace std;
  3.  void UpdateArray(int *array)
  4.  {
  5.      for(int i=0;i<=9;i++)
  6.           array[i]=array[i]*array[i];
  7.  }
  8.  int main()
  9.  {
  10.      int array[10];
  11.      for(int i=0;i<=9;i++)
  12.           array[i]=i+1;
  13.      UpdateArray(array);
  14.  // Updated Values of Array
  15.      for(int i=0;i<=9;i++)
  16.           cout<<array[i]<<endl;
  17.      return 0;
  18. }

Third way
 In this way we pass two arguments one is arrays name and second   is its size
 In function receiving parameters we declares an array or integer pointer (your choice) to receive address of array and another integer variable size which contains the size of array.
Prototype:  void  UpdateArray(int *array, int size );

  1.  #include <iostream>
  2.  using namespace std;
  3.  void UpdateArray(int *array, int size)
  4.  {
  5.      for(int i=0;i<size;i++)
  6.           array[i]=array[i]*array[i];
  7.  }
  8.  int main()
  9.  {
  10.      int array[10];
  11.      for(int i=0;i<=9;i++)
  12.           array[i]=i+1;
  13.      UpdateArray(array,10);
  14.  // Updated Values of Array
  15.      for(int i=0;i<=9;i++)
  16.           cout<<array[i]<<endl;
  17.      return 0;
  18.  }
 Sample output all the codes have same
passing array to a function cpp code different ways
more Arrays Examples Here
C++ Simple Examples
For best practice do experiment with codes and examine output

if any confusion found dry run the code on a paper.