Hi,
New to this forum, but been browsing a few threads and seems like the best place to seek help.

I have an assignment to complete.
What I need to do is read in values from a .txt file and place these values into a matrix.

I have the values stored in a file called r.txt.
R is a matrix that is 4 columns by 28 rows

I was thinking of using a nested for loop to create the matrix and then read the vlaues from the file using fstream.

Would someone be able to provide me with some sor of starting point, or give me some help with this. I have spent the last week trying to figure it and and been playing around with the code to see if I can get it to work but to no avail.

Thanks for your time,

NeedHelpWithC++

Recommended Answers

All 5 Replies

It is a good place to get help, if you follow the rules. .

I'm not asking for the answer, I'm asking for a starting point.
So far what I have is this;

#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include "matrices2.h"
using namespace std;

int main() {

	matrix R = matrix(28,4);
	matrix PE = matrix(1,28); 


// initialising values of matrix R

		ifstream stream1("directry");
		if(!stream1){ 
			cout << "File cannot be opened, please check again." << endl; 
			return 1;
		}
		//while(!stream1.eof()) {
			for (int i=0; i<3; i++) {
				for (int j=0; j<27; j++) {

I'm not sure how to get the for loop to read in the vlaues from the file and place them into the matrix R.

The matrices2 header file contains the following:

#include <iostream>
#include <vector>
// because of the bug in Visual C++ 6.0 the usual statement "using namespace std;" cannot be used here
// instead, the "namespace" std has to be manually specified for all objects in this namespace
// specifically, we use std::cout instead of cout, std::endl instead of endl and
// std::vector instead of just vector

// a class which holds matrices of an arbitrary size
class matrix {
protected:
	int sizex,sizey; // horizontal and vertical size of the matrix
	std::vector< std::vector<double> > thenumbers; // vector of vectors, which stores the actual numbers
public:
	matrix(); // default constructor
	matrix(int y_size, int x_size); // a constructor to create a matrix of specified dimensions
	matrix(const matrix& a_matrix); // copy constuctor
	inline double& operator() (int i, int j) { return thenumbers[i][j]; } // indexing operator
	inline const double& operator() (int i, int j) const { return thenumbers[i][j]; } // indexing operator
	void print(); // this function is for printing the matrix
	friend matrix transpose(const matrix& op1); // transposition
	friend bool operator== (const matrix& op1, const matrix& op2); // equality
	inline friend bool operator!= (const matrix& op1, const matrix& op2) { return (!(op1==op2)); } // inequality is "not an equality"
	friend matrix operator+ (const matrix& op1, const matrix& op2); // addition
	friend matrix operator- (const matrix& op1, const matrix& op2); // subtraction
	friend matrix operator* (const matrix& op1, const matrix& op2); // multiplication of matrices
	friend matrix operator* (const double op1, const matrix& op2); // multiplication by a number on the left
	friend matrix operator* (const matrix& op1, const double op2); // multiplication by a number on the right
	friend std::ostream& operator<< (std::ostream& stream, const matrix& op1); // printing matrices into the streams
};

// default constructor creates an empty matrix (0 by 0)
matrix::matrix() {
	sizex=0; sizey=0; // NB: in this case the vector of vectors is initialised using the default constructor
}

// a constructor to create a matrix of specified dimensions 
matrix::matrix(int y_size, int x_size)
: thenumbers(y_size,std::vector<double>(x_size,0.0)) { // calling appropriate constructors for the individual vectors
	sizex=x_size; sizey=y_size; // using the specified matrix dimensions
}

// copy constructor
matrix::matrix(const matrix& a_matrix) {
	sizex=a_matrix.sizex; sizey=a_matrix.sizey; // these two would be correctly copied by the default copy constructor...
	thenumbers=a_matrix.thenumbers; // ...but not the vector of vectors. This simple line calls the default copy
	  // constructor of the vector of vectors and, thus, it ensures that the data is copied correctly
}

// a member function for printing the matrix
void matrix::print() {
	for (int i=0; i<sizey; i++) { // rows
		for (int j=0; j<sizex; j++) { // columns
			std::cout << ' ' << thenumbers[i][j]; // space separated numbers of a single row
		}
		std::cout << std::endl; // endline to start printing the next row
	}
}

// matrix transposition
matrix transpose(const matrix& op1) { // the argument of this friend function is a matrix (X by Y) that we want to transpose
	matrix result=matrix(op1.sizex,op1.sizey); // this is new (=result) matrix (Y by X) - after creating it contains zeros
	for (int i=0; i<op1.sizey; i++) { // rows
		for (int j=0; j<op1.sizex; j++) { // columns
			result(j,i)=op1(i,j); // the data is copied from the original matrix into the new matrix...
		}
	}
	return result; // ...which is returned in the end
}

// matrix comparison
bool operator== (const matrix& op1, const matrix& op2) { // two arguments are the two matrices that we are comparing
	if ((op1.sizex!=op2.sizex)||(op1.sizey!=op2.sizey)) { // if the matrices of different dimensions, they cannot be equal
		return false;
	} else { // however, if the matrix dimensions are the same, the matrices should be compared
		for (int i=0; i<op1.sizey; i++) { // rows
			for (int j=0; j<op1.sizex; j++) { // columns
				if (op1(i,j)!=op2(i,j)) return false; // if we have two unequal elements, the matrices are not equal
			}
		}
		return true; // only if all of the matrix elements are the same, we can say that they the matrices are equal
	}
}

// the operator to add two matrices
matrix operator+ (const matrix& op1, const matrix& op2) {
	if ((op1.sizex!=op2.sizex)||(op1.sizey!=op2.sizey)) { // if the matrix dimensions do not match...
		throw 100; // ...throw an exception with a code 100
	} else {
		matrix result=matrix(op1.sizey,op1.sizex); // otherwise, create the resulting matrix
		for (int i=0; i<op1.sizey; i++) { // rows
			for (int j=0; j<op1.sizex; j++) { // columns
				result(i,j)=op1(i,j)+op2(i,j); // elements of the resulting matrix are the sums of appropriate elements of the argument matrices
			}
		}
		return result; // the sum is returned
	}
}

// the operator to subtract two matrices (for the detailed comments see the addition operator)
matrix operator- (const matrix& op1, const matrix& op2) {
	if ((op1.sizex!=op2.sizex)||(op1.sizey!=op2.sizey)) { // if the matrix dimensions do not match...
		throw 100; // ...throw an exception with a code 100
	} else {
		matrix result=matrix(op1.sizey,op1.sizex);
		for (int i=0; i<op1.sizey; i++) {
			for (int j=0; j<op1.sizex; j++) {
				result(i,j)=op1(i,j)-op2(i,j);
			}
		}
		return result;
	}
}

// operator that returns a product of two matrices
matrix operator* (const matrix& op1, const matrix& op2) {
	if (op1.sizex!=op2.sizey) { // if number of columns of the first matrix is not equal to number of rows of the second...
		throw 100; // ...then throw an exception
	} else {
		matrix result=matrix(op1.sizey,op2.sizex); // pay close attention to the dimensions of the result!
		for (int i=0; i<op1.sizey; i++) { // rows of the result
			for (int j=0; j<op2.sizex; j++) { // columns of the result
				result(i,j)=0.0;
				for (int k=0; k<op1.sizex; k++) // sum of products (i'th row elements times j'th column elements)
					result(i,j)+=op1(i,k)*op2(k,j);
			}
		}
		return result;
	}
}

// the following two operators implement multiplication of numbers and matrices
matrix operator* (const double op1, const matrix& op2) {
	matrix result=matrix(op2.sizey,op2.sizex); // it can be done for matrices with arbitrary dimensions
	for (int i=0; i<op2.sizey; i++) { // rows
		for (int j=0; j<op2.sizex; j++) { // columns
			result(i,j)=op1*op2(i,j); // the result is the element of the original matrix times the number
		}
	}
	return result;
}

// the same as above, but for matrix multiplied by a number
matrix operator* (const matrix& op1, const double op2) {
	matrix result=matrix(op1.sizey,op1.sizex);
	for (int i=0; i<op1.sizey; i++) {
		for (int j=0; j<op1.sizex; j++) {
			result(i,j)=op1(i,j)*op2;
		}
	}
	return result;
}

// this operator implements printing the matrix into the stream (for details see Lecture 12)
std::ostream& operator<<(std::ostream& stream, const matrix& mA) {
	for (int i=0; i<mA.sizey; i++) {
		for (int j=0; j<mA.sizex; j++) {
			stream << mA(i,j) << ' ';
		}
		stream << std::endl;
	}
	return stream;
}

any ideas?

Good start, although don't indent so far. 4 spaces is more than enough. 32 makes your code unreadable. See this.

for (int i=0; i<3; i++) {
		for (int j=0; j<27; j++) {

If your matrix is defined as 28x4, why are you reading 3x27? Use the dimensions you gave the matrix.

Now add the read command. You're good to go.

Why define a 'matrix' as 1x28? Isn't that an array[28]? You don't need a 2D array.

thanks, i managed to figure it out. now i need to transpose the matrices and program the gaussian elimination. oh what fun!

what is the data store in the file
if it is a mix between number and char u maybe use array of structure .
struct data {
int num;
char alpha;
} s[100];
all that befor the main.
then
f>>s.alpha;
f>>s.num;
if the file like a student mark with there names.
note:u can use pointer or array of char.
good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.