Bilbo
Bilbo

Reputation: 125

Min and max of n sets of numbers

I am supposed to write a program in C++ which will find maximum and minimum of n sets of numbers in the following way:

  1. The first input is a positive integer n = the number of 5-element sets of numbers. That is, we want to have n lines of numbers, 5 (real) numbers in each line.

  2. Each set of numbers consists of 5 numbers separated by a space bar.

  3. As a result we want to find for each of the n sets its maximum, minimum and arithmetic mean.

Here is my attempt:

#include<iostream>
using namespace std;

int n;
float maxx, minn, a;

int main()
{
    cin >> n;

    for (int j = 0; j < n; j++)

    {

        float T[5];

        for (int i = 0; i < 5; i++)
        {
            cin >> T[i];
        }

        if (T[0] > T[1]) {
            maxx = T[0];
            minn = T[1];
        }
        else {
            maxx = T[1];
            minn = T[0];
        }

        for (int i = 0; i < 5; i += 2) {

            if (T[i + 2] > T[i + 3]) {
                if (T[i + 2] > maxx) {
                    maxx = T[i + 2];
                    if (T[i + 3] < minn) {
                        minn = T[i + 3];
                    }
                }
            }
            else {

                if (T[i + 3] > maxx) {
                    maxx = T[i + 3];
                    if (T[i + 2] < minn) minn = T[i + 2];
                }
            }
        }
        a = (T[0] + T[1] + T[2] + T[3] + T[4]) / 5.0;

        cout << maxx << endl;
        cout << minn << endl;
        cout << a << endl;
    }

    return 0;
}

Here is what happens after correcting i, j=1, i, j ++ into i, j=0 and changing the algorithm for min and max:

enter image description here

As you can see, the calculations are done correctly this time, but there is still a problem with the loop.

Could you tell me what is wrong with my solution? How can I fix it?

OK! IT WORKS JUST FINE NOW! THANK YOU A LOT FOR ALL YOUR HELP AND PATIENCE!

Upvotes: 1

Views: 1170

Answers (6)

Jarod42
Jarod42

Reputation: 217265

You currently have out of bound accesses (T[i + 2] > T[i + 3] where i == 3).

split in sub-function may help:

void max_min_mean_5()
{
    float values[5];

    for (auto& value : values) {
        std::cin >> value;
    }

    const auto p = std::minmax_element(std::begin(values), std::end(values));
    const auto mean = std::accumulate(std::begin(values), std::end(values), 0.f) / 5.f;
    std::cout << *p.second << std::endl;
    std::cout << *p.first << std::endl;
    std::cout << mean << std::endl;
}

int main()
{
    int n;
    std::cin >> n;
    for (int i = 0; i != n; ++i) {
        max_min_mean_5();
    }
}

Live demo.

Upvotes: 1

kaikadi-bella
kaikadi-bella

Reputation: 121

According to your code you have to enter the other set of numbers after you get max, min and average.

Upvotes: 1

SIlverstripeNewbie
SIlverstripeNewbie

Reputation: 291

change

for(j=1; j<=n; j++)

to

for(j=1; j<=n; ++j)

Because j++ evaluates to prior j value, ++j evaluates to the just incremented value.

Upvotes: -6

kaikadi-bella
kaikadi-bella

Reputation: 121

float T[5];

for(i = 1; i <= 5; i++)
{
    cin >> T[i];
}   

Each and every array starts with an index 0. For example if you declare an integer array say int a[10]; It's starting address is say 1000. Then the memory location goes like this:

For first element a[0] 1000+0*size of int

For second element a[1] 1000+1*size of int

For third element a[2] 1000+2*size of int....... and so on.

In this example you have started with T[1], which is starting address+1*size of float, which is logically the second element

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409176

If you enter 2 for n, then the outer loop (for(int j=0; j<n; j++)) will run two times, letting you enter the five numbers for calculations twice. That's how you coded it. If it's not supposed to be like that then you need to rethink the solution about the problem you try to solve.

Upvotes: 1

Sam Varshavchik
Sam Varshavchik

Reputation: 118330

As written, your code results in undefined behavior, because it is going to corrupt the stack memory.

float T[5];

for(i = 1; i <= 5; i++)
{
    cin >> T[i];
}

This obviously results in undefined behavior. This may or may not be your actual bug, but until this bug is fixed (and this bug is repeated several times, in the following code), you cannot expect any defined behavior from your program.

Upvotes: 4

Related Questions