Click to See Complete Forum and Search --> : Wierd output
KRUNCH
March 4th, 2008, 03:30 PM
Hi , i got a task , that was for the olympiad of my country in informatics and i wanted to solve a few tasks , and i started with one , it was fairly easy to figure it out , and i easily coded it , and it works , its great , yeah but the fun ends anfte the enterd number is 88 or more than i got no output and i dont knw the reason so i would like to find it out but its allready 36 hours sice i started asking and searchy why the heck... so the input is a simple two line input
the number n
and the number m wich is 1 or 2.
my code looks like this
#include <fstream>
using namespace std;
int main()
{
long int n,m,rj,l,x;
bool used[n];
ifstream in("koder.in");
ofstream out("koder.out");
in>>n;
in>>m;
for (int i=1;i<=n;i++)
{used[i]=false;}
switch (m){
case 1:
l=n%3;
rj=n/2;
l=1-l;
while (rj!=1)
{
for (int j=2;j<=n;j+=2)
{
if (used[j]==true)
{continue;}
else
{
if(rj==1)
{break;}
if(l==0)
{
rj-=1;
used[j]=true;
l=1;
}
else
{
l=0;
continue;
}
}
}
}
for (int j=2;j<=n;j+=2)
{
if (used[j]==false)
{out<<j<<endl;
}
}
break;
case 2:
l=n%3;
rj=n/2;
l=1-l;
while (rj!=1)
{
for (int j=1;j<=n;j+=2)
{
I also tried it with vectors but no result from it either.I would appriciate youre help and thanks.
jeron
March 4th, 2008, 04:32 PM
long int n,m,rj,l,x;
bool used[n]; //n is zero at this point
ifstream in("koder.in");
ofstream out("koder.out");
in>>n;
in>>m;
for (int i=1;i<=n;i++) //should be i < n
{used[i]=false;}
The array 'used' is declared with 0 entries, n was declared in the previous line and initialized by the compiler I think to 0. However, even if it was declared with some other value, the FOR loops used to access the array are looping too far. For an array of size n, the largest index can be (n-1).
If you want a dynamically sized array, vectors are probably your best bet.
MrViggy
March 4th, 2008, 05:42 PM
long int n,m,rj,l,x;
bool used[n]; //n is zero at this point
ifstream in("koder.in");
ofstream out("koder.out");
in>>n;
in>>m;
for (int i=1;i<=n;i++) //should be i < n
{used[i]=false;}
The array 'used' is declared with 0 entries, n was declared in the previous line and initialized by the compiler I think to 0. However, even if it was declared with some other value, the FOR loops used to access the array are looping too far. For an array of size n, the largest index can be (n-1).
If you want a dynamically sized array, vectors are probably your best bet.
Actually, the compiler will not initialize any of the variables to any value. So, if uninitialized, they will contain garbage.
However, this is invalid, and shouldn't compile:
bool used[n];
You cannot create a dynamic array in this manner.
Viggy
KRUNCH
March 5th, 2008, 08:32 AM
Actually, the compiler will not initialize any of the variables to any value. So, if uninitialized, they will contain garbage.
However, this is invalid, and shouldn't compile:
bool used[n];
You cannot create a dynamic array in this manner.
Viggy
well the boolean part does compile , it actualy does go to n and not n-1 dunno why but i tested it and it printed out the right values but ill try it.ill post the results...
result : nothing changed;
MrViggy
March 5th, 2008, 03:26 PM
well the boolean part does compile , it actualy does go to n and not n-1 dunno why but i tested it and it printed out the right values but ill try it.ill post the results...
result : nothing changed;
No, it does not compile:
Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 6: error: expression must have a constant value
bool used[n];
^
"ComeauTest.c", line 5: warning: variable "x" was declared but never referenced
long int n,m,rj,l,x;
^
1 error detected in the compilation of "ComeauTest.c".
That's an error.
Viggy
potatoCode
March 5th, 2008, 05:58 PM
Hello KRUNCH,
long int n,m,rj,l,x;
bool used[n];
Believe it or not, this does compile with Dev-C++.
This is a shocking to me because like Mr.Viggy said, I thought the array dimension must be the constant expression.
but after thinking about it, I think it only compiled because C++ is statiscally typed language. (I'd really appreciate if someone could educatte me further on this issue).
What I mean by this is that, as far as I could tell, those 2 lines of statements do not violate the syntax rules.
on top of these,
you are not doing anything with them even though they are in error.
and you did give them proper values from ifstream.
but if you do a quick test in between there, for example
bool used[n];
cout << used[0]; << endl;
// same as before
you will clearly see that you did something wrong and it is a run-time error.
so my advice to you is that listen to Mr.Viggy.
hope this helped.
Paul McKenzie
March 5th, 2008, 06:35 PM
Hello KRUNCH,
long int n,m,rj,l,x;
bool used[n];
Believe it or not, this does compile with Dev-C++.It will not compile if you use the proper compiler switches. Dev-C++ uses the g++ compiler, and setting the ANSI switch to "on" will not make that code compile.
Regards,
Paul McKenzie
potatoCode
March 5th, 2008, 07:13 PM
I believe my compiler does gives out the error,
it's been doing that up until I tried this.
I checked the compiler setting, and searched the documentations about ANSI setting,
so far the only ANSI setting I could tell is under C compiler option.
is this what you're talking about?
if not,
how do I turn it on?
Thanks.
Paul McKenzie
March 5th, 2008, 07:25 PM
I believe my compiler does gives out the error,
it's been doing that up until I tried this.
I checked the compiler setting, and searched the documentations about ANSI setting,
so far the only ANSI setting I could tell is under C compiler option.
is this what you're talking about?I would think so. Unless you're compiling using C99 language enabled, declaring an array using a variable as the index is illegal in both C and C++.
Regards,
Paul McKenzie
KRUNCH
March 7th, 2008, 08:52 AM
well yes i solved the problem a bit earlier but i didnt have the time so didnt post , but yes i added a constant value (like 2 milions) but it only works for that far , after that it puts out a runtime error , but well it works , but i need it to go to like numbers like 50 ciphers so ill have to get a better algorithm.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.