c++ overloaded plus operator for dynamic arrays [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have class which contains a dynamic array I want to overload the plus operator. This is my code: it does not work. The class name TProgram and there is a dynamic array kt and I want to expand the orginal array with another array.
TProgram TProgram::operator+(const TProgram &Tv) const {
int K = 0;
TProgram Larger;
delete []Larger.Ct;
Larger.kt = new string[Length];
for(int Y = 0; Y < Length; Y++){
Larger.kt[K++] = kt[Y];
}
for(int X = Tv.Length; X < Length; X++){
Larger.kt[K++] = Tp.kt[X];
}
return Larger;
}

I think you have to change this 3 things:
Larger.kt = new string[Length]; to Larger.kt = new string[Length + Tv.Length]; because the array will be the sum of the two
int X = Tv.Length; to int X = 0; because you are iterating through another array, so you have to start from the beginning
X < Length to X < Tv.Length because you are iterating through the Tv's array, and not the *this's array
And then i think it should work.
Also I would suggest to create a private constructor that takes a int size and assign a new array to the array pointer inside the class with that size (better if in the initialization list) in order to avoid code like this:
TProgram Larger;
delete []Larger.Ct;
Larger.kt = new string[Length + Tv.Length];
and instead use code like this:
TProgram Larger(Length + Tv.Length);

Related

Optimising c++ arrays and vectors [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Im trying to opptimise this peice of code as its a small section of a longer code for speed rather than memory. How best would I do that. I was thinking to use set the v_vtx vector to be able to just to define the chitemp array.
double chitemp[nvert1][2];
for (int i=0;i<nvert1;i++){
chitemp[i][1]=v_vtx[i];
chitemp[i][0]=chi2->at(v_vtx[i]);
}
for (int k = 0; k < nvert1; k++){
for( int p = k+1; p < nvert1; p++){
if( chitemp[k][0] > chitemp[p][0]){
swap(chitemp[k][0], chitemp[p][0]);
swap(chitemp[k][1], chitemp[p][1]);
}
}
}
edit:
Im trying to sort chi2 (double) into order and know which v_vtx (int) links to the chi2 value
You could instead store your values as pairs (using std::array is optional, but offers a richer interface than an inbuilt array):
std::array<std::pair<double>, nvert1> chitemp;
for (size_t i = 0; i < nvert1; ++i) {
chitemp[i].second = v_vtx[i];
chitemp[i].first = chi2->at(v_vtx[i]);
}
Then, use...
std::sort(chitemp.begin(), chitemp.end());
...instead of your (inefficient) home-grown bubble-sort.

How to dynamically creat variables in a loop(c++) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to create some variables in a loop,e.g.
for(int i = 0; i < _vector.size(); i++) //_vector is a vector struct
{
auto v = _vector.at(i);
auto xi = get_name(v); //how to create x0,x1,x2,x3.....dynamically in this loop
}
anyone knows how to do it like that?
thanks very much!
If you want to reference these variables as x1, x2, etc., it would be better to create a vector to store these.
The code below is written to support integers, however, this can be replaced with another data type.
vector<int> x;
for(int i = 0; i < _vector.size(); i++) //_vector is a vector struct
{
auto v = _vector.at(i);
x.push_back(get_name(v)); //sets the value of x.at(0), x.at(1)...
}

How to Create an Array with Same Element repeated multiple times in Arduino? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
**array=[symbol,count,symbol,count.....]
for eg: array=[3,2,5,4..]
new_array=[3,3,5,5,5,5...]**
int array[]={25,6,10,2,4,3,9,5};
int value1[16]={0};
Serial.print("\n RLE decoded");
for(i=0;i<len;i++)
{
if(i%2==0)
{
value[i] = array[i];
i=i+1;
count=array[i];
}
for(j=0; j<count;j++)
{
Serial.print(value[i]);
Serial.print('\t');
}
How to Create an Array with Same Element repeated multiple times in Arduino?
This code is working properly and we are able to print repeatedly the symbols but the problem with this code is :: the repeated the values are not getting stored to a new array. we tried with declaring a new array to store the repeated values but it is not working!!
An array is a collection of the same elements, but symbol and count are obviously different things. To group different things together, struct was invented in the earliest days of C
struct {char symbol; byte count;} input[] = {
{'a', 2}, {'X' ,3} ,{'!', 1}
};
const byte inputcount = sizeof(input)/sizeof(input[0]); // 3 in this test
char expanded[20]; // will get the result
void setup() {
Serial.begin(9600);
char* resultpos = expanded;
for (auto& elem:input) {
for (byte p = 0; p < elem.count; p++) {
*resultpos++ = elem.symbol;
}
}
*resultpos = 0; // to make it a printable char array
Serial.println(expanded); // should give "aaXXX!"
}
void loop() {}
If you prefer, you can use the classic type of for loop as well. But this for each is really nice, IMO.

Warning to use encapsulated functions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have problem to sort array with objects which are private and they are encapsulated(have getter and setters).I am using my own bubble sort function.
void BubbleSort(apvector <int> &num)
{
int i, j, flag = 1;
int temp;
int numLength = num.length( );
for(i = 1; (i <= numLength) && flag; i++)
{
flag = 0;
for (j=0; j < (numLength -1); j++)
{
if (num[j+1] > num[j])
{
temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
flag = 1;
}
}
}
The problem is there that eclipse IDE sends me warning to use getters and setter in declaration of my class.
Why is better to use getters and setters?
P.S
Sorry for my bad asked question(this was one of my first questions) :)
void bubbleSort(Student* student, int size)
{ [...] }
Variable student is a pointer to an array.
You must also specific the size of the array.
To call it:
Student* myClass=new Student[5];
bubbleSort(myClass, 5); // Pass the array, and the size of the array both.
You need to create the said array before sending it as a parameter to the function. Alternatively you can create it inside the function but I think it's not what you want to go for here.
Student* students = new Student[5];
You should have written this somewhere before calling your function. Then, your function signature will have to turn to the following:
void bubbleSort(Student* student)
A logical thing to do would be to use a std::vector here though, it's much better than the method you're going for. See: http://en.cppreference.com/w/cpp/container/vector

Access elements of 2D array with absolute element number [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a 2-Dimensional array whose elements I typically want to access like this:
val = my_array[row][col];
But I also have need to access elements using their absolute index from time to time, where the row and column are not known. The "absolute" index of a given element can be computed as follows:
abs_idx = row*numCols + col;
I am thinking of achieving this as follows
mydatatype *my_array_abs = new mydatatype[numRows*numCols];
mydatatype **my_array = new mydatatype*[numRows];
for (int ii=0; ii<numRows; ii++)
{
my_array[ii] = &my_array_abs[ii*numCols];
}
Is this an appropriate way to achieve my goal, or should I expect to run into any problems or inefficiencies?
To achieve what you want you need to change your line:
mydatatype *my_array;
To:
mydatatype **my_array = new mydatatype*[numRows] ;
Note: I see only one issue with this approach that, you required continuous memory chunk.
Otherwise your approach is perfectly fine.
I'd be tempted to use std::vector to avoid all the hazards of raw arrays:
#include <vector>
#include <iostream>
typedef int mydatatype;
typedef std::vector<std::vector<mydatatype> > myvectortype;
int numCols = 10;
int numRows = 100;
mydatatype& at_absolute(myvectortype& v, int index) {
return v[index / numRows][index % numRows];
}
int main() {
myvectortype my_array(numRows, std::vector<mydatatype>(numCols, 0));
my_array[1][2] = 31;
std::cout << at_absolute(my_array, 102) << '\n';
}