SOLUTION_2.2

SOLUTION_2.2 - CS 31 Introduction to Computer Science I UCLA Winter 2010 CS 31 Homework 2.2 John A Rohr Winter 2010 Homework 2.2 Solution Page 1

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

View Full Document Right Arrow Icon
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;
Background image of page 1

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

View Full DocumentRight Arrow Icon
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 = 5; // Number of elements per output line const string TITLE = "MATRIX INPUT AND OUTPUT"; const string DIMPMT = "Dimension:"; const string MELTPMT = "Matrix Elements:"; const string MHEAD = "Matrix:"; // ************************************************************************** // * * // * GLOBAL VARIABLE DECLARATIONS
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/08/2010 for the course COM SCI 31 taught by Professor Kinsey during the Spring '98 term at UCLA.

Page1 / 12

SOLUTION_2.2 - CS 31 Introduction to Computer Science I UCLA Winter 2010 CS 31 Homework 2.2 John A Rohr Winter 2010 Homework 2.2 Solution Page 1

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online