-5

Below is the code, which is breaking my head since two days.

#include <iostream>
using namespace std;
int main()
{
    int N;
    int A[1][N-1];
    int B[1][N-1];

    std::cout << "ENTER NO OF ROUND" << '\n';
    std::cin >> N;
    for (int j=0; j<N; j++)
    {
            int i =0;
            std::cout << "enter the scores" << '\n';
            std::cin >> A[i][j]>>A[i+1][j];

            if (A[i][j] > A[i+1][j])
            {
                    B[i][j] = A[i][j] - A[i+1][j];
                    B[i+1][j] = 1;

            }
            if (A[i][j] < A[i+1][j])
            {
                    B[i][j] = A[i+1][j] - A[i][j];
                    B[i+1][j] = 2;
            }

    }
    std::cout << A[0][0]<<A[1][0] << '\n';
    return 0;
}

Here in line 18 line 19 and line23 line24i should get difference of two elements of array A[1][N-1] Which is then assigned to a element in array B[1][N-1],but am unable get the correct result ,rather am getting a random number. help me getting through this

Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
Samdare
  • 11
  • 2
  • 3
    `int N;` - Okay, so you declare an `N`, but what's its value for you to use it in the very next line? – StoryTeller - Unslander Monica Feb 07 '18 at 09:37
  • 2
    `[N-1]` is an evil extension since it's not a compiler time constant. Don't do that. use `constexpr int N` or std::vector depending on your plan – UKMonkey Feb 07 '18 at 09:38
  • 1
    You need to pick up a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Uninitialized and not-a-compile-time-constant `N` is not the only problem, you're accessing out of bounds when you do `A[anything_but_0]`, same with `B`... – LogicStuff Feb 07 '18 at 09:38
  • What is the value of the `N` variable in line 6 and line 7? – Jabberwocky Feb 07 '18 at 09:39
  • Aside: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – UKMonkey Feb 07 '18 at 09:40
  • 1
    I'd also add to UKMonkey's last comment that you don't even use that `using` directive, so what's the point of having it at all ? (btw, if you're compiling with clang or gcc, at the very least, turn on -Wall which includes -Wunitialized, that would have given you a warning on that unitialized N, on other compilers there should be similar options) – Caninonos Feb 07 '18 at 09:45
  • thanks @StoryTeller – Samdare Feb 07 '18 at 09:45
  • Your error most likely comes from reading from and writing to uninitilized data, as @LogicStuff has pointed out. I explained it below. – Mateusz Grzejek Feb 07 '18 at 09:54

2 Answers2

3

You use uninitialized data, so anything can happen.

You declare the N variable:

int N;

And then, in the very next line, without assigning any value, you use it to create two arrays using N as a size:

int A[1][N-1];
int B[1][N-1];

This already is a starting point for disaster. Moreover, declaring an array with size [N - 1] is not technically correct - N is a variable, so cannot be used to declare array in this manner. It's a compiler extension (are you using Visual Studio?). If value of N is known, declare it as:

static constexpr size_t N = value;

If it is read at runtime, create your arrays with new or, much better, use std::vector to make your code robust and less error-prone.

The second thing is, that A array is declared as int A[1][N-1];, but you do the following:

int i = 0;
....
if (A[i][j] > A[i+1][j])

Which results in reading A[1][j], which does not exist - in C++ we start indexing from 0! You also modify those non-existing elements:

std::cin >> A[i][j] >> A[i+1][j];

which most likely will result in writing to memory that belongs to B array.


Small notes:

  • using namespace std seems redundant, as you write std:: everywhere
  • what is the point of two-dimensional array [M][N] if M = 1?
Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
  • i am using ATOM editor ,after declaring the `int N` i have added the `cin` for `N`,after that i have declared the two arrays `A[1][N-1]`and `B[1][N-1]`,this working for me ,thanks @Mateusz Grzejek – Samdare Feb 07 '18 at 09:54
  • @Samdare Consider accepting an answer that best addresses your question. – Mateusz Grzejek Feb 07 '18 at 10:12
1

Use std::vectors if you need arrays with size determined at run-time. The C Variable-length arrays you use are not supported in C++.

std::cout << "ENTER NO OF ROUND" << '\n';
int N = 0;
std::cin >> N;
std::vector<std::vector<int>> A(2, std::vector<int>(N));
std::vector<std::vector<int>> B(2, std::vector<int>(N));

And note that you need 2 x N array because you read both A[i][j] and A[i+1][j] and your for loop is from [0 to N-1] -- N times.

Mihayl
  • 3,821
  • 2
  • 13
  • 32