Question

In: Computer Science

Exercise 2 — Matrix file The file format for the matrix will have the first line...

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

Solutions

Expert Solution

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


Related Solutions

Given the matrix A (2x2 matrix taking the first two column vectors from the input file),...
Given the matrix A (2x2 matrix taking the first two column vectors from the input file), compute the followings. Λ: the diagonal matrix whose diagonal elements are the corresponding eignevalues Λii = λi for i=1,2 R: the 2x2 matrix whose ith column vector is the eigenvector corresponding to λi for i=1,2 RΛRT: the matrix compositions 1/0: the comparison between A and RΛRT (is A= RΛRT?) Your output should have seven lines where the first two lines correspond to the 2x2...
Following is the normalized distance matrix for the first four records of the Excel file Credit...
Following is the normalized distance matrix for the first four records of the Excel file Credit Approval Decisions. Apply single linkage clustering to these records until only one option remains. What conclusions can you make from this analysis? Applicant 1 2 3 4 1 0 2.874 2.326 1.769 2 0 1.530 1.798 3 0 1.317 4 0
the assignment folder for this assignment contains a file called values.txt. The first line in the...
the assignment folder for this assignment contains a file called values.txt. The first line in the file contains the total number of integers which comes after it. The length of this file will be the first line plus one lines long. Implement a MinHeap. Your program should read in input from a file, add each value to the MinHeap, then after all items are added, those values are removed from the MinHeap. Create a java class called MinHeap with the...
Exercise 2.1.39 Let A be a 2×2 invertible matrix, with A = [a b c d]...
Exercise 2.1.39 Let A be a 2×2 invertible matrix, with A = [a b c d] Find a formula for A−1 in terms of a,b, c,d by using elementary row operations
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
Let B equal the matrix below: [{1,0,0},{0,3,2},{2,-2,-1}] (1,0,0 is the first row of the matrix B)...
Let B equal the matrix below: [{1,0,0},{0,3,2},{2,-2,-1}] (1,0,0 is the first row of the matrix B) (0,3,2 is the 2nd row of the matrix B (2,-2,-1 is the third row of the matrix B.) 1) Determine the eigenvalues and associated eigenvectors of B. State both the algebraic and geometric multiplicity of the eigenvalues. 2) The matrix is defective. Nonetheless, find the general solution to the system x’ = Bx. (x is a vector)
first matrix A [ 2 -1 3 ] [-4 0 -2 ] [2 -5 12 ]...
first matrix A [ 2 -1 3 ] [-4 0 -2 ] [2 -5 12 ] [4 0 4 ] amd b [2] [-2] [5] [0] solve for Ax=b using tan LU factorization of A
Checking Input File for correct line format. So for my C++ assignment, my professor says we...
Checking Input File for correct line format. So for my C++ assignment, my professor says we need to validate the Input File, if there are white lines and spaces, in which it will ignore them, separated by a comma, and that all data items are there. This will be used in a program where i will store these into an Array, and display them in First name, Last name, and Number of Votes. This is a sample of the txt...
Checking Input File for correct line format. So for my C++ assignment, my professor says we...
Checking Input File for correct line format. So for my C++ assignment, my professor says we need to validate the Input File, if there are white lines and spaces, in which it will ignore them, separated by a comma, and that all data items are there. This will be used in a program where i will store these into an Array, and display them in First name, Last name, and Number of Votes vertical columned categories. This is a sample...
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT