a C++ program with a function named FindMax that has two parameters

Task:
Write a C++ program with a function named FindMax that has two parameters, one in type
constant pointer to constant double array (i.e. const double* const arr), another one is in int
type that represents the size of the array. Your task in this part is to find the position of array
element with maximum value. Instead of returning the index of array, a pointer to the
maximum element should be returned (note: the returned pointer should be made in a way
that the value of the maximum value is unchangeable.)
Assumption: All the floating point values in the array are distinct.
Your main function should start by asking the user how many numbers he/she would like to
input, then followed by asking all the values. At the end of the program, print out the
maximum value obtained from the FindMax function.
A sample screen display when the method is called is given below:
How many numbers do you like to enter? 5
Enter values: 10.5 2.1 5.21 1.23 23.8
The maximum value: 23.8


These are work so far.But in the code cin >> arr;
I don't know how to grab user inputs( a few double ) into a array..
Thanks!



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <ctime>
#include <limits>
#include <cstdlib>
using namespace std;


int main()
{   
	int nums;
 
    cout << "How many numbers do you like to enter? " ;
    cin >> nums;

	
    
	int* arr = new int[nums];
	
	delete [ ] arr;

	cout << "Enter values: " ;
    cin >> arr;
	
	
	delete [ ] arr;
	// Hold the command window
	system("pause");
	return 0;
}

void FindMax( const double* const arr ,int arr2) 
{
}
first thing. You are supposed to use doubles. So
int * arr = new int[nums]; is useless
use
double* arr = new double[nums];
instead.
.
To answer, how to read the values. You are basically trying to read inputs to an array. You are supposed to use loops (for loop in particular). Lookup arrays and loops in your books. Happy Coding!
Hi

reading the array

1
2
for(int i= 0 ; i < nums ; i++)
	cin >> arr[i];


the function, a possible solution

1
2
3
4
5
6
7
8
9
10
11
12
const double& findMax(double* d, const int& n){

	int max = 0;
	for(int i = 0 ; i < n ; i++){
		for(int j = 0 ; j < n ; j++){
			if( d[i]  < d[ j ])
				max = j;
		}
	}

	return d[max];
}


hope it helps
thanks Pravesh Koirala & therockon7throw!!!!
these are new code but I don't know it is now correct or not
the programme give me address but the example given by my teacher are these
How many numbers do you like to enter? 5
Enter values: 10.5 2.1 5.21 1.23 23.8
The maximum value: 23.8

but the task in this part is to find the position of array
element with maximum value. Instead of returning the index of array, a pointer to the
maximum element should be returned (note: the returned pointer should be made in a way
that the value of the maximum value is unchangeable.)

are these correct???
And the final output are:
The maximum value: 0 The maximum value: 1 The maximum value: 2
How can I only show "The maximum value:" once only in the beginning in the statement????
Thanks!!!




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <ctime>
#include <limits>
#include <cstdlib>
using namespace std;

const double& FindMax(double* d, const int& n){

	int max = 0;
	for(int i = 0 ; i < n ; i++){
		for(int j = 0 ; j < n ; j++){
			if( d[i]  < d[ j ])
				max = j;
			cout << "The maximum value:"<<j;
			
		}
	}

	return d[max];
}

int main()
{   

	
	int size = 0; // intialize to zero so if cin fails, it's within invalid bounds.

	cout << "How many numbers do you like to enter? ";
	cin >> size;

	double* arr = new double[size];
	cout << "Enter values: ";
		for(int i= 0 ; i < size ; i++){
		
		cin >> arr[i];}
		FindMax(arr , size );

		

	
	
    
	// Hold the command window
	system("pause");
	return 0;
}
a pointer to the maximum element should be returned
const double& is not a pointer.

Don't put the output in the function, but use the returned value in main()

By the way, ¿do you realize that your algorithm is O(n^2)? You can do it O(n)

Edit: duplicated thread http://cplusplus.com/forum/beginner/62245/
Last edited on
Hi

You are right, to find the max or min you can use just one loop, it is faster,
I dont need to see the beginner article, if you recommend me!

1
2
3
4
5
6
int max = 0;
	for(int i = 0 ; i < n-1 ; i++){
			if(d[max ] < d [i +1 ] ) 
                               max = i+1;
	}
        return d[max];


and when it has to be a const pointer than do it this way

1
2
3
4
5
6
7
8
9
const double* findMax(double* d, const int& n){

	int max = 0;
	for(int i = 0 ; i < n-1 ; i++){
			if(d[max ] < d [i +1 ] ) 
                                 max = i+1; 
	}
	return &d[max];
}



ne555 said
a pointer to the maximum element should be returned
const double& is not a pointer.


I wonder why some people here just try to points to some thing they might not like
or point to some thing they might not used to, or point to some thing else other than the solution , instead of doing this you can just recommend a solution!
Last edited on
http://www.cplusplus.com/forum/articles/31015/

I found a pointer more useful, as you can use it for other operations (distance, insertion, erase, neighbours).
However being an array an index may be better. (still, the transition to STL algorithms may be easier)
You get the position of the element, that is lost with a reference.

By the way, your condition is incorrect. You are supposed to compare against the current maximum
1
2
if( array[max_index] < array[K] ) 
  max_index = K;
Topic archived. No new replies allowed.