### help with a massive

Hi!
I need to write a program that creates 3 4*4 massives and fills them with random numbers (from 1 to 100). Also I need to figure out which of these 3 massives is the biggest and then output that program.
I don't know if "massive" is the right word for the thing I'm trying to explain, but it is so in my native language. Hope you understand!
Anyway, here's my program.
 ``1234567891011121314151617181920212223242526272829`` ``````#include #include using namespace std; #define maxN 4 #define maxM 4 int t[maxM][maxN]; int n, m; void input(){ do{ srand((unsigned)time(0)); int n = rand(); int m = rand(); }while((n > maxN) || (m > maxM)); } void output(){ for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { cout << t[i][j]; } } } int main() { input(); output(); return 0; }``````

Here I tried to do output only 1 massive, nothing else, and I even got that wrong.
I know, I'm an absolute beginner, so bear with me, please!
In input() you don't write anything to t. You should probably use a loop similar to that in output() but instead of printing the value of t[i][j] give it a random value.
Last edited on
Well, I tried this, but nothing changed.
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include #include using namespace std; #define maxN 4 #define maxM 4 int t[maxM][maxN]; int n, m; void input(){ do{ srand((unsigned)time(0)); int n = rand(); int m = rand(); }while((n > maxN) || (m > maxM)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { t[i][j] = rand(); } } } void output(){ for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cout << t[i][j]; } } } int main() { input(); output(); return 0; }``````
Ah I see know what you are doing. So this code tries to find a random size for the matrix.
 ``12345`` ``````do{ srand((unsigned)time(0)); int n = rand(); int m = rand(); }while((n > maxN) || (m > maxM));``````

If you seed the random number generator by passing the same seed to srand you will get the same sequence of numbers from rand(). On most implementations time(0) returns the current time in seconds so that means that you will be seeding with the same number for a whole second. So you will only test one new position each second. It will probably take many attempts (seconds) before you find a valid size.

As a rule you should only call srand once at the beginning of main.

Instead of using a loop you can use the modulo operator to generate the random numbers in the correct range directly.
 ``12`` ``````int n = rand() % (maxN + 1); int m = rand() % (maxM + 1);``````

If you don't know what the modulo operator does you should look it up. It is often used together with rand().
Last edited on
Okay, I did it like this, but it still isn't working:
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include #include using namespace std; #define maxN 4 #define maxM 4 int t[maxM][maxN]; int n, m; void input(){ int n = rand() % (maxN + 1); int m = rand() % (maxM + 1); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { t[i][j] = rand(); } } } void output(){ for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cout << t[i][j]; } } } int main() { srand((unsigned)time(0)); input(); output(); return 0; }``````
Please tell us what it is that doesn't work.
It compiles, but it doesn't output anything.
first, don't use global variables.
 ``12345678910111213141516171819202122232425262728293031`` ``````// // main.cpp // massives // // Created by Home on 6/10/12. // Copyright (c) 2012 __MyCompanyName__. All rights reserved. // #include //no global variables void generate(int[][]);//declare functions like this void print(int[][]); int main() { //code and the needed variables here return 0; } void generate(int[][]) { //stuff here } void print(int[][]) { //stuff here }``````
The problem is probably that you don't change the global variables m and n in input(). Remove the int from the two first lines of input() and it should work better.
Well, at least it shows something at least. But what I want is, to show it in a 4*4 way. Kind of like a square of numbers. I hope you understand what I'm saying.
add a line after #26 `cout << endl;`. After that have line 24 be something like: ` cout << t[i][j] << " ";`

To get even better looking results, include <iomanip> and use the setw function:

http://www.cplusplus.com/reference/iostream/manipulators/

(with setw, line 24 would be `cout << setw(4) << t[i][j];` or something like that)
Last edited on
Hmm, now it seems to be working some of the time. Sometimes it shows me nothing and when I try it a minute later, it shows me a perfect 4*4 alignment, just as I wanted.. And also, I forgot to ask before, how can I make the random numbers between 1 and 100.
Big thanks for the help!
If n or m equals 0 it will print nothing. If you want to make sure they are not 0 you can change how you generate the random numbers.
 ``12`` ``````n = 1 + rand() % maxN; m = 1 + rand() % maxM;``````
Last edited on
Yeah, but now the output is sometimes 5*5, which i don't want. And still sometimes it is 2*3 or 4*1 or something like that. How can I do it so that it will always print 4*4?
`1 + rand() % 4` should never give you 5, you must be doing something wrong. If you always want the size to be 4*4 you can just set it like so
 ``12`` ``````n = 4; m = 4;``````

Or am I missing something?
Yeah, sorry, it was my careless mistake. Anyway I now have 3 matrixes lined up. Now all I have to do is output the matrix with the biggest sum. But I don't have a clue how to do that.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394`` ``````#include #include using namespace std; #define maxN 4 #define maxM 4 #define maxX 4 #define maxY 4 #define maxA 4 #define maxB 4 int t[maxM][maxN]; int u[maxX][maxY]; int s[maxA][maxB]; int n, m, x, y, a, b; void input(){ n = 4; m = 4; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { t[i][j] = rand() % 100; } } } void input2(){ x = 4; y = 4; for ( int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { u[i][j] = rand() % 100; } } } void input3(){ a = 4; b = 4; for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { s[i][j] = rand() % 100; } } } void output(){ for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cout << t[i][j] << " "; } cout << endl; } } void output2(){ for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { cout << u[i][j] << " "; } cout << endl; } } void output3(){ for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { cout << s[i][j] << " "; } cout << endl; } } int main() { srand((unsigned)time(0)); input(); input2(); input3(); cout << "First matrix: \n"; output(); cout << "\n"; cout << "Second matrix: \n"; output2(); cout << "\n"; cout << "Third matrix: \n"; output3(); return 0; }``````

I know, I probably could have done it much more efficiently and shorter, but that seemed to be the easiest way for me.
Last edited on
bump
Topic archived. No new replies allowed.