I'm trying to solve the problem of creating a two-dimensional dynamic array. But it produces an error on the line arr[h][g] = rand() % 20; (second function). Tell me what's wrong.

 #include "stdafx.h" #include <iostream> #include <time.h> #include <iomanip> #include <cstdlib> int **stvor(int h,int g); void organiz(int h, int g, int **arr); void del(int h, int g, int **arr); using namespace std; int **stvor(int h, int g){ int **arr = new int *[h]; for (int i = 0; i < h; i++) { arr[i] = new int[g]; } return arr; } void organiz(int h, int g, int **arr) { for (int i = 0; i < h; i++) for (int j = 0; j < g; i++) { arr[h][g] = rand() % 20; cout << setw(6) << arr[h][g]; } return; } void del(int h, int g, int **arr) { for (int i = 0; i < h; i++) { delete[]arr[g]; } delete[]arr; } int _tmain(int argc, _TCHAR* argv[]) { srand((signed)time(NULL)); int **mas; int n, m; cout << "input m = "; cin >> m; cout << "input n = "; cin >> n; cout << endl; mas = stvor(m, n); organiz(m, n, mas); del(m, n, mas); system("pause"); return 0; } 

Closed due to the fact that off-topic participants Abyx , αλεχολυτ , Denis Bubnov , Pavel Parshin , Vadim Ovchinnikov Jan 24 '17 at 9:23 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Abyx, αλεχολυτ, Denis Bubnov, Pavel Parshin, Vadim Ovchinnikov
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • @Harry, Unhandled exception at 0x00184ED9 in dinmas_2v.exe: 0xC0000005: access violation when writing at 0xFDFDFE09. - K.Mew
  • one
    Error / typo here: for (int j = 0; j < g; i++) , should be j++ - andy.37

2 answers 2

These two functions, organiz and del , contain errors.

In the first function instead of sentences

  arr[h][g] = rand() % 20; ^^^^^^ cout << setw(6) << arr[h][g]; ^^^^^^ 

must be

  arr[i][j] = rand() % 20; ^^^^^^ cout << setw(6) << arr[i][j]; ^^^^^^^ 

That is, you incorrectly specify the indexes.

And in this cycle typo

 void organiz(int h, int g, int **arr) { for (int i = 0; i < h; i++) for (int j = 0; j < g; j++) ^^^^ 

This is what this function might look like.

 void organiz(int h, int g, int **arr) { for (int i = 0; i < h; i++) { for (int j = 0; j < g; j++) { arr[i][j] = rand() % 20; cout << setw(6) << arr[i][j]; } cout << endl; } } 

In the second function should be

 void del(int h, int g, int **arr) { for (int i = 0; i < h; i++) { delete[]arr[i]; ^^^^ } delete[]arr; } 

The second parameter g not used and can be removed from the function declaration.

Also in this sentence in main should write

 srand((unsigned)time(NULL)); ^^^^^^^^ 

It is also an unfortunate decision to write identifiers in the form of Russian words in Latin letters. Use English words. Otherwise it is difficult to read the program.

  • thanks, corrected, but still does not work - K.Mew
  • @ K.Mew What does "not working" mean? - Vlad from Moscow
  • when starting, it displays the message: "Unhandled exception at 0x00FA5F49 in dinmas_2v.exe: 0xC0000005: access violation when writing at 0xFDFDFDFDD." - K.Mew
  • @ K.Mew You have an invalid index in the loop. See my updated answer. - Vlad from Moscow
  • Thank! You helped a lot - K.Mew

Do you have an error, it should be understood, not at compilation, but at execution? so it is understandable: you have created an array hxg , and assign values ​​to an element outside the array - arr[h][g] ...

Maybe you still mean

 void organiz(int h, int g, int **arr) { for (int i = 0; i < h; i++) for (int j = 0; j < g; j++) { arr[i][j] = rand() % 20; cout << setw(6) << arr[i][j]; } return; } 

Even in the nested loop, i++ needs to be changed to j++ .

Here is the final working code.

  • corrected, but still gives an error - K.Mew
  • See amended answer. - Harry