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) 
{
}
you can't grab user's input because you have delete "arr". you create and delete it at the same time...
Oh yeah thanks
but if I delete the delete [ ] arr;
It seems not quite done yet..
i haven't try to compile it, but it seems like no problem to me... it should be running... "delete" is usually be at the end of a program... to explicitly delete variables, otherwise, if the program closes, the program will automatically delete the variables...

CMIIW
closed account (o1vk4iN6)
You are using int types so any decimal place will cause the istream to stop. You are also only getting input for one value, you need a loop of some kind to ensure the cin was successful, as well to make sure you input for the entire array.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	int size = 0; // intialize to zero so if cin fails, it's within invalid bounds.

	cout << "Enter size: ";
	cin >> size;

	if(size > 0) {
		double* arr = new double[size];

		for(int i = 0; i < size; i++) {
			if(!(cin >> arr[i])) {
				delete[] arr;
				return -1;
			}
		}

		delete[] arr;
		return 0;
	}

	return -1;
}
Last edited on
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;
}
Topic archived. No new replies allowed.