Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#ifndef MATRIX_TYPE #define MATRIX_TYPE #include using std::string; #include using std::vector; /* very convenient. If you want to change the type stored in the matrix, you

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

#ifndef MATRIX_TYPE #define MATRIX_TYPE #include using std::string; #include using std::vector; /* very convenient. If you want to change the type stored in the matrix, you only have to change the single template type in matrix_row */ using matrix_row = vector; using matrix = vector; /* nicely print a matrix. Should have row/column alignment converts it to a string (doesn't print to cout!!!) uses width to space elements (setw). Default is 3 */ string matrix_to_str(const matrix &m1, size_t width=3); /* true if the two matrices have the same shape false otherwise */ bool same_size(matrix &m1, matrix &m2); /* matrices must not be empty and must be the same shape: - if true, return a new matrix that adds m1+m2 - if false, return an empty matrix (no elements) */ matrix add(matrix &m1, matrix &m2); /* matrix must not be empty: - if true, multiply T scalar value by m - if false, return empty matrix (no elements) */ matrix scalar_multiply(matrix &m, long val); #endif
CSE 232, Lab Exercise 7 Command Line Process Management (&, bg/fg) So far, we've been teaching you how to run programs at the command line. Running the program and waiting for it to finish works okay for short/fast programs, but if you have a program that takes minutes or hours to finish, you don't want to have to wait that long to do other things at the terminal. Instead, there is a way to run the program in the background, so that it is running but not blocking your access to the terminal Executing a Background Joh Let us say we have a long-running program called a.out. Normally you would run the program like so ./a.out But if you did that your terminal would be blocked until it finished. No commands can be executed until it is done. If instead you add a ampersand (&) after the command, the execution is run in the background ./a.out& The job, a.out, will now run but the terminal is freed for your commands. What jobs do I have? You can run the command jobs to see the status of your suspended (and running) jobs It reports it in the following way (I have a job called top running in background) >jobs [1+ Stopped top The [1] is your local job number. We will use that in a minute Suspending a Running Job and Running in the Background Lets say you already were running a.out in the foreground (without the ampersand). You already know about CTRL-C to kill a running program. Here's a new one, CRTL-Z CNTRL-Z doesn't kill a program, but instead suspends it (pauses it), giving you back control of your terminal. Whatever was running is now stopped, but can be restarted. After suspending a job, you can have the suspended job run in background using the command bg Take a Background/Suspended Job and Run It In The Foreground If you want to see the output from a suspended/background job, you can do so with the fg command. By default, fg makes the most recent background job run in the foreground. But if you supply the job ID number (from the jobs command), you can select a specific background job to be moved to the foreground like so: fg %1 Killing Background Jobs To kill a specific background job, use the kill command and the job id. Like so: kill %2 For example, the top command tells you about the present status of your computer. It continuously prints info about processes, memory usage etc. I can do the following: Make a program that outputs all the numbers between 1 and 1 million. Run the program in the background and demonstrate moving it to the foreground and killing it to the TA The Problem We are going to work on 2D vectors. 2D vector as a matrix You remember matrices, don't you? We are going to do some simple manipulation of a matrix, namely: adding two matrices and multiplying a matrix by a scalar. You watched the 2D vector video, right? RIGHT??2? Matrix A matrix is a 2-dimensional (rows and columns) data structure. It has a shape indicated by the number of rows and the number of columns. Though I suppose a matrix could have uneven sized rows, this doesn't usually happen in practice so a matrix is always rectangular, potentially square (based on its shape). rows shape - rows x cols (here 3 x cols Matrix operations We will perform two operations on our matrices, yielding a new matrix as a result. The first is scalar multiplication. Regardless of the size or shape, if the matrix is not empty we multiply the scalar value by every entry in the matrix, yielding a new matrix. We do this for every entry in the matrix. The second is addition. The shape of the two matrices must be the same for addition to go forward. If the shapes are the same and they are both not empty, we add the same row/col element of each argument matrix into the same row/col element of a new matrix, yielding the new matrix. We do this for every element in the two matrices. x 3 12 15 18 21 24 27 Scalar Multiplication 2 8 10 12 7 8 9 7 8 9 14 16 18 Addition of two matrices Requirements We provide a lab07_functions.h As always, you will submit a lab07_functions.cpp to Mimir for testing. Write your own main to test your code We will use a vectorvector> as the underlying representation of our matrix. This means that the top level vector has, as elements, another vector In lab07_functions.h we provide two using definitions to make things a little easier, to wit: using matrix-row vector, using matrix - vector; This is really a big win! We need only say that the type of vector in a matrix_row is a long and then, if we are careful, can easily change the type of our entire code set by just changing that one template Function Declarations The functions are clearly described in the lab07_functions.h file provided, read them there Printing I think printing the 2D matrix is actually kind of hard. Here are some tips to help out In the include file ioma nip s an io manipulator setw. It sets the width for an output element: * Unlike every other manipulator, it requires you to run it each time you use it. . If you say something like cout using std: :vector very convenient. If you want to change the type stored in the matrix, you only have to change the single template type in matrix_row using using matrix-row-vector: matrix = vector(matrix-row>; nicely print a matrix. Should have row/column alignment converts it to a string (doesn' t print to cout!! !) uses width to space elements (setw). Default is 3 string matrix to str(const matrix &ml, size t wid true if the two matrices have the same shape false other wise bool same size(matrix &m1, matrix &m2) matrices must not be emp ty and must be the same shape: - if true, return a new matrix that adds ml+m2 if false, return an empty matrix (no elements) matrix add(matrix &ml, matrix &m2) matrix must not be empty: - if true, multiply T scalar value by m if false, return empty matrix (no elements) matrix scalar multiply(matrix &m, long val) # endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions