this code is working perfectly for me except for one small issue. After calling on my find_min
and find_max
functions, the code also prints out a seemingly random number. I think this has to do with pass by reference, and its a memory value or something. Can somebody explain and tell me how to get rid of it? Thanks.
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;
//get_avg function will get the average of all temperatures in the array by using for loop to add the numbers
double get_avg(int* arr, int n) {
double res = 0.0;
for(int i = 0; i<n; i++){
res += arr[i];
}
return (res/n); //average
}
void get_array(int* arr){
int i;
for(i=0; i<25; i++){
arr[i] = (rand() % 100) + 1;
}
}
//prints the results of get_avg in and get_array in table format, then finds difference between avg and temp.
void print_output(int* arr) {
double avg = get_avg(arr, 24);
cout << "The average is: " << avg << endl;
cout << "Position\tValue\t\tDifference from average\n\n";
char sign;
for(int i = 0; i < 24; i++){
sign = (avg > arr[i])?('+'):('-');
cout << i << "\t\t" << arr[i] << "\t\t" << sign << fabs(avg-arr[i]) << endl;
}
}
//finds the minimum function using a for loop
int find_min(int* arr, int n) {
int low = arr[0];
int index = 0;
for(int i = 1; i<n; i++){
if(arr[i] < low){
low = arr[i];
index = i;
}
}
cout << "The position of the minimum number in the array is "<< index <<" and the value is " << low << endl;
}
//finds the maximum function using a for loop
int find_max(int* arr, int n){
int hi = arr[0];
int index;
for(int i = 1; i<n; i++) {
if(arr[i] > hi) {
hi = arr[i];
index = i;
}
}
cout << "The position of the maximum number in the array is "<< index <<" and the value is " << hi << endl;
}
int main(){
int arr[24]; //declares array
get_array(arr);
print_output(arr);
cout << find_min(arr, 24) << endl;
cout << find_max(arr, 24) << endl;
cout << endl;
// wrap-up
cout << "This program is coded by Troy Wilms" << endl; // fill in your name
// stops the program to view the output until you type any character
return 0;
}
in this two lines :
cout << find_min(arr, 24) << endl;
cout << find_max(arr, 24) << endl;
you are using std::cout
to print the returning value of these functions (which is int), but in your function definition you are not returning any value, So it will print a garbage value.
at the end of your functions ( find_min
and find_max
) add return arr[index];
As was already pointed out in another answer, the problem lies in not returning a value from a function declared to return one. So a great part of the solution here is to read and understand your compiler's warnings!
Even better, turn compiler warnings into errors (eg with GNU g++ by using the option -Werror
.
Other comments suggested to use "real" C++ types, eg std::vector
as a container for the "array", instead of using the C-style array of integer and use some magic constants when iterating over the collection. This is all correct and can even be put further: Know C++ standard library well and use it! Why programming functions that get minimum and maximum value of a collection, when these functions are already available?
An attempt to avoid these pitfalls and provide a version that is more C++-like, see the following example:
#include <algorithm>
#include <ctime>
#include <ios> // I/O manipulators
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
using Container = std::vector<int>;
// get_avg function will get the average of all temperatures in the container
double get_avg(const Container& c) {
// force promotion from int to double by using 0.0 as initial value!
return std::accumulate(c.begin(), c.end(), 0.0) / c.size();
}
// fill container c with 'count' random values, originally get_array()
void fill(size_t count, Container& c) {
std::srand(std::time(0));
size_t i = 0;
while (i++ < count) {
c.push_back((rand() % 100) + 1);
}
}
//prints the results of get_avg in and get_array in table format, then finds difference between avg and temp.
void print_output(const Container& c) {
double avg = get_avg(c);
std::cout << "The average is: " << avg << endl;
std::cout << "Position\tValue\t\tDifference from average\n\n";
size_t idx = 0;
for(auto e : c){
cout << idx++ << "\t\t" << e << "\t\t" << std::showpos << avg - e << std::noshowpos << std::endl;
}
}
// There is std::minmax_element that gets both minimum and maximum value at once!
void print_min_max(const Container& c) {
auto mm = std::minmax_element(c.begin(), c.end());
std::cout << "The position of the minimum number in the container is "<< mm.first - c.begin() <<" and the value is " << *(mm.first) << std::endl;
std::cout << "The position of the maximum number in the container is "<< mm.second - c.begin() <<" and the value is " << *(mm.second) << std::endl;
}
int main() {
Container c;
const size_t count = 24;
fill(count, c);
print_output(c);
print_min_max(c);
std::cout << endl;
// wrap-up
std::cout << "This program is coded by Troy Wilms" << endl; // fill in your name
// stops the program to view the output until you type any character
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.