{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

SOLUTION_2.2

# SOLUTION_2.2 - CS 31 Introduction to Computer Science I...

This preview shows pages 1–3. Sign up to view the full content.

CS 31: Introduction to Computer Science I: Winter 2010: Homework 2.2 Solution Page 1 // UCLA Winter 2010 // CS 31 Homework 2.2: Matrix Operations // John A. Rohr // February 20, 2010 // // // This program performs input and output of square matrices. // // A square matrix of dimension n consists of n*n elements // arranged in rows and columns. // // The program handles square matrices of dimension 1 to 10. // // The program can hold one square matrix at a time. // // The program first reads a square matrix dimension. If the dimension is // negative, the program terminates. If the dimension is zero or too large, // the program reads the dimension again. If the dimension is valid, the // program reads values for each of the elements of the vector, then displays // the vector. // // For input, values are read row by row First all the values for all the // elements in the first row are read, then the second row, etc. until the // last row is filled. // // For output, if the dimension is no greater than the global constant value // ELEMENTS_PER_LINE, each row is displayed on one line. // // Otherwise, ELEMENTS_PER_LINE values are displayed on each line until // the end of the first row is reached. A blank line is then displayed, // followed by the rest of the rows in the same format, each followed // by a blank line except for the last row. // // The program continues until a negative dimension is input. // // All input is read from the file Hw22.In if it can be opened. Otherwise all // input is read from the keyboard. All output is written to cout and the file // Hw22.Out if it can be opened. Otherwise all output is written to cout only. // // If the end of the input file is encountered, the program terminates // immediately with output of a blank line and a termination message. #include <iostream> #include <fstream> #include <iomanip> using namespace std;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS 31: Introduction to Computer Science I: Winter 2010: Homework 2.2 Solution Page 2 // ************************************************************************** // * * // * GLOBAL CONSTANTS * // * * // ************************************************************************** const string INFILE_NAME = "Hw22.In"; const string OUTFILE_NAME = "Hw22.Out"; const int MAX_DIM = 10; // Maximum matrix dimension const int PRECISION = 4; // Precision of double output const int OUTPUT_WIDTH = 12; // Width of double output const int ELEMENTS_PER_LINE =
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern