In: Computer Science
Exercise 2 — Matrix file The file format for the matrix will have the first line contain 2 integers, which are the number of rows and columns. After this line, repeatedly count from 0 to 9, filling in the matrix size conditions as specified above. For example:
3 4
0 1 2 3
4 5 6 7
8 9 0 1
Randomly create different sized arrays with random numbers. There should be a space between each number. You will be using this file when reading data from a file. Hint: Consider using nested loops and modulus arithmetic (“%”) top help create this program. please help
As given in the question , the elements of the matrix must be between {0,1,2.....,9} and must be continuos and repititive. The numbers must follow the sequence 0,1,2,3,4,5,6,7,8,9 and then keep repeating. Also this pattern is followed while traversing the array in row - major form.
It is much easier to think of the algorithm if you are clear with row-major form traversal , which is traversing 1 row after another and moving from left to right.
Also , since there are only 10 elements in domain , they will appear after 10 positions in row-major traversal , irrespecitive of the size of the array.
Hence the numbers occur at following positions ,
0 - 0th ,10th , 20th , 30th.............(10*i)th position in row-major traversal.
1 - 1st , 11th , 21st , 31st............(10*i +1 )th position in row-major traversal
2 - 2nd , 12th , 22nd , 32nd.......... (10*i +2)th position in row-major traversal.
.....
.....
Similarly ,
9 - 9th , 19th , 29th ,39th .......... (10*i +9)th position in row-major traversal.
Let the array size be n*m.
Now , for any element at a position (i,j) in the array , its position in row-major traversal must be known.
1. Row number = i => all elements of all (i-1) rows have been visited = (i-1)*(m) elements.
2. Column number = j => it is the jth element to be visited in this row. = (j-1) elements visited.
Hence , in row-major traversal , the position of the element at (i,j) would be
= Elements visited before (i,j) + 1
= (i-1)*m + (j-1) + 1 = (i-1)*m + j
NOTE : - The above formula is correct mathematically , but in Programming labguages , indexes are 0 based hence for any element at a position (i,j) , the position in Row- Order traversal would be i*m + j.
Since , the position in row-major traversal is known now and there are 10 elements.
Hence , element at Xth position in row-major traversal = X%10 (can be verified from the table)
Here is working code of the same logic
#include <iostream>
using namespace std;
int main()
{
int n,m;
/* Generating random sizes , adding +1
so that it does not give 0 */
n = rand()%10 + 1;
m = rand()%10 + 1;
int arr[n][m];
cout<<n<<" "<< m<<endl;
/* Run a nested for loop */
for(int i = 0 ; i < n ; i++)
{
for(int j = 0 ; j < m ; j++)
{
/* Find the position in the row-major traversal */
/* NOTE - position is (i*m) + j because , the indexes are 0-based */
int position = i*m + j;
/* Take modulo with 10 */
int number = position%10;
/* Assign the number */
arr[i][j] = number;
/* Print the number */
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
}
Here is a sample output