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.
#include <iostream>
usingnamespace 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;
}
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?
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?
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.
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.
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 = newint*[ 7 ]; // the array of pointers
matrix[0] = newint[ 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
}
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.
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.