new file: assignment1/Summer_Assignment1_2014.pdf
new file: assignment1/assignment1_reading_sample.cpp new file: assignment1/assignment1_reading_sample_iostream.cpp new file: assignment1/matrix1.txt
This commit is contained in:
BIN
assignment1/Summer_Assignment1_2014.pdf
Normal file
BIN
assignment1/Summer_Assignment1_2014.pdf
Normal file
Binary file not shown.
55
assignment1/assignment1_reading_sample.cpp
Normal file
55
assignment1/assignment1_reading_sample.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
//159201 assignment 1 skeleton
|
||||
//You need to add your own AddNode and PrintLL functions, as well as an AddMatrices function
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
struct Node { //declaration
|
||||
int row;
|
||||
int column;
|
||||
int value;
|
||||
Node *next;
|
||||
};
|
||||
Node *A, *B; //declaration
|
||||
|
||||
|
||||
void read_matrix(Node* &a, char *file_name){
|
||||
//reads a matrix from a file
|
||||
|
||||
int col = 0, row = 0, value = 0;
|
||||
FILE *input = NULL;
|
||||
input = fopen(file_name, "r");
|
||||
|
||||
if(input == NULL){
|
||||
printf("Cannot open file %s. Exiting.\n", file_name);
|
||||
exit(0);
|
||||
}
|
||||
//reads the matrix dimensions from the first line
|
||||
fscanf(input, "%d %d", &row, &col);
|
||||
|
||||
//read matrix
|
||||
for(int i = 0; i < row; ++i){
|
||||
for(int j = 0; j < col; ++j){
|
||||
//reads each value from this row (second line onwards)
|
||||
fscanf(input, "%d", &value);
|
||||
if(value == 0) continue;
|
||||
//
|
||||
//Include your own add_node(a, i, j, value); function here
|
||||
//
|
||||
//The next line is for debbuging, it can be commented later
|
||||
printf("Element at (%d %d) is different than zero and it is: %d ",i,j,value);
|
||||
}
|
||||
//the next line is for debbuging purposes, it can be commented out later
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
fclose(input);
|
||||
}
|
||||
|
||||
int main( int argc, char** argv ) {
|
||||
if(argc!=3) {printf("needs two matrices \n"); exit(0);}
|
||||
read_matrix(A, argv[1]);
|
||||
read_matrix(B, argv[2]);
|
||||
|
||||
}
|
||||
|
||||
66
assignment1/assignment1_reading_sample_iostream.cpp
Normal file
66
assignment1/assignment1_reading_sample_iostream.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
//159201 assignment 1 skeleton
|
||||
//You need to add your own AddNode and PrintLL functions, as well as an AddMatrices function
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
//#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Node { //declaration
|
||||
int row;
|
||||
int column;
|
||||
int value;
|
||||
Node *next;
|
||||
};
|
||||
Node *A, *B; //declaration
|
||||
|
||||
|
||||
void read_matrix(Node* &a, char *file_name){
|
||||
//reads a matrix from a file
|
||||
int col = 0, row = 0, value = 0;
|
||||
ifstream input;
|
||||
input.open(file_name);
|
||||
if(!input.good()){
|
||||
cout << "Cannot open file " << file_name << endl;
|
||||
exit(0);
|
||||
}
|
||||
int c;
|
||||
string line;
|
||||
//reads the first line to get dimensions
|
||||
if(input.good()){
|
||||
getline(input,line);
|
||||
stringstream sline(line);
|
||||
sline >> row >> col;
|
||||
cout << "Matrix dimensions " << row << " " << col << endl;
|
||||
}
|
||||
//read matrix
|
||||
for(int i = 0; i < row; ++i){
|
||||
if(input.good()) {
|
||||
getline(input,line);
|
||||
stringstream sline(line);
|
||||
for(int j = 0; j < col; ++j){
|
||||
sline >> value;
|
||||
if(value == 0) continue;
|
||||
//
|
||||
//Include your own add_node(a, i, j, value); function here
|
||||
//
|
||||
//The next line is for debbuging, it can be commented later
|
||||
cout << "Element at (" << i << " " << j << ") is different than zero and it is: "<< value <<" ";
|
||||
}
|
||||
//the next line is for debbuging purposes, it can be commented out later
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
input.close();
|
||||
}
|
||||
|
||||
int main( int argc, char** argv ) {
|
||||
if(argc!=3) {printf("needs two matrices \n"); exit(0);}
|
||||
read_matrix(A, argv[1]);
|
||||
read_matrix(B, argv[2]);
|
||||
|
||||
}
|
||||
5
assignment1/matrix1.txt
Normal file
5
assignment1/matrix1.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
4 4
|
||||
0 1 0 0
|
||||
0 0 2 0
|
||||
0 3 0 4
|
||||
0 0 0 5
|
||||
Reference in New Issue
Block a user