Poniter Program with Array and Function in cpp 2022 - mymixindia.com

Pointer Program with Array and Function in Cpp 2022


Pointer Program with array and function in Cpp

The Pointer Program with Array and Function in Cpp 2022 has the 2 arrays for sum and 1 for result and printing the values and address of the array a and b with the help of pointer.

//......Mymixindia.com......
//......Pointer Program in Cpp 2022.......
//........Pointers with Arrays with functions...........
#include<iostream>
using namespace std;
void sum(int a[], int b[]); //.......function prototype.....

int main(){

int a[4] = {23,45,56,24}; 
int b[4] = {33,55,66,44}; 
//....calling the sum function....
sum(a,b);
    return 0;
}

void sum(int a[], int b[]){

int result[4];
int *ptr,*ptr2;
ptr  = &a[0];
ptr2 = &b[0];

//..........Printing the information on the screen......
cout<<"\n Welcome to the Pointer Program...\n";
cout<<"\n Pointer with Array with Function Program...\n";
cout<<"Value of a[0] = "<<a[0]<<endl;
cout<<"Value of b[0] = "<<b[0]<<endl;
cout<<"address of a ptr = "<<ptr<<endl;
cout<<"address of b ptr2 = "<<ptr2<<endl;
cout<<"Value of a[0] by pointer = "<<*ptr<<endl;
cout<<"Value of b[0] by pointer = "<<*ptr2<<endl;
cout<<"\n Callng the sum function....\n";
for(int i=0; i<4;i++) //.....sum of the array a and b and sum value store in result array.... 
{
    result[i] = a[i]+b[i];
}

cout<<"\n The sum of the two arrays a/b \n";

for(int i=0; i<4;i++)//......printing the resultant values......
{
cout<<"Result [ "<<i<<" ] = "<<result[i]<<endl;
}

}

 

The output of the pointer program

 Welcome to the Pointer Program...

 Pointer with Array with Function Program...
Value of a[0] = 23
Value of b[0] = 33
address of a ptr = 0x7bfe10
address of b ptr2 = 0x7bfe00
Value of a[0] by pointer = 23
Value of b[0] by pointer = 33

 Callng the sum function....

 The sum of the two arrays a/b 
Result [ 0 ] = 56
Result [ 1 ] = 100
Result [ 2 ] = 122
Result [ 3 ] = 68

 

Related Posts:

Pointer Program in Cpp 2022

Program to Print Prime Number in CPP 2020

5 Namespace Program in C++ 2022

Constructors Program in Java

Java Data Type Program

HTML Tutorials 

CSS tutorials