0

i want to put the input for n / anzahl and the output of the minimum and maximum temperature in the main function while having the calculation in the void function. i think my mistake is by calling the wrong reference but i can't see it. can someone help me see my mistake?

i got this so far. this code is everything in the main function, it works perfectly but i have problems with the realization of putting the output in the main function

#include <iostream>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{


    int n,i;
    int groesst, kleinst;
    int rand(void);
    cout << "Geben Sie Anzahl der Temperaturwerte an: "; //put in the amount of temperatures
    cin >> n;
    n=n+1; //so non programmers arent confused


    vector<int> temp(n);
    cout << "31 zufaellige Temperaturen:\n" << endl;
    groesst = temp[0];
    kleinst = temp[0];
     for (i=1;i<n;i++)
    {
        temp[i]=rand()%20-4;//random temperature between -4 and 15
        cout << temp[i] << " Grad Celsius"<< endl;

        if (temp[i]>groesst) //
        {
            groesst = temp[i]; //
        }

        if (temp[i]<kleinst)
        {
            kleinst = temp [i];
        }
    }


    cout << kleinst; //minimum temperature
    cout << "\n";
    cout << groesst; //maximum temperature

 return 0;
}

this is my attempt:

#include <iostream>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;

void minmaxim(vector<int>& temp, int& n, int& kleinst, int& groesst)
{
    int i;
    int rand(void);

    temp[n];
    groesst = temp[0];
    kleinst = temp[0];
     for (i=1;i<n;i++)
    {
        temp[i]=rand()%20-4;


        if (temp[i]>groesst)
            groesst = temp[i];
        }

        if (temp[i]<kleinst)
        {
            kleinst = temp [i];
        }
        return;
    }


int main ()
{
    vector<int> temps;
    int anzahl, minimum,maximum;
    cout << "Geben Sie die Anzahl der Temperaturwerte ein: "; //type in the amounts of temperatures
    cin >> anzahl;

   minmaxim(temps, anzahl, minimum, maximum); //calling the function
   cout << " " << anzahl;

    cout << " " << minimum <<endl;
    cout << " " << maximum <<endl;

    return 0;
}
nrerf
  • 15
  • 3
  • Can you explain the problem you're seeing? Does it not compile, is it printing the wrong values, or? – Tumbleweed53 Dec 10 '20 at 04:45
  • Oh - first you'll want to resize the vector, e.g. `temps.resize(n);`. As it is, I think you are just using undefined memory. – Tumbleweed53 Dec 10 '20 at 04:46
  • it's not printing anything except what i should input so ` "Geben Sie die Anzahl der Temperaturwerte ein: " ` – nrerf Dec 10 '20 at 09:30

1 Answers1

1

The biggest problem are minimum and maximum are both uninitialized when minmaxim() is called invoking undefined behavior. Before you compare your values, you must initialize maximum and minimum to numbers below, and above, the range of possible temperatures, e.g.

    int anzahl, 
        minimum =  200,     /* initialize min above and max below possible range */
        maximum = -200;

Or, properly covering the entire range of int, e.g.

#define NMAX std::numeric_limits<int>::max()
#define NMIN std::numeric_limits<int>::min()
...
    int anzahl,
        minimum = NMAX,     /* initialize min above and max below possible range */
        maximum = NMIN;

Your use of random values using C rand() has been replaced in C++ with Pseudo-random number generation. You would create and use a random device as follows:

void minmaxim (std::vector<int>& temp, int& n, int& kleinst, int& groesst)
{
    std::random_device rd;    /* delcare the randon number generator device */
    std::mt19937 gen(rd());   /* standard mrsene_twister engine seeded w/rd */
    std::uniform_int_distribution<int> dist(0, NMAX); /* create disribution */

Then calling

int value = dist(rd);

retrieves a random value in the range. You can set the max of range or continue to use the entire range of values and modulo, more or less your choice.

Your use of std::vector is not quite right. std::vector provides the .push_back() member function to add to the vector. your minmaxim () function could be written as:

void minmaxim (std::vector<int>& temp, int& n, int& kleinst, int& groesst)
{
    std::random_device rd;    /* delcare the randon number generator device */
    std::mt19937 gen(rd());   /* standard mrsene_twister engine seeded w/rd */
    std::uniform_int_distribution<int> dist(0, NMAX); /* create disribution */
    
    for (int i = 0; i < n; i++) {
        int randval = dist(rd) % 20 - 4;
        temp.push_back(randval);
        
        if (randval < kleinst)
            kleinst = randval;
        if (randval > groesst)
            groesst = randval;
    }
}

See Why is “using namespace std;” considered bad practice?. Making those changes, you could write your entire source as:

#include <iostream>
#include <vector>
#include <random>

#define NMAX std::numeric_limits<int>::max()
#define NMIN std::numeric_limits<int>::min()

void minmaxim (std::vector<int>& temp, int& n, int& kleinst, int& groesst)
{
    std::random_device rd;    /* delcare the randon number generator device */
    std::mt19937 gen(rd());   /* standard mrsene_twister engine seeded w/rd */
    std::uniform_int_distribution<int> dist(0, NMAX); /* create disribution */
    
    for (int i = 0; i < n; i++) {
        int randval = dist(rd) % 20 - 4;
        temp.push_back(randval);
        
        if (randval < kleinst)
            kleinst = randval;
        if (randval > groesst)
            groesst = randval;
    }
}


int main (void)
{
    std::vector<int> temps{};
    int anzahl,
        minimum = NMAX,     /* initialize min above and max below possible range */
        maximum = NMIN;
    
    std::cout << "Geben Sie die Anzahl der Temperaturwerte ein: ";
    if (!(std::cin >> anzahl)) {
        std::cerr << "error: invalid integer input.\n";
        return 1;
    }

    minmaxim (temps, anzahl, minimum, maximum); 
    
    for (const auto& t : temps)
        std::cout << t << '\n';
    std::cout << "\n " << anzahl << "\n " << minimum << "\n " << maximum << '\n';

    return 0;
}

(note: avoid including unused headers)

Example Use/Output

$ ./bin/maxmintemps
Geben Sie die Anzahl der Temperaturwerte ein: 5
4
-4
8
3
13

 5
 -4
 13

If you wanted to tidy up the output from main() a bit more, you could output the temperature values in lines of 10 with the results below. You could then legitimately include the <iomanip> header to align the temperature values wtih std::setw(). You could replace the current output loop with:

#include <iomanip>
...
    for (size_t i = 0; i < temps.size(); i++) {
        if (i % 10 == 0)
            std::cout.put ('\n');
        std::cout << " " << std::setw(2) << temps[i];
    }
    std::cout << "\n\n anzahl  : " << anzahl << 
                "\n minimum : " << minimum << 
                "\n maximum : " << maximum << '\n';

Updated Output

$ ./bin/randtempchk
Geben Sie die Anzahl der Temperaturwerte ein: 40

  3 13  9  8 12 15  1  0  7  4
 -2 -3  8 14 -4  7 -2  7 -2  2
 10  1 14  7  6  6 13  6  6  0
  5  9  6  8 13  9 14  9 15  6

 anzahl  : 40
 minimum : -4
 maximum : 15

Look things over and let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • thank you for all of this!! i should have said so but i'm not that familiar with programming yet so i'm not sure what most of these things mean that you brought in. i don't think we had 'distribution' or 'pushback' or this random number generator in our lecture yet. is there any way solve this without using these? – nrerf Dec 10 '20 at 09:47
  • 1
    Chuckling - nobody know this stuff when they start. Learning C++, it is like eating a whale -- you just take it one byte at a time `:)`. See [UniformRandomBitGenerator](https://en.cppreference.com/w/cpp/named_req/UniformRandomBitGenerator) for random generation. (and also just bookmark the [cppreferece.com](https://en.cppreference.com) site). It is the best reference going. Even now, when I need to work with a tuple, I go to [std::tuple](https://en.cppreference.com/w/cpp/utility/tuple) and refresh on the syntax and how to get things into one, and then back out again. So one-byte-at-a-time `:)` – David C. Rankin Dec 10 '20 at 09:57
  • 1
    At the cppreference site, you can use the search box to find most everything you need. To get to the get from the tuple page to the page on vectors, just type `std::vector` into the search box. (If you don't know the exact thing you are looking for -- then search sucks, so then just start from the links at the home-page for that site and you can usually find what you need.. Search here on stack overflow, just use `"[c++] your search terms"` to limit the search to questions tagged `[c++]`. There is good info here and at cppreference -- but be skeptical of what you find elsewhere. Good luck. – David C. Rankin Dec 10 '20 at 10:04