﻿ C++ exercises and solutions: max value

# 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 ramzaninclude some tricky problems plzzz I couldn't find a single program according to my need2017-12-04 ahmed salama#include 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>ptr[i]; } cout<<*findMax(ptr,size); delete[] ptr; } int *findMax(int arr[],int n){ int *max=arr; for(int i=0;i*max){ *max=arr[i]; } } return max; } 2017-05-15 ahmed salama#include 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>ptr[i]; } cout<<*findMax(ptr,size); delete[] ptr; } int *findMax(int arr[],int n){ int *max=arr; for(int i=0;i*max){ *max=arr[i]; } } return max; } 2017-05-15 GilpowThe 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 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> 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 *Max) Max = &formalArray[i]; } /* //original solution: changes (first value of) the array for(int i=1; i

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.