C++ :: 2 Dimensional Vector Represented As A Class
Nov 5, 2014
I have to create a class to represent a 2 dimensional vector. I need to include certain member functions such as a function to find magnitude of the vector, and one to find the dot product of that vector with another vector, and several others too. That's all fine. A stipulation of the problem is that I must include a constructor which can take cartesian form of the vector and a constructor which can take polar form of vector. Since this involves overloading the constructor the best solution I have come up with is to create the object with either doubles or floats so that the compiler can choose the correct constructor. This seems like a really bad idea. Is there a way I can get the compiler to choose the correct constructor without doing it using the precision? Here is a sample of my header file, there are many more member functions
class Vector{
public:
Vector(double x, double y, double v, double w){
myArray[0]=x;
myArray[1]=y;
additionalArray[0]=v;
additionalArray[1]=w;
[Code] .....
In my .cpp file the object is created with either four doubles or four floats depending on which constructor I want to implement. There must be a better way. Additional Array is created for use in member function which require calculations with a second 2d vector.
View 1 Replies
ADVERTISEMENT
Apr 25, 2014
How do I iterate through a two dimensional vector? To iterate through and print the elements of a single dimensional vector I use:
vector<int> a;
for(vector<int>::iterator it=a.begin();it<a.end();it++)
cout<<*it;
How do I do the same for a two dimensional Vector?
View 2 Replies
View Related
Oct 30, 2012
Suppose I have a two-dimensional stl vector (a vector of vectors), for example:
Code : vector<vector<int> > x;
And then I want to sort the outer vectors in order of the size of their inner vectors. How do I do this?
Usually, with a one-dimensional vector, I can just create a comparison function and use the sort function. For example, if my vector is defined as:
Code:
vector<int> y;
And I want to sort if in terms of the int values of each vector element, then I can sort by using the stl::sort function, with the comparison function defined as:
Code:
static bool SortFunction(int val1, int val2) {
if (val1 > val2) {
return true;
} else {
return false;
}
};
However, with my two-dimensional vector, I use the following comparison function:
Code:
static bool SortFunction(vector<int> vec1, vector<int> vec2) {
if (vector1.size() > vector2.size()) {
return true;
} else {
return false;
}
};
I get a runtime error:
"Debug Assertion Failed!
File: c:Program Files (x86)Microsoft Visual Studio 10.0VCincludealgorithm
Line 3686
Expression: invalid operator<"
Why is this?
View 2 Replies
View Related
Nov 8, 2014
#include <iostream>
#include <cassert>
#include <vector>
using namespace std;
vector<int> flatten(int a[100][200]) {
vector<int> v;
for (int i = 0; i < 100; ++i) {
[Code]...
I made a function for two dimensional arrays with vector, but I don't know how to make the test code for it.
View 4 Replies
View Related
Mar 24, 2012
I am using 2 ARRAYS OF DIFFERENT SIZES in One 2-Dimensional Vector, and my output is not correct. The arrays are size 4 and size 13.
I want COLUMN 0 to have: 55, 66, 77, 88.
I want COLUMNs 1-12 to have 1,2,3,4,5,6,7,8,9,10,10,10,11 in EACH ROW. It would seem that the 2nd loop for the size 13 array would need to loop 4 times in order to fill 4 rows, however, I'm not sure how to do that. Here is what I have so far in code and output:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int typeArray[4] = {55,66,77,88};
int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};
[Code] ....
OUTPUT:
55 0 0 0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10 10 10 11
77 0 0 0 0 0 0 0 0 0 0 0 0
88 0 0 0 0 0 0 0 0 0 0 0 0
How to populate rows correctly?
View 2 Replies
View Related
Mar 20, 2012
Two Hard-coded Arrays into One 2-Dimensional Vector I have 2 arrays, each of them are hard-coded with integer values. I also have one 2-Dimensional vector and I want to put 1 array into the first column of the vector and the other array into the 2nd column of the vector. The reason is that I want to do math on the 2nd column of the vector only.
I am able to accomplish this with 3 arrays. Two of them are 1-Dimensional and the third array is 2-Dimensional.
I know how to pass ONE Array into ONE vector:
Code:
vector<int> myVector(typeArray, typeArray + 4);
however, when I declare a 2-Dimensional vector:
Code:
vector< vector <int> > myVector(3, vector<int> (2,0))
I am not seeing how to add TWO arrays or how to OUTPUT it to the screen.
Here is my code using DevCPP:
#include <iostream>
#include <vector>
#include <Windows.h>
[Code]...
I don't get any errors, however, I don't know how to output it to the screen to see what it looks like.
View 5 Replies
View Related
May 19, 2013
I need to make a small program with a function with this prototype: void f(char *a,char *b) that adds two numbers represented as strings without using conversion operators or other tricks.
View 8 Replies
View Related
Nov 29, 2013
If I have a number 117, represented in binary as : 01110101 and I wanted to grab the top nibble. What would be the decimal value I would be extracting?
Would it be 0111 or 0101 decimal values 112 or 5 or is my understanding completely wrong?
View 11 Replies
View Related
Mar 27, 2014
The following function finds the normal to a terrain represented by a texture. I found it somewhere online , it works but i couldn't understand the math behind it. So , How (or Why ?) does it works ?
//psuedo code
Vector2 normal(x,y) {
Vector2 avg;
for(int w = -3; w <= 3; w++) {
for(int h = -3; h <= 3; h++)
[Code] ....
View 6 Replies
View Related
May 13, 2013
I need to create a class vector as a template and define operations on vectors.
And this is what I made.
#include<iostream>
using namespace std;
template<class T>
[Code].....
View 2 Replies
View Related
Jul 31, 2013
How can I concatenate two 2-dimensional int arrays into one larger 3-dimensional array. This question is also valid for the 3-dimensional vectors. I know the command for the one dimensional vector as:
std::vector<int> results;
results.reserve(arr1.size() + arr2.size());
results.insert(results.end(), arr1.begin(), arr1.end());
results.insert(results.end(), arr2.begin(), arr2.end());
and for the one dimensional array as:
int * result = new int[size1 + size2];
copy(arr1, arr1 + size1, result);
copy(arr2, arr2 + size2, result + size1);
But I do not know how to make a 3-dimensional array or vector.
View 3 Replies
View Related
Dec 14, 2014
im passing a vector of a class to a function of another class. But i cant access the data on the classes inside the vector.
Something like that:
class CDummy{
...
public:
string m_name;
[Code].....
Im creating the vector on main() and using push_back with a pointer to an initialized CDummy instance
View 5 Replies
View Related
Jan 17, 2014
I had a hard question in my C++ final exam and I'm trying to solve it for the last 3 days. I haven't succeded yet! Here is the question: You have a one-dimensional array A[20]={1,2,3,4,...,20} and B[5][4] you have to assign the A array's elements to the B array but there is an order which is: B[5][4] = { { 12, 9, 11, 10 }, { 14, 7, 13, 8 }, { 16, 5, 15, 6 }, { 18, 3, 17, 4 }, { 20, 1, 19, 2 } } and there is a restriction: you can only use ONE for statement, nothing else!
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int A[20] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20 }; // define A array's elements.
int B[5][4] = { 0 }, k = 1; // define B array and k counter.
[code]....
I can't narrow the statements to one,This program works perfectly but it shouldn't be that long, we need ONLY ONE FOR statement, not two!
View 2 Replies
View Related
Nov 15, 2014
I was trying to implement own vector class and wrote some code. Below is the code.
Code: #include<iostream>
#include<vector>
using namespace std;
template <typename T> class MyVector
{
T *mem;
int m_size,final_size;
public:
MyVector() : final_size(1),m_size(4)
{
[code].....
I have question on this function.
Code: myVecPush_back(T t){}
Here I was able to put elements upto any number of time, but I have allocated memory to only 4 elements in T *mem.
My question, why the program is not crashing when I tried to put elements more that 4?
Is since the variable is of type Template? Any specific reason for this?
View 2 Replies
View Related
Oct 9, 2014
Let's say we have a custom Vector class and I need to know which of the following is considered to be more efficient and why of course.
Vector Vector::operator+(const Vector &b) const {
return Vector(x+b.x,y+b.y);
}
Vector Vector::operator+(const Vector &b) const {
Vector tmp(x+b.x,y+b.y);
return tmp;
}
View 1 Replies
View Related
Aug 9, 2013
First this is my code:
#include <iostream>
#include <vector>
#include <cstdlib>
[Code].....
the blacked content got problems: the error messages are the throat_t::P or throat_t::T is inaccessible.
View 3 Replies
View Related
Aug 19, 2013
I have a settings class and a settingItem class. The settings class has a vector of settingItems. The vector is not working:
error C2065: 'settingItem' : undeclared identifier
error C2923: 'std::vector' : 'settingItem' is not a valid template type argument for parameter '_Ty'
with code line:
vector<settingItem> settings;
View 8 Replies
View Related
Feb 12, 2014
Okay so I have a class Student, which takes a number and a vector as a parameter for the constructor. Everything works well, until I output the values of the vector for every instance. The problem is that the same vector is being shared with EVERY instance I create, but I want it to be unique for every single one!
//Student.h
#ifndef __Grade_calculator__Student__
#define __Grade_calculator__Student__
#include <iostream>
[Code].....
View 1 Replies
View Related
Mar 16, 2013
So I'm trying to store class objects in a vector:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
[Code]....
1. Am I storing it correctly?
2. How would I access the stored data, say, if I wanted to compare it to other stored data or COUT it?
View 1 Replies
View Related
Jun 15, 2012
What I want to do with the below code is to construct the vector containing 'Ability' objects in the class 'Card'. I have searched for the solution in the past, and have been unsuccessful, mainly because the vector contains child classes of the parent class 'Ability'. The below code is a snippet of the larger program that I am working on, and should compile:
main:
Code:
#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#ifndef _abilities_h_
[Code] ....
As you can see, in the class 'Card' I have a pretty large constructor. Up to this point, however, I have failed in my attempts to construct the abilities vector, because it contains those child classes.
View 7 Replies
View Related
Jul 7, 2014
Input file:
Course Of Sales.csv
Time,Price ($),Volume,Value ($),Condition
10/10/2013 04:57:27 PM,5.81,5000,29050.00,LT XT
10/10/2013 04:48:05 PM,5.81,62728,364449.68,SX XT
10/10/2013 04:10:33 PM,.00,0,.00,
How to read the csv file with my own vector class. I already have date.h,date.cpp,time.h,time.cpp
How to do the maintest and how to create the vector class
I need to show Date, highest price and Start time(s) of the highest share price
View 3 Replies
View Related
Mar 22, 2013
For a beginners C++ lab, I have a base class Employee and two derived classes HourlyEmployee and SalaryEmployee. In main, I have a vector defined as vector <Employee *> VEmp; It's then passed to a function to get the input, which works fine. But what I'm struggling with is another function with the header "printList(const vector <Employee *> & Ve)". It's supposed to loop through the vector and call the appropriate printPay function, which is a seperate print function inside each derived class. How do I loop through the vector and print it out? I was trying to do a for loop and something like "Ve[i].printPay();", but that doesn't work. So how would I do it?
Here's some snippets of the relevant code.
class Employee {
....
virtual void printPay() = 0;
};
class HourlyEmployee : public Employee {
[Code] ....
View 4 Replies
View Related
May 13, 2014
I need to create a vector of pointers and hold the book objects in it. then i have a virtual function in my books which is a pure virtual in LibraryItems. When i try to add the books object in my code, i understand that since the scope runs out there is no object that is added. so when i run print it gives me an error.
#include<iostream>
#include "books.h"
#include "library.h"
#include <vector>
using namespace std;
int main(int argc, char * argv[]) {
vector<LibraryItems* >libraryInfo;
[Code] .....
View 4 Replies
View Related
Feb 16, 2013
I get a problem with the vector as a private class member: When I did't initialize the vector in constructor(which means the size of the vector would be 0), I used a class function to add two elements to the vector and it worked (because I added a "printf" to output the size of the vector and the elements within that function). However, when I used another class function to visit that vector, no element was in and the size became 0.
Then I tried to add two elements to the vector during the construction, and it turned out that these two elements could be stored in the vector while other elements added through class functions could not.
I guess there may be some problems on the scope of the function. But I feel the class member should not be effected by the scope of the class function.
View 7 Replies
View Related
Jul 24, 2013
How I can use class functions in a vector. I have 2 classes:
Gamemode class
bullets class
Inside the Gamemode class I declared: vector<bullets> Bullet and bullets * b.
If the user presses the shoot button a new bullets class will be added to the Bullet vector:
b = new bullets;
Bullet.push_back(b)
Bow I'd like to check all objects in the Bullet vector for the collision() function inside the bullets class.
I already set up a for loop which counts from 0 to the end of the vector:
for( int i=0;i<Bullet.size;i++)
{
}
My idea was to do something like:
if(Bullet[i].collision()) then erase Bullet[i]
But that doesn't work...
View 2 Replies
View Related
Dec 9, 2013
I'm having the same problem : [URL] .....
Though, my vector isn't one of int, it is a vector of objects from another class and NetBeans won't compile it.
#include <cstdlib>
#include<vector>
#include <string>
#include<iostream>
using namespace std;
class estoque{
[Code] .....
View 3 Replies
View Related