C++ :: Matrix Class Using Vectors

Jul 23, 2012

I have implemented matrix class using vectors. code is

template <class T>
class CBSMatrix : public CBSVector< CBSVector<T> >
CBSMatrix(long r,long c, T t) {
setsize(r, c);
init(t);

[Code] .....

Although i have implemented the cols() but things in it are confusing me specially this line "if(size()) return at(0).size();" and "CBSVector<T> v(cols());" this line in add row function is also troubling me. in main i have done some thing lyk CBSMatrix <int> mat(5,5,0); now how to put values in this matrix. How to put values in this template based and vector based wired matrix because first row is created in this and then cols are added to that row.

View 14 Replies


ADVERTISEMENT

C/C++ :: Matrix Multiplication (Vectors)

Oct 31, 2014

I am writing a brute force implementation of matrix multiplication:

Header file:

vector<vector<int> > matrix_multiplication(vector <vector<int> >& a, vector <vector<int> >& B)/>/>/>;

Source file:

vector<vector<int> > matrix_multiplication(vector <vector<int> >& a, vector <vector<int> >& B)/>/>/>{
int n = a.size();
vector< vector <int> > c (n , vector<int> (n));
for (int i =0 ; i<n ; i++){
for (int j =0 ; j < n ; j++){
for (int k ; k < n ; k++){
c[i][j] = a[i][k] + b[k][j];

[Code] ....

I keep on getting occasional seg faults />/>. I can't see why.

View 10 Replies View Related

C++ :: Using OOP To Implement Matrix Class That Provide Basic Matrix Operations

Mar 27, 2013

i want to know how i can solve this question? do i need to create a class or write the program codes.

View 12 Replies View Related

C++ :: Derived Class Not Overwriting Base Class Function - Using Vectors

Feb 4, 2014

So I have a base class, lets call it base. In base I have a virtual function called update(), update just couts "base" then I have a class derived from base called derived;

it has a function called update(), update just couts "derived" then I create a vector called Vec it's initialised like this:

std::vector<base> Vec;

then I add an element into it like this

Derived DerElement;
Vec.push_back(DerElement);

then when I type:

for (int i=0; i<Vec.size(); i++) {
Vec.at(i).Update();
}

It outputs:

Derived DerElement2;
DerElement2.Update();

and it outputs this:

#include <iostream>
#include <vector>
class Base {
public:
virtual void Update() {

[Code] .....

and this is it's output:

Base
Derived
Press any key to continue . . .

View 1 Replies View Related

C++ :: Deriving A Class From Vectors

Jul 13, 2012

i want to derive a class named population which is derived from vectors of solution and solution class is derived from vectors of int. That means that population is collection of solutions and is created with vector by deriving it from template based vectors<solution>. my question is that I am a bit confused in this hierarchy and not getting idea of constructors and destructors and calling mechanism of methods. I do have concepts of inheritance and constructors in inheritance but confused in this perspective.

View 10 Replies View Related

C++ :: Pointer Class Object Vectors

Aug 4, 2013

#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
struct bop {
string realname; //real name

[Code] ....

Okay, so first thing's first. The program will not compile due to lines 39-45. If I were to change those pointers into regular objects, it will not change the values of my class object. So what is the right way to do this?

I want the user to be able to input the # of employers/programmers into the system. But I cannot do that with an array of classes because when declaring an array; the array size must be constant.

View 6 Replies View Related

C++ :: Add Three Objects Of A Class Matrix

Apr 9, 2014

I have been working on an assignment where I have to add three objects of a class Matrix. The class should have the flexibility to add more than two oprands without changing any operand on Left hand side of the '=' operator.

Matrix obj1, obj2, obj3, obj4
Obj1=obj2+obj3+obj4

I am using the following code for this

Matrix Matrix::operator + (Matrix &obj) {
if((this->Columns == obj.Columns) && (this->Rows == obj.Rows)) {
for(int i=0;i<obj.Rows;i++)

[Code] .....

the problem is that it is just adding only 2 operands. How can I add more?

View 2 Replies View Related

C++ :: List Of Vectors (vector Of N Vectors Of M Elements)

Mar 19, 2014

I create a list of vectors (a vector of n vectors of m elements).

std::vector <std::vector <int> > vsFA (n, std::vector<int>(n));

How I assign values? I try below, but not worked

void armazenaFA( std::vector <int> &vFA) // this function only knows about vFA
{ vsFA[n] [m]= simTime().dbl();
OR
vsFA[n].push_back(simTime().dbl());
}

View 1 Replies View Related

C++ :: Adding Arrays And Matrix To A Class?

Feb 5, 2014

I'd like to start out by adding an array to a C++ class. I'd like to be able to reference the array using a class object that I create, for example:

Class is Stone.

Stone Bob is an instance of "stone" that I name "Bob".

"Bob.array[1] = "granite";" tells the compiler that the second element in the array (with the first being the zeroth element) is a string containing "granite".

I'll eventually want to extend this to an n x m matrix within the "stone" class that can be referenced as: Bob.matrix[1][3]="lignite";

I tried to make this work using a text again and again last night to no avail. My code is below.

NOTE: Since I am dynamically allocating memory space, I'd like to avoid memory leaks when using this class with dynamically allocated arrays and matrices. Not sure how to do this. Also need some insight into "destructor", and why my simple version reduced to a comment below doesn't seem to please the compiler.

CODE FOLLOWS:

Code:
// AINOW.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using std:: string;
using std:: cout;
using std:: endl;
using std:: cin;

[code]....

View 11 Replies View Related

C++ :: Reading From Boolean Matrix Class

Feb 18, 2015

Question for coding transivity in c++

a->b b->c then a->c

It should contain a main code reading from boolean matrix class. for ex)input file is

8
0 1
1 2
2 3
3 4
4 5
5 6
6 7

8 is the number of no.

what program should do.
1. check if 0->7 first(0th element)->last
2. check every 5th(5,10,15...) element is to last 5->7

View 1 Replies View Related

C/C++ :: Matrix Class - Addition / Subtraction

Jul 7, 2014

I'm working on a matrix class and I've hit a few snags. Here's what I've got so far:

Matrix.h

#include <iostream>
#include <conio.h>
using namespace std;
class Matrix {
private:
int matrix[50][50];

[Code] ....

Where I have questions is in implementing the addition and subtraction bits into the class itself. I understand that I'm going to have to do a copy operation (this from reading in C++ Primer Plus 5th Edition). Right now I'm just after adding/subtracting as the rest will be variations on the same theme.

So, here it goes:

As I understand the problem, I need to pass my two matrices as arguments to an addition function. This is going to involve having to copy the values of the two existing matrices into temporary matrices within the addition function, then I'll add them, and store them in a new matrix which will be my return value. So...something like this:

int Matrix::matrixAdd(int R, int C, const Matrix & matrix1, const Matrix & matrix2) {
int sum;
Matrix matrix;
for (int i = 0; i < R; i++)

[Code] ....

I do end up with errors there...C2240, and C2662. Again, I'm new to working through this, but that's what I've got. My idea is that I'm passing the maximum size of the array as defined by the user, in this case a 2x2 array, it'll cycle through and add up to that imposed limit...I went 2x2 because it's small enough that testing doesn't drive me up a wall.

View 14 Replies View Related

C++ :: Adding Two Objects Of Class (Matrix)

Feb 13, 2013

I am trying to add matrices a and b. I am getting an error in the "add" function, probably because I have m[i] in it, and m is not an array. What is the correct way of writing the "add" member function here?

Also, although the "read" and "write" member functions of the class are working just fine, do you think there is a better way of writing them?

Code:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
const int rows=3;
const int columns=3;

[code].....

View 2 Replies View Related

C/C++ :: Resizing Matrix And Zero-parameter Class Member

Sep 19, 2014

I'm attempting to make a matrix class, and so far I've made a member that can make a vector of vectors, but I'm not sure how to go about resizing the vectors if need be.

EX: resizeArray(3,3) should return a 3x3 vector.

Also, for some reason when I call my default member, matrix(), I get an error that says.. "Request for member numrows in myMatrix, which is of type matrix<int>[][]"

#include <iostream>
#include <vector>
using namespace std;

[Code].....

View 1 Replies View Related

C++ :: Matrix Class (strings But No String Header Allowed)

Sep 27, 2013

I am IT student and had a C++/C (oral + paper) exam today. One of the tasks was to write a 2D-Matrix (as the question said) class with following restrictions:

- No <string> header is allowed
- Only Dtor needs to be implemented
- No templates
- Following should be possible:

Code:
std::cout << mat1 + mat2 + "some randome string";
mat1 += mat2; So i did the following:
In Matrix.h i wrote: Code: Class Matrix{
int rows, cols;
char *arr[][];

[Code] .....

Now..this destructor made me loose some points since the Prof. said that it is not correct. The corrected version was:

Code:
Matrix::~Matrix(){
if(arr){
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
delete [] arr[i][j];
[Code] ....

Now, i agree on that error i made, but it is only in case we use the "new" keyword to reserve place dynamically for each string(for each char*). So this raised the question in my head about:

Since the following is allowed in C++

Code:
char* str1 = "hello";
char* str2 = "you";
arr[1][3] = str1;//arr[1][3] was initialized to "_" without new keyword
arr[6][0] = str2;//arr[6][0] was initialized to "_" without new keyword why would someone use the new keyword..

I mean like this:

Code:
arr[1][3] = new char*[sizeof("sometext1")+1];
arr[1][3] = "sometext1";
arr[6][0] = new char*[sizeof("sometext2")+1];
arr[6][0] = "sometextw";

What is happening internally in C++ in both the cases(with and without new keyword)?

View 11 Replies View Related

C++ :: Matrix Class - Template Constructor Doesn't Work So Well

Feb 5, 2013

I realized a Matrix class to practice and I have a problem I can not solve! Here my problematic code:

Mtrx.h:

Code:
template <class T>
Mtrx::Mtrx(dim m, dim n, const bool random_constructed = false, const T min = static_cast<T>(0), const T max = static_cast<T> (10))
Mtrx.C

[Code] ...

And here the relative main section:

Code:
Mtrx rand1 ( 5, 5, bool);// ok
cout<<rand1<<endl;

Mtrx rand2 ( 7, 3, bool, -5, 20);// ok
cout<<rand2<<endl;

Mtrx rand3 ( 7, 7, bool, 0., 15.);// compilation error: undefined reference to
// "Mtrx::Mtrx<double>(unsigned long, unsigned, bool, double, double)"
// collect2: error: ld returned 1 exit status

I compiled in a Linux OS with g++ -std=c++11

View 3 Replies View Related

C++ :: Create Two Vectors And Then Loop Through The Vectors

Sep 19, 2014

This is probably a very basic question, but I need to create two vectors and then loop through the vectors and output each pair that is found.

The user will input min1 and max1 with step1 for the first vector and min2 and max2 and step2 for the second vector. Then the loops will go through and return the combinations will return each pair of the two vectors.

So if I input min1=1 and max1=10 and step1=1 and same for vector two the return would be:

[1,1]
[1,2]
.
.
.
[10,10]

This is for part of a homework assignment, but I can't continue on the assignment without first getting this simple part to work.

View 1 Replies View Related

C++ :: Template Class For Matrix Manipulation - Expected Initializer Before Constant

Jan 29, 2013

I wrote a template class for Matrix manipulation. The compiler cannot compile the source and complains

Matrix.h:144:41: error: expected initializer before ‘const’

What is the problem of the code enclosed below?

#ifndef MATRIX_H
#define MATRIX_H
/**
* @file Matrix.h
*The Matrix template class written for simplify the matrix manipulation.
*/

#include <cassert>
using namespace std;
template<typename T>
class Matrix {
int rows,cols;

[Code] .....

View 6 Replies View Related

C++ :: Accept Matrix As Argument And Display Its Multiplication Matrix

Jun 1, 2014

I just want to know the code of the program: Write code to accept matrix as aurgument and display its multiplication matrix which return its multiplication matrix.

View 1 Replies View Related

C/C++ :: Assign A Matrix To Submatrix Of A Bigger Matrix?

Feb 27, 2012

I want to assign a matrix to submatrix of a bigger matrix.

ublas::matrix<int> A(8,5);
ublas::matrix<int> B(2,5);
for(size_t i=0;i<A.size1();++i)
for(size_t j=0;j<A.size2();++j)
        A(i,j)=i*A.size2()+j+1;
for(size_t i=0;i<B.size1();++i)
    for(size_t j=0;j<B.size2();++j)
        B(i,j)=i*B.size2()+j+5;
ublas::matrix_range<ublas::matrix<int> > a(A,ublas::range(0,2),ublas::range(0,5));
a=B;  

and it works.

but if the matrix is compressed_matrix type, there's something with it. the error log as below:

Check failed in file boost_1_48_0/boost/numeric/ublas/detail/matrix_assign.hpp at line 1078:
detail::expression_type_check (m, cm)
terminate called after throwing an instance of 'boost::numeric::ublas::external_logic'
what(): external logic
Aborted

I'm not sure this is a bug or not.

View 2 Replies View Related

C++ :: Find Matrix P For A Square Matrix A

May 25, 2014

I have to prepare a project. But I don t know how can I do this. Program will find a matrix P for a square matrix A such that P^-1 A P ....

View 15 Replies View Related

C++ :: How To Convert Compact Form Of Matrix Into Matrix Form

Oct 4, 2014

3 3
2 1 3 2 3
1 0 2
1 2 6

First line shows row and column number

First index of the second line tells the number of non-zero entries of the first row and second index tell the column number where the non zero entry is placed

for 1st row of matrix:

non-zero entries=2
column number=1
non-zero entry=3
column number=2
non-zero entry=2

covert this in the given form
0 3 3
2 0 0
0 0 6

View 1 Replies View Related

C/C++ :: Any Way To Have 2 Vectors Mix Together?

Mar 27, 2013

I want to add 2 vectors to print out so that there on the same line. What I am trying to make is an inventory system that will use 2 vectors to keep the pounds of the item and list the 2 vectors on one line.

(I am using Microsoft Visual C++ 2010 Express)

Like this:

0. empty 0
1. empty 0
2. empty 0

etc...

Right now it looks like this:

0. empty
0. 0

The code:

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cmath>  
using namespace std;  
int main() {
    vector<string> inv;

[Code] ....

View 14 Replies View Related

C/C++ :: How To Add Strings To Vectors

Apr 19, 2012

I have a vector I want to add book titles to, then i want to print my updated vector. This is best I have come up with but the program fails at the getline line. why?

string book;
cout << "Enter book to add: "<< endl;
getline(cin, book);
books.push_back(book);
for(int i = 0; i < books.size(); ++i) {
cout << i+1 << ". " << books[i] << endl;
}

View 1 Replies View Related

C++ :: Calculate The Angle Between Two Vectors

Feb 20, 2014

My question is not in c++ programing , but because my aim is to make code that calculate the three angles between two vector i ask my question here

So as the title i have three point that create two vector and I want to get the angles in x,y and z of the point2

I used crosproduct that give me one angle without axe , I don't know in which axe this angle

My aim is the three angles for the 3 axes ....

View 7 Replies View Related

C++ :: Manhattan Distance Between Two Vectors

Mar 25, 2014

I can't figure out the error in this code; it compiles but returns rubbish.

serge
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>

[Code] ......

View 7 Replies View Related

C++ :: Quaternion For Rotating Vectors

Dec 28, 2013

I try to use quaternion to rotate one(and later more!) 3d-vectors in a general manner.

i read in wikipedia, that the general calculation goes with:

vector_rot = quaternion * vector * quaternion_compl.conj.

I used the class of irrlicht [URL] .... to do the calculations.

there is no premade function in this class to rotate vectors through this quaternions, so i just tried to it this way:

irr::core::vector3df v1(1,0,0);
irr::core::quaternion a1(2,3,4,6);
a1.normalize();
irr::core::vector3df result2(a1.X*v1.X*(-a1.X),a1.Y*v1.Y*(- a1.Y),a1.Z*v1.Z*(-a1.Z));

result2 should then be the rotated vector, but it does not work. i dont know how to explicitely write down the rotation specification in this topic.

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved