Warning to use encapsulated functions [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 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

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.

memoizing a recursion solution (DP) [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 2 years ago.
Improve this question
I have written this recursion for the problem https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee
I want to know how can we memoize this solution (using a dp array).
or do we have to write recursion in a specific way to memoize it?
class Solution {
public:
int solve(vector<int>& prices,int fee,int i,int profit,int buy,int sell)
{
if(i==prices.size())
{
if(buy==sell)
return profit;
else
return 0;
}
int ans = 0;
ans = max(ans,solve(prices,fee,i+1,profit,buy,sell));
if(buy>sell)
{
ans = max(ans,solve(prices,fee,i+1,profit+prices[i]-fee,buy,sell+1));
}
else
{
ans = max(ans,solve(prices,fee,i+1,profit-prices[i],buy+1,sell));
}
return ans;
}
int maxProfit(vector<int>& prices, int fee) {
vector<int> diff;
int sum = 0;
sum = solve(prices,fee,0,0,0,0);
return sum;
}
};
You can just create an array where element i is equal to solve(i). Then, inside your function, you can pass this array through by reference to each call. You add an if/else structure in your function testing if the input you got was defined in the array, if so return arr[input] and if not, run through your normal function except just before you return, you initialize arr[input] to the value you will return.

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);

Declare int variable aux a.length = (); Or use o.length () in all loops? [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 6 years ago.
Improve this question
I wonder which is faster: Say I'm working with some text (30 characters), which would be better? And with a lot of text which would be better?
1-
int tam = text.length();
for(int i=0;i<tam;i++)
{
//something here//
}
2-
for(int i=0;i<a.length();i++)
{
//something here//
}
and also comparing these two:
1-
for (int i = 0; i < b.length(); i++)
{
aux = a.find(b[i]);
if (aux == -1)
{
sucess = 0;
break;
}
else
{
a.erase(aux,1);
}
}
2-
for (int i = 0; i < b.length(); i++)
{
if (a.find(b[i]) == -1)
{
sucess = 0;
break;
}
else
{
a.erase(a.find(b[i]),1);
}
}
Both first are the better approach.
On the first example you are checking if i<a.length() is true on every cycle. That means that you are going to execute a.length() for every iteration. If the variable a is not changed, it is unnecessary and the better approach is to calculate before and use that value.
Note that if the variable a is changed inside, placing i<a.length() might be the correct approach. It depends on your problem.
On the second example it is the same basics. You avoid useless calculations because you won't need to calculate a.find(b[i]) again inside the else.
As a general rule of thumb, as computations get bigger, more complex, and more frequent you want to minimize your unnecessary calculations. This means that storing something that needs to be calculated in a variable may speed up the process.
In both of your examples, for extremely large numbers,
int scratch = big.length();
for(int i=0;i<scratch;i++){
//body//
}
is usually faster.
In the future, general questions like this tend to belong in something like the Code Review Stack Exchange.

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