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.
#include<iostream>
#include<ctime>
usingnamespace 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.
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().
//
// 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
}
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.
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!
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?
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.