C++: Algorithm wrong output ( works on java ) [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 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.

Related

is cin is logic 1 or 0? [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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x,y;
while(cin)
cin>>x>>y;
cout<<"YES";
}
here for which input it will print out "YES"?
is cout is logic 1 or 0?
This will print "YES" for any input (except for an infinite stream of valid input, in which case it'll run forever and never print anything).
You forgot braces for your loop, so the cout statement is not actually in the loop.
Your cout is in no way conditional on the values you read in.

What's the difference between the given two codes. One gives time limit exceeded when run on ideone and the other works fine [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 5 years ago.
Improve this question
1st code: works fine gives success with time of 0sec
int main()
{
int n=100000;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{}
cout<<"ffdfdf";
}
2nd code: gives a time limit exceeded
int main()
{
int n=100000;
bool **a=new bool*[n];
for(int i=0;i<n;i++)
{
bool[i]=new bool[n];
for(int j=0;j<n;j++)
{
bool[i][j]=1;
}
}
cout<<"ffdfdf";
}
can anyone explain why the two code fragments have a vast time difference.I am not understanding it.
bool[i]=new bool[n]; is extremely expensive cf. the other statements.
A good compiler will optimise out your first program to cout << "ffdfdf";, since it will know that the loop doesn't do anything.
Once you've replaced your errant bools with as so the second snippet actually compiles, you'd be advised to pair your new[] calls with delete[].

Is if statement over complicated? [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
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.

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

Threshoding image between certain range [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
How to Threshold image between certain range?
i have done this but it doesn't work.
for (int i=0;i<s.size().height;i++)
{
for(int j=0;j<s.size().width;j++)
{
int k=int (s.at<uchar>(j,i));
if (k>6 && k<10)
k=255;
else k=0;
s.at<uchar>(j,i)=k;
}
}
You get an uchar value, and convert it to integer. Try this :
uchar k= s.at<uchar>(j,i);
if (k>6 && k<10) {
k=255;
}else {
k=0;
}
s.at<uchar>(j,i)=k;
I think it may work.