im trying to make an array that is (n x n) large depending on what value you enter, and then populate that array with random integers. Could you guys please help me with two problems?
-
Any values of n greater than 4 give "error at display" in the last rows, if i display what value it would have been if it wasn't an error it is the same across, and something huge like 20019238394
-
Why does it create the same number all the way across? Is it not generating new random numbers?
#include <iostream> #include <string> #include <cmath> #include <cstdlib> #include <time.h> using namespace std; int arrayCreate(int); int main(int argc, const char * argv[]) { int n; cout << "enter how big your array is (n) , it will be shown as (n x n)" << endl; cin >> n; cout << "------------------------------" << endl; arrayCreate(n); return 0; } int arrayCreate(int n){ srand (time(NULL)); int y=0, x=0; int original[x][y]; //putting in random values for (int y=0; y< n; y++){ for (int x=0; x< n; x++){ int check = (rand() % 9 + 1); if (check < 10) original[x][y] = check; else cout << "error"; } } //displaying those values for (y=0; y<n; y++){ for (x=0; x<n; x++){ if (original[x][y] < 10) cout << original[x][y] << " "; else cout << "error at display"; } cout << " y is " << y << endl;} return original[x][y]; }
Aucun commentaire:
Enregistrer un commentaire