[try Beta version]
Not logged in

 
help with a massive

Jun 10, 2012 at 1:07pm
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<ctime>
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!
Jun 10, 2012 at 1:19pm
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 Jun 10, 2012 at 1:20pm
Jun 10, 2012 at 1:34pm
Well, I tried this, but nothing changed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<ctime>
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;
}
Jun 10, 2012 at 1:47pm
Ah I see know what you are doing. So this code tries to find a random size for the matrix.
1
2
3
4
5
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.
1
2
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 Jun 10, 2012 at 1:49pm
Jun 10, 2012 at 4:44pm
Okay, I did it like this, but it still isn't working:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<ctime>
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;
}
Jun 10, 2012 at 4:50pm
Please tell us what it is that doesn't work.
Jun 10, 2012 at 4:55pm
It compiles, but it doesn't output anything.
Jun 10, 2012 at 4:57pm
first, don't use global variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
//  main.cpp
//  massives
//
//  Created by Home on 6/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>

//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
}
Jun 10, 2012 at 5:59pm
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.
Jun 10, 2012 at 7:02pm
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.
Jun 10, 2012 at 7:31pm
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 Jun 10, 2012 at 7:33pm
Jun 10, 2012 at 8:46pm
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!
Jun 10, 2012 at 8:50pm
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.
1
2
n = 1 + rand() % maxN;
m = 1 + rand() % maxM;
Last edited on Jun 10, 2012 at 8:50pm
Jun 10, 2012 at 9:12pm
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?
Jun 10, 2012 at 11:02pm
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
1
2
n = 4;
m = 4;

Or am I missing something?
Jun 11, 2012 at 4:23pm
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<iostream>
#include<ctime>
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 Jun 11, 2012 at 4:24pm
Jun 11, 2012 at 7:38pm
bump
Topic archived. No new replies allowed.