0

I need help in a C++ for a school task. I don't really know where the error is. It seems like it skips the first row.

I should compare the highest value with an other row's avarage value.

Task: In the first row of the standard input there are the count of the cities (1≤N≤1000) and the count of days (1≤M≤1000). In the following N row there are the daily forecast M temperature values (-50≤Hi,j≤50). In the standard output's first row, you have to write the city number, which maximal forecast has to be lower than some other city's avarage temperature! If there is none you should write -1!

Example: Input

3 5
11 11 11 11 20
18 16 12 16 20
10 15 12 10 10

The code:

#include <iostream>
using namespace std;
int main() {
    int N, M;
    cin >> N;
    cin >> M;

    int homerseklet[N][M];
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            cin >> homerseklet[i][j];
        }
    }

    int maxh[N] = {0}, osszh[N] = {0};
    for (int i = 0; i < N; i++)
    {
        maxh[i] = homerseklet[i][0];
        for (int j = 0; j < M; j++)
        {
            osszh[i] = osszh[i] + homerseklet[i][j];
            if (homerseklet[i][j] > maxh[i])
            {
                maxh[i] = homerseklet[i][j];
            }
        }
    }

    int atlag[N] = {0};
    for (int i = 0; i < N; i++)
    {
        atlag[i] = osszh[i] / M;
    }

    bool van = false;
    for (int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            if (i != j && maxh[i] < atlag[j])
            {
                if (van = true)
                {
                    cout << i + 1 << endl;
                }
            }
        }
    }

    if (!van)
    {
        cout << -1 << endl;
    }
    return 0;
}
Melkor
  • 1
  • 1
  • 1
    `int homerseklet[N][M]` is not a valid c++, you should use dynamic allocation for runtime size dependent arrays – Hawky Jan 07 '20 at 13:06
  • [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) `std::vector> homerseklet(N, std::vector(M)); std::vector osszes(N, 0);` instead of `int homerseklet[N][M], osszes[N] = {0};` – Thomas Sablik Jan 07 '20 at 13:07
  • Your code snippet is C++ code. It does not compile with a C compiler. Please remove the C tag. – Thomas Sablik Jan 07 '20 at 13:12
  • In practice, `atlag` should memorize the maximum average temperature – Damien Jan 07 '20 at 13:23
  • Why do you think there is an error? I get the expected output with the given input: https://wandbox.org/permlink/JSkbUUFBAZsgKMzu – Thomas Sablik Jan 07 '20 at 13:23
  • @ThomasSablik It may work here because the maximum average temperature corresponds to the last row in this example – Damien Jan 07 '20 at 13:25
  • @Damien A [mcve] should contain an example to reproduce the problem. – Thomas Sablik Jan 07 '20 at 13:25
  • @ThomasSablik I did not say the contrary :) – Damien Jan 07 '20 at 13:27
  • 1
    @Damien Of course, you are right, but I don't like if people dump their code with "It doesn't work. Please fix it" without any effort. At least OP should provide problematic input and describe the problem. – Thomas Sablik Jan 07 '20 at 13:29
  • 1
    In the loop `for(int i = 0; i < N; i++) { aveh = sumh[i]/M; }` you are repeatedly setting `aveh` but the value is overwritten in the next loop iteration and only the last value is stored in the variable. The loop is the same as `aveh = sumh[N-1]/M;` – Thomas Sablik Jan 07 '20 at 13:40
  • In your example you define a variable `summh` but you use a variable `sumh`. Please provide actual code that you compiled and tested. – Thomas Sablik Jan 07 '20 at 13:52
  • It was pretty much solved. Thank you for your help! – Melkor Jan 07 '20 at 16:15

0 Answers0