Error executing the program in c ++ of arrays

2

I go back to the community in search of help and learn, the code is not finished but when compiling and executing, when I do the test it throws me an error, can someone tell me what is wrong ?, in the faculty I made the same code and in the version of dev c ++ 5.11 throws me that error, and in an older version I was walking perfectly.

#include <iostream>
#include <windows.h>

using namespace std;

int main () {
    int a,b,fila,columna;
    int matriz [fila][columna];
    system ("color F3");
    cout << "\t\t\t PROGRAMA DE MATRIZ EJ 1 C++" << endl << endl;
    cout << "Ingrese el valor de filas que quiere que tenga la matriz: "; cin >> fila;
    cout << "Ingrese el valor de columnas que quiere que tenga la matriz: "; cin >> columna;
    cout << "Ingrese el valor de los elementos de la matriz: " << endl;

    for (a = 0 ; a < fila ; a++) {
        for (b = 0 ; b < columna ; b++) {
            cout << "Fila " << "[" << a << "]" << " " << "columna " << "[" << b << "]: ";
            cin >> matriz[a][b]; cout << endl;
        }
    }
    system ("pause");
}
    
asked by Sham del Prado 05.06.2016 в 09:10
source

3 answers

1

Good, I'm not an expert but I've seen a couple of things that I have not liked ...

- Uninitialized matrix: int a, b, row, column; int array [row] [column];

matrix you do not have it defined to anything: It is a pointer to a two-dimensional array of size or random or 0 (depends on the compiler uninitialized variables are set to 0 or left with "garbage")

A priori I would say that I should never have executed you, I do not know if some old compiler would catch you garbage for the initial values of row and column and something would do to you, but I do not see it clearly.

2 solutions:

- Dynamic memory: Definition int ** matrix; Once you know the dimensions of the matrix: matrix = malloc (ablablalbla you should know how it goes :)) Example with google apps script : It's not the same language but it will be easy to convert it

- Static memory: int matrix [100] [100] and that the dimensions that the user enters never exceed 100 or the value that you put ... the problem is that it depends on what values you put you will have no battery memory, so it depends on the case better dynamic memory. Example of static matrix in C

Apart from what I have told you previously ...

- Deprecated calls to system: system ("color F3"); I would not use this call, I do not think it gives you problems, but system calls for this kind of things tend to give more problems than anything else.

    
answered by 05.06.2016 / 13:51
source
2

Part of the answer has already been given Pako LordPakus ; so I'll go on to expand it and guess your problem, since you do not share that the error shows you that I'm sure it's very revealing.

  

In the faculty I made the same code and in the version of dev c ++ 5.11 throws me that error, and in an older version I was walking perfectly .

To start main has no return, usually this is marked as alarm (warning) but different versions of compiler or different compilers can treat it as an error , add return 0; to the end of your function main .

You use Variable Length Arrays (VLA in English ), which do not belong to the standard C ++ if not they are an extension of the compiler (Read about the extension in the GCC compiler ). Each compiler has its own extensions so that if you use a compiler in the faculty and in your later compilations you use another, this extension could be disabled or not exist and would show you an error .

On the other hand, your code shows certain lacks of style, understanding style as non-obligatory but useful recommendations:

  • Scope of variables greater than necessary : the variables a and b are only used in loops for , they would be better declared in the loop itself to limit its scope.
  • Names of unnecessarily short variables : naming variables such as a and b provides very little information about their mission or intended use; avoid using single-character variable names and look for self-explanatory names.
  • Variables not initialized : The response of Pako LordPakus is excellent. I will add that in the test that I have done, in the compiler that I tested your code fila and columna are created with the value 2'147'483'647 ( 0x7fffffff ) with what you try to create a VLA of several million megabytes which overflows the stack at run time. In the case that fila and columna get lower values than those captured in cin , during the writing you would leave the memory of the fix (since matriz is created before to capture values of its size) and anything could happen: it could work, not work, the program could be aborted at runtime or demons could shoot out of your nostrils .
answered by 06.06.2016 в 09:32
1

In these cases, what can be done is to declare a vector data type, putting the #include <vector> library in the headers. Anyway, there is an error in the order of the sentence. You declare an array int matriz [fila][columna]; before reading the row and column variables. Remember that programs run always from top to bottom. and to declare them without having read them before will throw away an error, because the arrangements must have a capacity. Instead of using an array in C ++, you can use the following vector syntax vector<int> nombreMatriz(fila*columna); The requirement for this is that the #include<vector> library is declared and that the vector declaration is always after read the capacity of the vector. Another important thing is to declare the variables at the beginning of the program The program would be as follows:

#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;

int main(){
    int a,fila,columna;
    system ("color F3");
    cout << "\t\t\t PROGRAMA DE MATRIZ EJ 1 C++" << endl << endl;
    cout << "Ingrese el valor de filas que quiere que tenga la matriz: "; cin >> fila;
    cout << "Ingrese el valor de columnas que quiere que tenga la matriz: "; cin >> columna;

    vector<int> Matriz(fila*columna);

    cout << "Ingrese el valor de los elementos de la matriz: " << endl;

    for(a = 0; a < fila*columna ; a++)
        cin >> Matriz[a];      

    cout << "Matriz introducida:\n";

    for(a = 0; a < fila*columna ; a++ )
        cout << "Fila " << "[" << a << "]" << " " << "columna " << "[" << a << "]: " << Matriz[a] << endl; 
    system ("pause");
    return 0;
}
    
answered by 10.06.2017 в 19:03