Newbie not sure what they are asking for.

Hello,
Newbie here. Ive been assigned a piece of code to design that I dont know exactly what they are asking for. They are asking me to produce a matrix of 0's and 1's in a three by three format using a integer of N that produces those 0's and 1's randomly. Im supposed to use the void printmatrix(int n). Im not asking for any code Im asking What do they mean by a matrix? Im not trying to sound like an idiot. But I kind of am. I dont know if its some kind of math or binary or something else entirely.

This is what I've been able to come up with so far. Not really looking for code help but just knowledge help. This is mainly just the base of the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
using namespace std;


void Matrix (int n)
{  
if 
}

int main()
{

  double n;

  cout << "This is a program\n";
  cout << "It is designed to produce a matrix from a integer\n";
  cout << "Please input a number or integer: ";
  cin >> n;

  cout << "The matrix is :  ";
  Matrix (n);
  return 0;
}
A 'matrix' is a math term to refer to a two dimensional array.

What you are being asked to do is create a 2D array and randomly fill it with zeros and ones.

What you have written does not make it clear to me whether there should be N zeros (or ones) or whether your matrix's dimensions should be N by N.
The assignment states
"(Display matrix of 0's and 1s) Write a function that displays an n by n matrix using the following header: void printMatrix(int n) :
Each element is 0 or 1, which is generated randomly. Write a test program that prompts the user to enter n and displays it in a n by n matrix.

Then it shows a sample run where:
it says enter n: 3, then it produces
010
000
111

So Im taking this to mean I need to create a array of some sort that produces a 3 by 3 square of 1's and 0s?
Last edited on
No, your matrix must be a 2D array of N rows and N columns. If the user enters 3, it is a 3 by 3 matrix (9 elements total). If the user enters 5 it is a 5 by 5 matrix (25 elements total).

Every element of the matrix is randomly 0 or 1.


Playing with 2D arrays is a messy thing, because there are so many different ways to look at it. I presume your professor has given you notes on how to create arrays of variable size?
Last edited on
Notes? HA HA HA HA. Not really. He says read the book and you'll figure it out. But ok. I'll read up on a 2d array and see what I can come up with. Thank you.
Seriously?

Well, the basic options are:
- Just create a big array and only use part of it
- Dynamically allocate an array of dynamically allocated arrays (the most common answer).
- Dynamically allocate a 1D array which you index as if it were a 2D array.

My favorite cool trick is actually this:
http://stackoverflow.com/questions/29375797/copy-2d-array-using-memcpy/29375830#29375830

It works by creating a 1D array of values, then creating an array of pointers that reference the correct positions in the 1D array of values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
  int ** matrix = nullptr;

  // create a 7 by 9 array of ints
  matrix = new int*[ 7 ];  // the array of pointers
  matrix[0] = new int[ 7 * 9 ];  // the array of values
  for (int i = 1; i < 7; i++) matrix[i] = matrix[i-1]+9;  // update the array of pointers

  // access an element
  matrix[5][3] = 72;

  // when done, remember to delete[] both arrays:
  delete[] matrix[0];  // the array of values
  delete[] matrix;  // and the array of pointers
}

Hope this helps.
Ok, I think I see it. Each integer is to lay out the number of rows and columns in your example. Then I just need to put what goes into those integers with a rand that is limited to 0 and 1. This should generate a random set of ones and zero's.
Now I just need to remember how to limit the rand function. Thank you.
And yes. That is what he says.
Last edited on
How to use rand()
http://www.cplusplus.com/faq/beginners/random-numbers/

To get 0 or 1, you could just rand() % 2. Just be aware that it isn't as random as it could be.
Yes, I remember reading about that on here. That rand isnt actually very random. Then what is?
A good RNG is. Rand is not a very good RNG, particularly in the low bits. You'd probably get a better random sampling from bit 6 or 7 instead of bit 0.

Since you are using C++, you could also use one of the generators in the <random> library. But, to be frank, that's probably complete overkill for your assignment.
Topic archived. No new replies allowed.