Calling a function

Pages: 12
i don't know how to call the function I created.

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
#include <iostream>
using namespace std;

void maxmin (double* a, double& min, double& max, int size) {

	for (int x = 0; x < 5; x++) {
		if (a[x] == -6.5) {
			cout << " The minimum value is: " << a[x];
		}
	}
	
	for (int i = 0; i < 5; i++) {
		if (a[i] == 11.1) {
			cout << " The maximum value is: " << a[i];
		}
	}
}



int main () {
	
	double a[5] = { 11.1, 5.3, 7.3, -1.5, -6.5};
	
}
Firstly, you don't need the references min and max in your function. You also don't need size if you aren't actually going to use that variable. Define it like this:
 
void maxmin (double* ptr) {...}

Call it using:
maxmin(a);

However, I would suggest editing your function to actually use the size variable and be able to text the maximum or minimum of any size array.
Last edited on
1
2
3
4
5
6
7
8
9
10
name_of_the_function
( //open parenthesis
   first_parameter /*variable name or literal value*/ , //comma
   second_parameter,
   //...
   last_parameter
)//close parenthesis

//example
std::accumulate(v.begin(), v.end(), 42);
whitespace is optional.

> the function I created.
¿did you ever bother with pseudocode or diagram flow?


> you don't need the references min and max in your function.
> be able to text the maximum or minimum of any size array.
I'd suggest you to not do i/o on such a function
if you obtain the `min' and `max' value, but you only may print them to screen, then the function is quite useless.
Last edited on
@TheToaster i did what you've told me to do, so now my function is void maxmin (double* a) Thanks now i can call my function using maxmin (a);

Ok so our prof. told us to write a c++ program containing a function void maxmin (double* a, double& min, double& max, int size)
which evaluates the minimum value and the maximum value of the array(which i believe i already did thanks to your help). int size denotes the size of the array and in the present case size is = 5

So my question is, is there a way that i could use those other parameters besides the one im already using which is double* a

ps: I don't know if there is a reply button and also sorry for my english
Last edited on
In that case, you should create two double variables in your main() function (one for maximum and one for minimum) and pass those into the min and max parameters of maxmin. You should also pass in the size of the stack based array into the size variable of maxmin.
Also, rewrite your function to actually find the minimum and maximum rather than checking for predetermined values. See this:
https://beginnersbook.com/2017/09/cpp-program-to-smallest-element-in-an-array/

yes. You are getting close. I went ahead and did it, on the grounds that this early in your studies seeing an example can be as instructive as tearing your hair out over it. We normally try not to do them for you, so use this to learn not just copy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>

void maxmin (double* a, double& min, double& max, int size)
{
     min = max = a[0];
     for(int x = 0; x < size; x++) //notice how I did 1/2 as much work by loop only one time
      {
            if(a[x] > max) max = a[x];
            if(a[x] < min) min = a[x];
      }
}

int main()
{
     double a[5] = { 11.1, 5.3, 7.3, -1.5, -6.5};
     int size = 5;
     double mainmin, mainmax;
     maxmin(a, mainmin, mainmax, size);
     std::cout << mainmin << std::endl;  //these are the function's results. 
     std::cout << mainmax << std::endl; //do you understand WHY?
     
}
Last edited on
@TheToaster thanks a lot for your help gonna try it out tomorrow.
@jonnin thanks a lot man really appreciate it, yes indeed my other prof told our class that its better to have an example while coding it really helps and im really trying to understand your code cuz i really want to learn, because just copying your code is not learning at all, so thanks a lot again !
Last edited on
@jonnin can you explain the min = max = a[0] ? Yes, i know its a stupid question and its kinda basic and i maybe asking for too much spoon feeding but yeah hope you can explain it to me thanks
I've been really trying hard to make a solution of my own, yeah i know you've already given me the answer which is really better than mine cuz you did it only in few lines but hope you can help me with this
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>
using namespace std;

void maxmin (double* a, double& min, double& max, int size) {
	
	cout << "Array size is: " << size << endl;
	
	double tempmin = 0;
	for (int min = 0; min < size; min++) {
		if (tempmin > a[min]) {
			tempmin = a[min];
			
			cout << "Minimum value is: " << tempmin << endl;
		}
	}
	
	double tempmax;
	for (int max = 0; max < size; max++) {
		if (tempmax < a[max]) {
			tempmax = a[max];
			cout << "Maximum value is: " << tempmax << endl;
		}
	}
	
}


int main () {
	double mainmin, mainmax;
	int size = 5;
	double a[5] = {11.1, 5.3, 7.3, -1.5, -6.5};
	maxmin (a, mainmin, mainmax, size);
}


so the output is this

Array size is: 5
Minimum value is: -1.5
Minimum value is: -6.5
Maximum value is: 11.1


So obviously I'm getting 2 minimum values
For doubles, min = max = a[0]; is equivalent to (min = (max = a[0]));, which is equivalent to max = a[0]; min = max;.

EDIT:
I've been really trying hard to make a solution of my own

That's good! That's how one learns. :)

Currently, you print whenever a new lowest/largest value is found. You probably only want to do it once instead, after your code has examined each value in the array and found the true largest/smallest values.

Friendly reminder, your min and max parameters are currently unused, and tempmax is used uninitialized.

-Albatross
Last edited on
@Albatross thanks you so much for explaining it to me but i think it doesn't have a purpose (maybe ?) cuz i tried to remove it and the program still runs the same.

Also yeah thanks for pointing that out because, thats what happening right now it print whenever a new lowest value is found.

So maybe what i need to do is print it out of the loop i hope i'm right. brb
In jonnin's example, it does have a purpose. When mainmin and mainmax are passed to maxmin, they're uninitialized. That line sets them both to a valid value before they're read from.

-Albatross
Ok so now my code looks like this

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
#include <iostream>
using namespace std;

void maxmin (double* a, double& min, double& max, int size) {
	for (int x = 0; x < size; x++) {
		if (a[x] < min) {
			a[x] = min;
		}
		if (a[x] > max) {
			a[x] = max;
		}
	}
}


int main () {
	
	double mmin;
	double mmax;
	int size = 5;
	double a[5] = {11.1, 5.3, 7.3, -1.5, -6.5};
	
	maxmin (a, mmin, mmax, size);
	cout << "Array size is: " << size << endl;
	cout << "Minimum value of array is: " << mmin << endl;
	cout << "Maximum value of array is: " << mmax << endl;
	
}


and i think its working fine but,

the output is

Array size is: 5
Minimum value of array is: 2.91499e-322
Maximum value of array is: 2.10215e-317


which i believe is the ram address because of the double& min, double& max
so my question is what should i do to show the value instead of the address ?
Could you explain to me (or yourself) what lines 7 and 25 do?

-Albatross?
The are not addresses.

Carefully read this line from your most recent post:

1
2
3
	if (a[x] < min) {
			a[x] = min;
        }


Notice that the entry a[x] is assigned to min

...when what was required was to assign min to the entry a[x]

Same problem with max
@Albatross ok so first line 7 stores the lowest value which came from the test done in line 6.
and line 25 prints the mmin which stores the value of the parameter double& min that is holding the value of the line 7. I hope I am right.
@Niccolo got it dude thank you so much !
This is why you should always initialize your variables to avoid this problem. You should do this instead:

 
double mmin = 0.0, mmax = 0.0; 


Provided, of course, that you set them equal to a[0] at the beginning of the function
Last edited on
kramsuiluj (17)
@jonnin can you explain the min = max = a[0] ? Yes, i know its a stupid question and its kinda basic and i maybe asking for too much spoon feeding but yeah hope you can explain it to me thanks

the most efficient algorithm to find max or min is
1) assume the first value in the list is the target, that is a[0]
2) look for something better, if found, set to the better value, until all items looked at.

my loop should have started at 1, not 0 -- the dangers of typing online and not double checking it very much.

I did not initialize my vars in main for the same reason (typing fast). Could set them to a[0] in main if you wanted to be a purist.

The initialize variables thing is 'good practice' not 'required by c++'. You don't have to do it, as seen in my code, and its fine so long as you don't goof. Everyone goofs, so its generally 'not fine' to do that. I am old, most of the code I write is < 2 pages total, and I have a LOT of bad habits! Or, as they say ... don't do as I do, do as I tell you to do ...
Last edited on
Pages: 12