Cpsc 440 Problem Set 2 Question 2

Cpsc 440 Problem Set 2 Question 2 - hw2p2.h#ifndef...

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

View Full Document Right Arrow Icon
hw2p2.h 2/27/2008 # ifndef HW2P2_H # define HW2P2_H typedef struct matrix * matrix ; matrix newMatrix ( int row , int col ) ; void freeMatrix ( matrix m ) ; double getElement ( matrix m , int row , int col ) ; void setElement ( matrix m , int row , int col , double value ) ; void printMatrix ( matrix m , FILE * out ) ; void printDiagonalizationResults ( double * s , double * u , int n , FILE * out ) ; void printFullDiagonalizationResults ( double * s , double * u , int n , FILE * out ) ; void jacobi ( double * a , int n , double * s , double * u ) ; double sumOffDiagonal ( matrix m ) ; void performJacobiRotation ( matrix A , matrix V , int p , int q ) ; void performSweep ( matrix A , matrix V ) ; void sortEigenvalues ( matrix A , matrix V ) ; # endif 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
hw2p2.c 4/21/2008 # include < math . h > # include < stdio . h > # include < stdlib . h > # include "hw2p2.h" # define PI 3.14159265358979323846 # define ERROR_TOLERANCE 10e-12 struct matrix { int num_rows ; int num_cols ; double * elements ; } ; matrix newMatrix ( int num_rows , int num_cols ) { matrix m = malloc ( sizeof ( struct matrix ) ) ; m - > num_rows = num_rows ; m - > num_cols = num_cols ; m - > elements = malloc ( m - > num_rows * m - > num_cols * sizeof ( double ) ) ; for ( int i = 0 ; i < m - > num_rows * m - > num_cols ; i + + ) { m - > elements [ i ] = 0 ; } return ( m ) ; } void freeMatrix ( matrix m ) { free ( m - > elements ) ; free ( m ) ; } double getElement ( matrix m , int row , int col ) { return ( m - > elements [ ( row - 1 ) * m - > num_rows + ( col - 1 ) ] ) ; } void setElement ( matrix m , int row , int col , double value ) { m - > elements [ ( row - 1 ) * m - > num_rows + ( col - 1 ) ] = value ; 1
Background image of page 2
hw2p2.c 4/21/2008 } void printMatrix ( matrix m , FILE * out ) { for ( int r = 1 ; r < = m - > num_rows ; r + + ) { printf ( "|" ) ; for ( int c = 1 ; c < = m - > num_cols ; c + + ) { if ( c ! = 1 ) { printf ( "\t" ) ; } printf ( "%13.9f" , getElement ( m , r , c ) ) ; } printf ( "|\n" ) ; } for ( int r = 1 ; r < = m - > num_rows ; r + + ) { fprintf ( out , "|" ) ; for ( int c = 1 ; c < = m - > num_cols ; c + + ) { if ( c ! = 1 ) { fprintf ( out , " " ) ; } fprintf ( out , "%13.9f" , getElement ( m , r , c ) ) ; } fprintf ( out , "|\n" ) ; } } int main ( int argc , char * argv [] ) { // Extract the command-line arguments if ( argc ! = 2 ) { printf ( "\nERROR: %s expects 2 arguments, given %i\n" , argv [ 0 ] , argc ) ; return ( 1 ) ; } char * output_file_name = argv [ 1 ] ; // Open the output file printf ( "\nAttempting to open file %s for writing. ..\n" , output_file_name ) ; 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
hw2p2.c 4/21/2008 FILE * out ; out = fopen ( output_file_name , "w" ) ; if ( out = = NULL ) { printf ( "Failed to open file: %s for writing.\n" , output_file_name ) ; fclose ( out ) ; return ( 2 ) ; } printf ( "Successfully opened file: %s for writing.\n" , output_file_name ) ; fprintf ( out , "Successfully opened
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

Cpsc 440 Problem Set 2 Question 2 - hw2p2.h#ifndef...

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

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