What is wrong with my little programm?
Asked by
Rickover (
110)
January 7th, 2011
This program made in MinGW should decompose numbers in prime factors. MinGW doesn’t say that there is on error, but the program does nothing:
#include<iostream.h>
#include<math.h>
int main(void)
{
int i,j,a,b=1;
cout<<“Introduceti numarul”<<endl;
cin>>a;
struct descomp
{int baza,exp;};
descomp x[20];
for(i=1;i<=a;i++)
{
j=0;
while(a%i==0)
{
a=a/i;
j+=1;
}
if(j>=1)
{
x[b].baza=i;
x[b].exp=j;
b+=1;
}
}
for(i=1;i<=b;i++)
cout<<x[i].baza<<”^”<<x[i].exp<<endl;
return 0;
}
What is wrong with it?
Observing members:
0
Composing members:
0
8 Answers
If you post the code to Gist.Github.com or PasteBin and paste the link in the question, the code will be easier to read and will not get in the way of the rest of the description.
I know nothing about this language at all, but at a first glance, based on what I know from other languages, I would guess that you have misused the curly brackets ”{ }”.
The part I’m talking about is here:
x[b].baza=i;
x[b].exp=j;
b+=1;
}
}
does that last curly bracket not state that it is the end of the program?
I think it should maybe be like this instead:
}
for(i=1;i<=b;i++) {
cout<<x[i].baza<<”^”<<x[i].exp<<endl;
return 0;
}
}
Just a stab in the dark, hope it helps.
Does the program print out the initial message? Maybe there is something wrong with the cin and cout implementation. Try using read and print statements. The program should work.
You appear to be attempting to pass “void” as a parameter to main(). void is a reserved word in C which refers to a data type, not a value, so it might cause a problem in compilation. You should be using the -Wall switch with gcc when you compile.
It would be helpful if you post the output of the program when you try to run it, and also the output at compile time. The output of ‘gdb [your-program]’ might also be useful.
Wow. I haven’t coded in C++ in years, but I thought I’d compile it and take a look. I formatted it a bit: http://pastie.org/1439130 (you don’t need to include math, you should probably do it as c++, etc.)
First of all your program doesn’t do “nothing”. I ran it and it never ended; it was stuck in an infinite loop. The for loops are set to end, which made me suspicious of your while loop.
Let’s consider the use case of me entering “4”. First time through the while loop a is 4 and i is 1. Since a%i==0 is true, the while loop continues. The variable a is set to a/i or a=4/1, so the value of a doesn’t change. The while checks 4%1==0 which is still true, a is set to 4 again, 4%1==0 is still true, a is set to 4 again, 4%1==0 is true, a is set to 4 again…
Hopefully that puts you on the right track :)
@koanhead int main(void) is perfectly fine in C, but int main() is preferred for C++ (iirc).
check it out: http://www.google.com/search?q="int+main(void)"
@phaedryx , You are right. I missed that. If the loop goes from i=2 to a, that should fix it.
Answer this question
This question is in the General Section. Responses must be helpful and on-topic.