Sunday, January 24, 2016

Pointer in C/C++ - Exersice 3

Exercise:   By using C++ pointer, write a C++ program to find the max of an integral data set. The program will ask the user to input th... thumbnail 1 summary

Exercise:  By using C++ pointer, write a C++ program to find the max of an integral data set. The program will ask the user to input the number of data values in the set and each value. Then your program will show the max of the data set. See example below.  Your C++ program will use a function that accepts the array of data values and its size. The return from the function is the pointer that points to the max value.
  Enter number of data values: 3
  Enter value 1: 21
  Enter value 2: 12
  Enter value 3: 4
  The max is 21. 
  Result: 2/9 
Solution:
#include<iostream> #include<conio.h> 
using namespace std;
int *findMax(int arr[],int n); 
int main(){
  
   int n,i,*p;
   
  cout<<"Enter number of data values";
   
  cin>>n;
   
  int arr[n];
       
  for(i=0;i<n;i++)
     {     
    cout<<"Enter value<<i+1<<":";
     
   cin>>arr[i];
           
}
         
  p=findMax(arr,n);
   
  cout<<"The max value is:"<<*p;
   
  getch();
   
  return 0;
 
}
  int *findMax(int data[],int n){   
  int *max=data;
   
  int i;
  
 for(i=1;i<n;i++){
          
   if(*max<*(max+i)) *max=*(max+i);         
                   
  }
   
  return max;
 
}

No comments

Post a Comment