C++:Pointers|Function, Arrays, And Pointer

Function, Arrays, And Pointer in C++

Example:4
Take input for “n” element using an array, pass the entire array to a function and display the elements. Also check and print the following:
1. max element
2. min element
3. diff between max and min element
Sol:

#include<iostream>
#include<conio.h>
using namespace std;
void show(int *p,int n)
{
	int i,ma,mi,d;
	ma=*p;
	mi=*p;
	for(i=0;i<n;i++)
	{
		cout<<*p<<endl;
		//max no
		if(*p>ma)
		ma=*p;
		//min no
		if(*p<mi)
		mi=*p;
		p++;
	}
	//diff 
	d=ma-mi;
	cout<<"Max element  = "<<ma<<endl;
	cout<<"Min element  = "<<mi<<endl;
	cout<<"diff = "<<d<<endl;
}

int main()
{
	int a[20],n,i;
	cout<<"Enter total elements ";
	cin>>n;
	//input
	for(i=0;i<n;i++)
	{
		cout<<"Enter any element ";
		cin>>a[i];
	}
	//display
	show(a,n);
	//show(&a[0],n);
	return(0);
	
}

Output:

Enter total elements 5
Enter any element 25
Enter any element 6
Enter any element 32
Enter any element 48
Enter any element 5
25
6
32
48
5
Max element = 48
Min element = 5
diff = 43

Example:5
Take input for “n” element using an array, pass the entire array to a function, further take input for an element to search. Check and print whether the element is present in the array or not?
Sol:

#include<iostream>
#include<conio.h>
using namespace std;
void search(int *p,int n,int n1)
{
	int i,z=0;
	for(i=0;i<n;i++)
	{
		if(*p==n1)
		{
			z=1;
			break;
		}
		p++;
	}
	if(z==0)
	cout<<"Element is not present";
	else
	cout<<"Element is present";
}

int main()
{
	int a[20],n,i,n1;
	cout<<"Enter total elements ";
	cin>>n;
	//input
	for(i=0;i<n;i++)
	{
		cout<<"Enter any element ";
		cin>>a[i];
	}
	cout<<"Enter element to search  ";
	cin>>n1;
	//display
	search(a,n,n1);
	//search(&a[0],n,n1);
	return(0);
	
}

Output:

Enter total elements 5
Enter any element 14
Enter any element 52
Enter any element 36
Enter any element 95
Enter any element 86
Enter new value 36
Element is present

Example:6
Take input for “n” element using an array, pass the entire array to a function, further take input for an element to search. Check and print whether the element is present in the array or not, if the element is present also print its count (how many times it is present) and position?
Sol:

#include<iostream>
#include<conio.h>
using namespace std;
void search(int *p,int n,int n1)
{
	int i,z=0;
	for(i=0;i<n;i++)
	{
		if(*p==n1)
		{
			z=z+1;
			cout<<"Element found at position "<<i+1<<endl;
		}
		p++;
	}
	if(z==0)
	cout<<"Element is not present";
	else
	cout<<"Element is present "<<z<<" times";
}

int main()
{
	int a[20],n,i,n1;
	cout<<"Enter total elements ";
	cin>>n;
	//input
	for(i=0;i<n;i++)
	{
		cout<<"Enter any element ";
		cin>>a[i];
	}
	cout<<"Enter element to search  ";
	cin>>n1;
	//display
	search(a,n,n1);
	//search(&a[0],n,n1);
	return(0);
	
}

Output:

Enter total elements 5
Enter any element 10
Enter any element 23
Enter any element 10
Enter any element 6
Enter any element 10
Enter new value 10
Element found at position 1
Element found at position 3
Element found at position 5
Element is present 3 times

Example:7
(Search and replace)
Take input for “n” element using an array, pass the entire array to a function, further take input for an element to search and a new value to replace with. Check and print whether the element is present in the array or not and also replace it with the new value?
Sol:

#include<iostream>
#include<conio.h>
using namespace std;
void search(int *p,int n,int n1,int n2)
{
	int i,z=0;
	for(i=0;i<n;i++)
	{
		if(*p==n1)
		{
			z=1;
			*p=n2;
		}
		p++;
	}
	if(z==0)
	cout<<"Element is not present";
	else
	{
		cout<<"Element found and replaced"<<endl;
		p=p-n;
		for(i=0;i<n;i++)
		{
			cout<<*p<<endl;
			p++;
		}
	}
	
}

int main()
{
	int a[20],n,i,n1,n2;
	cout<<"Enter total elements ";
	cin>>n;
	//input
	for(i=0;i<n;i++)
	{
		cout<<"Enter any element ";
		cin>>a[i];
	}
	cout<<"Enter element to search  ";
	cin>>n1;
	cout<<"Enter new value ";
	cin>>n2;
	//display
	search(a,n,n1,n2);
	//search(&a[0],n,n1,n2);
	return(0);
	
}

Output:

Enter total elements 10
Enter any element 25
Enter any element 10
Enter any element 361
Enter any element 10
Enter any element 65
Enter any element 25
Enter any element 10
Enter any element 45
Enter any element 9
Enter any element 25
Enter element to search 10
Enter new value 500
Element found and replaced
25
500
361
500
65
25
500
45
9
25