Implement arrays with pointers and functions in OOP

 Write a program that inputs four integer values and passes them to a function using pointers. The function then exchange the value of 1st integer with 2nd integer and 3rd with 4th integer and then the program finally display the values.

Source Code:
#include<iostream>

using namespace std;

int swap(int*,int*,int*,int*);

int main()

{

            int a,b,c,d;

            cout<<"Please enter the 4 values"<<endl;

            cin>>a>>b>>c>>d;

            cout<<"Before swaping:"<<endl;

             cout<<"a="<<a<<","<<"b="<<b<<","<<"c="<<c<<","<<"d="<<d<<endl;

            swap(&a,&b,&c,&d);

            cout<<"After swaping:"<<endl;

            cout<<"a="<<a<<","<<"b="<<b<<","<<"c="<<c<<","<<"d="<<d<<endl;

            return 0;

}

            int swap(int*q1, int*q2,int*q3,int*q4)

           

{

            int temp1,temp2;

            temp1=*q1;

            *q1=*q2;

            *q2=temp1;

            temp2=*q3;

            *q3=*q4;

            *q4=temp2;

}

output:


______________________________________________________________________________

Write a program to input arrays values by using functions and pointer array. Make a function of name “input Array” in which user enters the value of array and then display the values in main code.

Source Code:
#include<iostream>

using namespace std;

void inputArray(int *i)

{

int array[*i];

cout<<"Enter the value of array="<<endl;

for(int s=0;s<*i;s++)

{

cin>>array[s];

}

cout<<"The values display in  main code:"<<endl;

for(int y=0;y<*i;y++)

{

cout<<array[y]<<" ";

}

}

int main()

{

int n;

cout<<"Enter the size of array=";

cin>>n;

cout<<"           "<<endl;

inputArray(&n);

}

output:

__________________________________________________________

Write a program to input any integer value and then to find out if the given value is a prime number or not by passing a number to a function as pointer argument

Source Code:

#include<iostream>

using namespace std;

void isprime(int *q)

{

int t=1;

while(t<=*q/2)

{

if(*q%t==0)

{

cout<<"The number is a prime"<<endl;

}

else

cout<<"The number is not a prime"<<endl;

break;

}

}

int main()

{

int q;

cout<<"Input the number=";

cin>>q;

isprime(&q);

return 0;

}

output:


__________________________________________________________

Write a program to input arrays values by using functions and pointer array. Make a function of name “inputArray” in which user enters the value of array and then display the values in main code. Make second function with name “doubArray” which makes the enter array elements double by multiplying them by 2 and then display the result

Source Code:
#include<iostream>

using namespace std;

void inputArray(int *i)

{

int array[*i];

cout<<"Enter the value of array="<<endl;

for(int s=0;s<*i;s++)

{

cin>>array[s];

}

cout<<"The values display in  main code:"<<endl;

for(int y=0;y<*i;y++)

{

cout<<array[y]<<" ";

}

}

void doubArray(int*b)

{

int array[*b];

cout<<"           "<<endl;\

cout<<"The Double values of the Array:"<<endl;

for(int y=0;y<*b;y++)

{

cout<<array[y]*2<<" ";

}

}

int main()

{

int n;

cout<<"Enter the size of array=";

cin>>n;

cout<<"           "<<endl;

inputArray(&n);

doubArray(&n);

}

 

output:


__________________________________________________________

 

Post a Comment

0 Comments