computing the minimum value

Hello everyone,

I'm just trying to compute the mean, minimum, and maximum values on three different threads. I was just wondering if anyone had any idea why my minimum function keeps computing the min as 0. Thanks to anyone that can give me some insight.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <thread> //new c++ library
using namespace std;

float average, min, max, list_max;
vector<float>input;

void get_input(){
	float in;
	cout << "How many numbers are you listing?" << endl;
	cin >> list_max;
	cout << "Type your numbers and press the Enter key after each number" << endl;
	for (int i = 0; i < list_max; ++i){
		cin >> in;
		input.push_back(in);
	}
	cout << "You listed: { ";
	for (int z = 0; z < list_max; ++z){
		cout << input[z]<<' ';
	}
	cout << "}" << endl;
}

void compute_average(vector<float>nums){
	for (int i = 0; i < list_max; ++i){
		average += input[i];
	}
	cout << "Average: " << average/list_max <<endl;
}

void compute_min(vector<float>nums){
	for (int i = 0; i < list_max; ++i){
		min = (input[i] < min) ? input[i] : min;
	}
	cout << "Minimum Value: " << min << endl;
}

void compute_max(vector<float>nums){
	for (int i = 0; i < list_max; ++i){
		max = (input[i] > max) ? input[i] : max;
	}
	cout << "Maximum Value: " << max << endl;
}

int _tmain(int argc, _TCHAR* argv[]){
	
	get_input();

	thread avg(compute_average,input);
	thread min(compute_min,input);
	thread max(compute_max,input);

	avg.join();
	min.join();
	max.join();

	return 0;
}

I think it has to do with your min initializing.

You should have a separate variable that's temporary and replaceable.

In this case if you're initializing the min and it ends up being some negative float value then your input[i] might never be less than min.

You'd want to do:

1
2
3
4
5
6
//for loop
temp = input[i+1];
min = (input[i] < temp) ? input[i] : temp;
//end of for loop



Also
What is the point of having a vector parameter vector<float>nums

when you don't even use it for your argument
vector<float>input?


Here's an example:
http://ideone.com/CnZOfw
Last edited on
Topic archived. No new replies allowed.