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

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';
}

Related

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)...
}

c++ overloaded plus operator for dynamic arrays [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 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);

Returning position of int [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 4 years ago.
Improve this question
The task is to return the leftmost position of given int A in larger int B (counting from 0)
A = 53, B = 1953786,
return: 2
I've found similar problem, but in that solution java function "indexOf" was used, is there a possibility to do it in other way?
The easiest way to do this would be to convert them both to std::string, and then you can use functions of std::string to find the index, similar to how you'd have indexOf for Java.
#include <iostream>
#include <string>
int main()
{
int a = 53;
int b = 513953786;
std::string number = std::to_string(b);
std::string numberToFind = std::to_string(a);
auto index = number.find(numberToFind);
std::cout << index;
}
This will return 4, since I moved the 53 to be at the 4th index to show it won't find the earlier 5 or 3 I added, per the advice of #Blastfurnace in the comments
What about using the c++ function: std::find()? You could then do something like this:
int getIndex(int a, int b){
std::string a_str = std::to_string(a);
std::string b_str = std::to_string(b);
return a_str.find(b_str);
}

Find the closest object from your position on a proper way QT c++ [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 7 years ago.
Improve this question
I'm trying to find the closest Healthpacks or enemy from a player's position. I have wrote this like:
for(auto &hp : model->getAllHealthPacks()){
if(!hp->getUsed()){
int x = hp->getXPos();
int y = hp->getYPos();
int q = (x*x)+(y*y);
if(q < smallest){
smallest = z;
hpfound = hp;
foundAHp++;
}
}
}
Now I was wondering, this is actually not proper. Are there better and profesional way's to improve my code? (Lambda,...)?
The code in general is not bad, but there is some room for improvement. First, you could make the variable hp a constant since you are not modifying its contents.
You could also create a class to store the coordinates in a single object like this
class Coordinate{
std::pair<int,int> coords;
...
};
The final code could look like this:
for(const auto &hp : model->getAllHealthPacks()){
if(!hp->getUsed()){
Coordinate coord(hp->getCoord());
int q = coord.getX()*coord.getX()+coord.getY()*coord.getY();
if(q < smallest){
smallest = z;
hpfound = hp;
foundAHp++;
}
}
}
You should also rename q to something more clear for future reference.

How do you shrink a larger array into a smaller array? [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 8 years ago.
Improve this question
I have an array, let's say 200 members, and then a smaller array, let's say 100 members. And if the larger array has 113 members, you can't fit the whole thing into the smaller array. So I want to have it so that it starts from the bottom up and tries to fit as much as it can into the smaller array. How do I do this?
int uniq() { static int current = 0; return ++current; }
int main()
{
int larger_array[200];
int smaller_array[100];
std::generate_n(larger_array, 113, uniq);
std::reverse_copy(std::begin(larger_array), std::end(larger_array), std::begin(smaller_array));
for (int i = 0; i < 100; ++i)
std::cout << smaller_array[i] << "\n";
return 0;
}
Use std::copy, something like:
std::copy(std::begin(largerArray), std::begin(largerArray) +
(std::end(smallerArray) - std::begin(smallerArray)),
std::begin(smallerArray));
If you know you want the last 100 elements of larger_array copied in reverse order this should work. The only difference from your code is using std::prev() to get the first iterator.
std::reverse_copy(std::prev(std::end(larger_array), 100), std::end(larger_array), std::begin(smaller_array));