Is if statement over complicated? [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
This is the part that brakes, zod1 zod2 zodA are vectors. In vectors I store int. Cant tell why it crashes cause it gives no error
if(zod1[u]+zod2[y]==zodA[t] || (zod1[u]+zod2[y])%10==zodA[t])
Questions:
1 Is if statement over complicated
2 Can I use vectors like that for solving math in if statement (sorry can't explain better)
3 If 2 question is true how should I solve it
Explanations:
variable names are in my own language (sorry if it looks weird)
Values are zero for all (ex zod1[u]=0)
added whole function (variables going to the function are passed correctly and I know I use some unnecessary thing)
void calc(vector<char> zodis1, vector<char> zodis2, vector<char> zodisAts,int zo1,int zo2,int zoA)
{int i,keliamas=0;
int k =0;
vector<int> zod1(0);
vector<int> zod2(0);
vector<int> zodA(0);
for(i=0;i<zodis1.size();i++)
{
zod1.push_back(0);
}
for(i=0;i<zodis2.size();i++)
{
zod2.push_back(0);
}
for(i=0;i<zodisAts.size();i++)
{
zod2.push_back(0);
}
int u=zodis1.size()-1;
int y=zodis2.size()-1;
int t=zodisAts.size()-1;
if(zod1[u]+zod2[y]==zodA[t] || (zod1[u]+zod2[y])%10==zodA[t])
{//if((zod1[u]+zod2[y])/10==1)
{
keliamas=1;
}
if(u==0||y==0||t!=0)
{
if(keliamas==1)
{
}
}
u--;
y--;
t--;
}
else
{if(zod1[u]!=9)
zod1[u]=zod1[u]+1;
else
{ if(u!=zodis1.size()-1)
u++;
else
{
cout<<"something wrong man";
}
}
}
}

EDIT:
Error is here:
for(i=0;i<zodisAts.size();i++)
{
zod2.push_back(0); // should be zodA
}
Is if statement over complicated
No, I think it is very simple with just one 'or'. However, if zodA[t] is always less than 10, then your if condition can be written as:
if ( (zod1[u]+zod2[y])%10==zodA[t] )
Can I use vectors like that for solving math in if statement (sorry
can't explain better)
Yes you can.
If 2 question is true how should I solve it
If it compiles but then crashes, then probably you are accessing out of bounds indices. Check that your indices are less than the vector sizes.

Related

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.

What is the difference between the two codes in C++? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I was trying the problem Hash Tables: Ice Cream Parlor on Hackerrank. It is a simple question, but here is the bizzare situation i got to. How does the change of data structure matter?
Case 1:
void whatFlavors(vector<int> cost, int money) {
int ans1,ans2;
vector<int> arr(100,0); //notice this
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
And output is:
Case 2:
code:
void whatFlavors(vector<int> cost, int money) {
int arr[100]={0}; //notice this
int ans1,ans2;
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
output:
Let's just notice this part of your code:
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
This means that your expect money-cost[i] to be negative for some values of i. So you end up reading locations that are outside your array (vector or array) which will lead to undefined behavior in both cases.

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.

C++: Algorithm wrong output ( works on java ) [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 8 years ago.
Improve this question
Can someone help me with my code ? I'm trying to use different algorithm but it returns way to big numbers, when i use algorithm between /* */ it works perfect, anyone can see whats wrong with my new code ? (same on java works)
int* czynnikiPierwsze(int n)throw (string){
if(n<0){
string wyjatek1="Nie mozna rozlozyc ujemnej liczby";
throw wyjatek1;
}
int b=0;
while(n>2){
n=n/tab[n-2];
b++;
}
dzielniki=new int[b]();
int j=0;
while(n>2){
dzielniki[j]=tab[n-2];
n=n/tab[n-2];
j++;
}
/* int a=n;
int*dzielniki=new int[30]();
for(int j=0;j<n+a;j++){
while(n>2){
dzielniki[j]=tab[n-2];
n=n/tab[n-2];
break;
}
}*/
return dzielniki;
}
There's no chance your second while(n>2) loop runs even once, since the first loop only exited when that same condition was no longer true.

if statement doesn't work with function [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 9 years ago.
Improve this question
void skaitoInformacija(){
ifstream duomenys("duom.txt");
int eil_nr;
duomenys >> eil_nr;
string eil[eil_nr];
string nereikalinga_eilute;
getline(duomenys, nereikalinga_eilute);
for(int i=0; i<eil_nr; i++){
getline(duomenys, eil[i]);
if(salinamTarpus(eil[i]) == "good"){ //this if statement doesn't work
}
}
}
void salinamTarpus(string eil) {
...
}
void salinamTarpus(string eil)
your function is not returning anything that you can compare with "good" string
you need to change it to return at least some result if you want to compare it...
string salinamTarpus(string eil) {
if(eil == "okString") // string eil is the right one
{
return "good";
}
return "bad";
}
also if your function salinamTarpus(string eil) returns only 2 values("good","bad") it might be better idea to return boolean,char or so. string is a little bit too much overkill