PLEASE HELP WITH THIS

my program is supposed to output all prime numbers, given the array of any size of elements.
why isnt it working? when I compile it i don't get an error and ill get is blank.
please help its to help me on my final exam.



#include <iostream>
using namespace std;


void intialize(int a[], int size)
{
for(int i=0; i<size; i++)
a[i]=1;
}

void eliminate (int array[], int k, int size)
{
for(int i=k+1; i<size; i++)
if(i%k==0)
array[i]=0;
}

int find_next_value(int array[], int k, int size)
{
k++;
while(array[k]!=1)
k++;
return k;
}

int main()
{
int index;
int size;
cin>>size;
int array[size];
intialize(array,size);
int k = 2;
do
{
index=k;
eliminate(array,index, size);
find_next_value(array,k,size);
}while(k<size);

for(int i=1; i<size; i++)
{
if(array[i]==1)
cout<<i<<endl;
}

return 0;
}
Last edited on
The array size has to be a constant. It can't be a variable.

The space for the array is set aside at compile time. The variable that you're trying to initialise the array with is set at runtime--long after the program is compiled.
The array size has to be a constant. It can't be a variable.

Wrong. The first dimension can be variable.
The space for the array is set aside at compile time.

Also wrong. It is set aside at runtime on the stack, because the array is an automatic variable.
Wrong. The first dimension can be variable.


Not in this case.

Also wrong. It is set aside at runtime on the stack, because the array is an automatic variable.


Actually the space it takes is calculated at compile time here.
Then why do I get no errors when I compile it?
I reference the C++ FAQ lite, by Marshall Cline.
"even built-in array types can specify the first index bounds at run-time."
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.20
Last edited on
To quote that site:

Does C++ have arrays whose length can be specified at run-time?
Yes, in the sense that the standard library has a std::vector template that provides this behavior.
No, in the sense that built-in array types need to have their length specified at compile time.
Yes, in the sense that the standard library has a std::vector template that provides this behavior.

No, in the sense that built-in array types need to have their length specified at compile time.

Yes, in the sense that even built-in array types can specify the first index bounds at run-time.

Quoting only the part that supports your view is such an ugly way to argue.
In any case, even if it were technically illegal, this construct compiles and runs as expected on every compiler I've ever used.
Last edited on
^ lol
By instance gcc with -pedantic-errors
error: ISO C++ forbids variable length array
error: ISO C90 forbids variable length array
Last edited on
Why do you think you need -pedantic-errors? Why does this not show up with -Wall -Wextra?
Here we come to a conclusion about C++ as specified in a standard and C++ as actually used in real life. As most C++ compilers implement this feature (it's simply a special syntax for the ancient alloca() function, and is part of C99), it is used in real code, so it is beneficial for future C++ compilers not to issue a warning when this feature is used. So a de-facto standard has evolved which may be different from the actual text of the C++ standard.
Last edited on
In any case, even if it were technically illegal, this construct compiles and runs as expected on every compiler I've ever used.


Try using the LLVM compiler:
j@j-desktop:~/badCode$ clang++ 318.cpp 
318.cpp:31:10: error: variable length arrays are not permitted in C++
int array[size];
         ^
1 diagnostic generated.

j@j-desktop:~/badCode$ clang++ --version
clang version 1.1 (branches/release_27)
Target: x86_64-pc-linux-gnu
Thread model: posix

j@j-desktop:~$ llvmc --version
Low Level Virtual Machine (http://llvm.org/):
  llvm version 2.7


Last edited on
You're writing non-portable (non-correct) code!

GCC enables a number of extensions and supports a number of standards. Cool right? Well, almost. They allow newer features in even when you explicitly ask for support for an older standard, C89 for instance.

To quote the GGG man page:
The -ansi option does not cause non-ISO programs to be rejected gratuitously. For that, -pedantic is required in addition to -ansi.


This means, if you want to enforce a particular standard, you have to use -pedantic along with -std=cNN. The other warning flags allow new features in.

Now all that's great until you attempt to recompile your code under a compiler that doesn't support all those lovely extensions.

As I recall, Ritchie was particularly annoyed about this particular point (allowing variable length arrays), but conceeded that he'd have to live with it because he hadn't made a fuss about it when it was initially proposed.

I am particularly annoyed about it because a pointer to an array is not the same as an array. There's an extra layer of indirection which your system programming language is now hiding.

My personal view is C89 was good enough. restrict is a reasonable addition because it follows in the tradtion of register to be a hint to the compiler. But I can't think of a justifyable reason to create a second C standard when C++ exists.

The reason I gave why posted code didn't compile is correct. And I hope I've shet some light on why rocketboy9000's code behaves differently.

Consider this example:
1
2
3
4
5
6
7
int main()
{
        int sz = 3;
        int array[sz];

        return 0;
}


-ansi is the same as -std=89
1
2
3
$ cc -pedantic -ansi x.c -o x
x.c: In function `main':
x.c:4: warning: ISO C90 forbids variable-size array `array'


$ cc -pedantic -std=c99 x.c -o x
Last edited on
Topic archived. No new replies allowed.