es26-le03-handson

Upload: wilmarc

Post on 30-May-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 ES26-LE03-Handson

    1/3

    ES 26 Third Long ExamFirst Semester, AY 2009-2010

    Matrix Calculator

    Create a program that performs the following operations on three matrices (double) A, B and C of

    row and column sizes less than or equal to 50 on a command-line fashion:

    1. Program Operations (50 pts)a) Make/overwrite matrix A, B or C(15 pts)e.g. make matrix C: MAKE C. Ask the user

    to first input the number or rows and columns.

    b) Swap matrix A, B or C(15 pts)e.g. swap matrices A and B: SWAP A Bc) Display matix A, B or C (15 pts)e.g. show matrix C: DISP C. Display the number of

    rows and the number of columns followed by the matrix itself. An example would beMatrix A:

    Number of rows: 3

    Number of columns: 2

    0 1 23 4 55 6 7

    d) Exit the program (5 pts): EXIT

    2. Matrix Operations (50 pts). If an operation error occurs like when rows and columns oftwo matrices are not fit for an operation, issue an error and do not proceed but do notterminate the program. If a matrix is not yet initialized but is used in an operation, issue an

    error and do not proceed.

    a) Addition (15 pts)e.g. add A and B then put result to C: ADD A B Cb) Scalar multiplication(10 pts)e.g. multiply 78.5 with C then put result to B:SMUL C 78.5 B

    c) Transpose (15 pts)e.g. transpose A then put result to A: TRANS A Ad) Matrix multiplication (10pts)e.g. matrix multiply C and A then put result to B:

    MMUL C A B

    3. Row Operations (25-pt. Bonus)a) Multiplying any row by a non-zero constant(5 pts) e.g. multiply row 0 of matrix A by

    19.8: RMUL A 0 19.8. To convert a string to int use atoi().b) Interchanging any rows (10 pts) e.g. swap matrix A's row 0 and 1: SWAPR A 0 1

    c) Adding any two rows together (10 pts)e.g. add rows 1 and 1 of matrix A then putresult to row 1: ADDR A 1 1 1

    Other specifications:1) To show that your program is ready for an input/command, display >>. An example would

    be:>>ADD A B COperation successful!>>TRANS A BOperation successful!

    2) Issue an error message if an operation does not exist or the user has inputted an invalid

    command.3) Commands are case-sensitive. Hence, trans is not a valid command.4) Matrices are also case-sensitive. Thus, issuing ADD a b c is invalid. Issue an appropriate

  • 8/14/2019 ES26-LE03-Handson

    2/3

    error message.

    5) Assume that the user will always input correct arguments, except for those mentionedabove. Hence, you are ensured that the second argument for SMULC will always be a

    double. To convert a string to double, use atof().

    6) Other errors are trivial and are expected to be handled properly.7) You either get the full point for a given command or no point at all.

    8) A deduction of 3 points shall be given to each unhandled exceptions and/or user errorsspecified above.

    9) Write your name, section, time and date of exam on the source code like the ff:/*** Wilmarc D. Lopez* ES26 THWX1* September 15, 2009 (Wednesday)* 1:00pm-4:00pm*/

    10) Also print the same information on screen at the start of your program.11) This is an open everything exam.

    12) Anybody caught talking with anyone except me will automatically be given a grade of 5.0.

    Do not say I didn't warn you.13) A student is only allowed to leave the classroom once and only ifno student is already

    outside.14) When you're finished, call my attention.

    Tips:1) Please be careful on passing arrays to functions as they are called by reference especially on

    cases that an operation's result is placed on one of the operands e.g. ADD A B A. This one'stricky.

    2) You can make things easier by creating a three-dimensional array of doubles. Remember

    that using only one bracket refers to a two-dimensional array. Hence, you are actuallycreating an array of two-dimensional doubles.

    3) Remember that a string in C should always be ended with '\0' for you to use the functions instring.h properly, among others.

    4) Use memcpy() to copy a certain part of a string (substring) and just append '\0' to turn it into

    another string. The first parameter of memcpy() is the address (e.g. &array[3]) wherecopying begins.

    5) You can create a function that tells you what operation to do by passing the command e.g.ADD. This is of course just a suggestion and may not fit to your logic. Do what you think

    is best.

    6) Remember that only chars are enclosed in single quotes. So when you're refering to a

    number, never put any.7) Never use scanf() for string input.8) For your sake, please read everything carefully!

    Sample run:Wilmarc D. LopezES26 THWX1September 15, 2009 (Wednesday)1:00pm-4:00pm

    Matrix Calculator>>DISP AA is not yet initialized!>>MAKE A

  • 8/14/2019 ES26-LE03-Handson

    3/3

    Input number of rows: 51Size is out of bounds!>>MAKE AInput number of rows: 2Input number of columns: 2A[0][0] = 1A[0][1] = 2

    A[1][0] = 3A[1][1] = 4Operation successful!>>DISP AMatrix A:

    Number of rows: 3Number of columns: 2

    0 1 23 4 55 6 7

    >>ADD A B BB is not yet initialized!>>add A A AInvalid command!>>ADD a A AInvalid argument!

    >>SMUL A A A This command will never ever happen!>>EXITCInvalid command!>>EXITCiao mother/father! ^_^v

    "A C program is like a fast dance on a newly waxed dance floor by people carrying razors"-- Waldi Ravens