assign5/mat.cpp
// read a matrix in.
// first two numbers are the number of rows and columns.
// then the matrix values by row.
// will deallocate old array if different size.
read a matrix in.
first two numbers are the number of rows and columns.
then the matrix values by row.
will deallocate old array if different size.
read a matrix in . first two numbers are the number of rows and columns . then the matrix values by row . will deallocate old array if different size .
void Matrix::read()
{
    int r, c;
    int numread;
    numread = scanf("%d", &r);
    if (numread==EOF) {
        if (name.length()==0) {
            printf("ERROR(read): Trying to read a matrix but end of file was found\n");
        }
        else {
            printf("ERROR(read): Trying to read matrix named \"%s\" but end of file was found\n", name.c_str());
        }
        exit(1);
    }
    if (numread!=1) {
        printf("ERROR(read): number of rows was not a valid integer.  First character is '%c'\n", getchar());
        exit(1);
    }
    numread = scanf("%d", &c);
    if (numread==EOF) {
        if (name.length()==0) {
            printf("ERROR(read): Trying to read a matrix but end of file was found\n");
        }
        else {
            printf("ERROR(read): Trying to read matrix named \"%s\" but end of file was found\n", name.c_str());
        }
        exit(1);
    }
    if (numread!=1) {
        printf("ERROR(read): number of columns was not a valid integer.  First character is '%c'\n", getchar());
        exit(1);
    }
    if (maxr!=r || maxc!=c) {
        deallocate();
        reallocate(r, c, "");
    }
    for (int r=0; r<maxr; r++) {
        for (int c=0; c<maxc; c++) {
            numread = scanf("%lf", &(m[r][c]));
            if (numread==EOF) {
                if (name.length()==0) {
                    printf("ERROR(read): Trying to read element [%d, %d] of a matrix but end of file was found\n", r, c);
                }
                else {
                    printf("ERROR(read): Trying to read element [%d, %d] of matrix named \"%s\" but end of file was found\n", r, c, name.c_str());
                }
                exit(1);
            }
            if (numread!=1) {
                printf("ERROR(read): invalid number when trying to read row: %d and col: %d.  First character is '%c'\n", r, c, getchar());
                exit(1);
            }
        }
    }
    defined = true;
}
void Matrix :: read ( ) { int r , c ; int numread ; numread = scanf ( " " , & r ) ; if ( numread == EOF ) { if ( name . length ( ) == 0 ) { printf ( " \n " ) ; } else { printf ( " \" \" \n " , name . c_str ( ) ) ; } exit ( 1 ) ; } if ( numread != 1 ) { printf ( " \n " , getchar ( ) ) ; exit ( 1 ) ; } numread = scanf ( " " , & c ) ; if ( numread == EOF ) { if ( name . length ( ) == 0 ) { printf ( " \n " ) ; } else { printf ( " \" \" \n " , name . c_str ( ) ) ; } exit ( 1 ) ; } if ( numread != 1 ) { printf ( " \n " , getchar ( ) ) ; exit ( 1 ) ; } if ( maxr != r || maxc != c ) { deallocate ( ) ; reallocate ( r , c , " " ) ; } for ( int r = 0 ; r < maxr ; r ++ ) { for ( int c = 0 ; c < maxc ; c ++ ) { numread = scanf ( " " , & ( m [ r ] [ c ] ) ) ; if ( numread == EOF ) { if ( name . length ( ) == 0 ) { printf ( " \n " , r , c ) ; } else { printf ( " \" \" \n " , r , c , name . c_str ( ) ) ; } exit ( 1 ) ; } if ( numread != 1 ) { printf ( " \n " , r , c , getchar ( ) ) ; exit ( 1 ) ; } } } defined = true ; }
read a matrix in.
read a matrix in .