C++ exercises and solutions: max value


C++ pointer: max value

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;

}



Comments

saba ramzan comment

 saba ramzan

include some tricky problems plzzz
I couldn't find a single program according to my need


2017-12-04
ahmed salama comment

 ahmed salama

#include<iostream>
using namespace std;
int *findMax(int arr[],int n);
int main(){
int size;
cout<<"Enter number of data values: ";
cin>>size;
int *ptr=new int [size];
for(int i=0;i<size;i++){
cout<<"Enter value" <<i+1<<": ";
cin>>ptr[i];
}

cout<<*findMax(ptr,size);

delete[] ptr;
}
int *findMax(int arr[],int n){
int *max=arr;
for(int i=0;i<n;i++){
if(arr[i]>*max){
*max=arr[i];

}
}


return max;

}


2017-05-15
ahmed salama comment

 ahmed salama

#include<iostream>
using namespace std;
int *findMax(int arr[],int n);
int main(){
int size;
cout<<"Enter number of data values: ";
cin>>size;
int *ptr=new int [size];
for(int i=0;i<size;i++){
cout<<"Enter value" <<i+1<<": ";
cin>>ptr[i];
}

cout<<*findMax(ptr,size);

delete[] ptr;
}
int *findMax(int arr[],int n){
int *max=arr;
for(int i=0;i<n;i++){
if(arr[i]>*max){
*max=arr[i];

}
}


return max;

}


2017-05-15
Gilpow comment

 Gilpow

The second cout in the solution generates an error. It should be written as:
cout << "Enter value " << i+1 <<": ";

Anyway, the findMax in the solution changes the first value in the array, so the array can't be reused after calling this function.

In my solution, I dynamically allocated memory for the array (not necessary), and the array is not touched in the findMax function.

-------------------------

#include <iostream>

using namespace std;

int* findMax(int*, int);

int main()
{
int arraySize;

cout << "Size of the array: ";
cin >> arraySize;

int *Array = new int[arraySize]; //dynamic memory allocation

for(int i=0; i<arraySize; i++){
cout << "#" << i << " element: ";
cin >> Array[i];
}

int* Max = findMax(Array, arraySize);

cout << "The max value is " << *Max << endl;

//this is just to check if, after calling findMax, the first value of the array is the same
for(int i=0; i<arraySize; i++){
cout << Array[i] << endl;
}

return 0;
}

int* findMax(int* formalArray, int formalArraySize){

int* Max = formalArray;

for(int i=1; i<formalArraySize; i++){
if(formalArray[i] > *Max)
Max = &formalArray[i];
}

/* //original solution: changes (first value of) the array

for(int i=1; i<formalArraySize; i++){
if(*Max < *(Max+i))
*Max = *(Max+i);
}
*/

return Max;
}


2017-04-24



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.