#Im not sure what im doing wrong pls help soon

25 messages · Page 1 of 1 (latest)

hallow flint
#

this is my code
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, const char * argv[]) {

int x=0;
int y;
int check;
cin>>y;
int array[y];
int array1 [y];
while (x<y) {
    
    check=rand() % 101;
    if (check>100){
        if (check<0){
            array[x]=7;
        }
        
    }
    
    cout<<array[x]<<endl;
    x++;
    
}
x=0;
while (x<y) {
    if((array[x]%2)==0){
        cout<<array[x]<<" is even"<<endl;
        
    }
    else if ((array[x]%2)>0){
        array1[x]=array[x];
        
    }
    x++;
}
x=0;
while (x<y) {
   
        cout<<array1[x]<<" is odd "<<endl;
    
    x++;
}

}
And this is the out come
9
0
1
0
11892
1
4287072
1
-381468224
0 is even
0 is even
11892 is even
4287072 is even
-381468224 is even
9 is odd
1 is odd
1 is odd
1 is odd
1876947472 is odd
1 is odd
-2125276220 is odd
1 is odd
48 is odd
Program ended with exit code: 0
Im making a program that checks how many numbers you want it to use and chooses that many random numbers between 1 -100.

icy trenchBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

mighty ginkgo
# hallow flint this is my code #include <iostream> #include <cstdlib> using namespace std; int...
check=rand() % 101; // This doesn't give a random number from 1-100, it gives a random number from 0-100

// Correct way would be:
check=(rand() % 100) + 1;
if (check>100){
  if (check<0){
    array[x]=7;
    // If you want to set array[x] to 7 if the number is out of
    // this won't do since you're checking conditions separately
    // This line of code will ONLY be reached if the number is
    // simultaneously >100 and <0 which is impossible so this
    // never gets executed
  }
}

// Correct form would be:
if(check > 100 || check < 0) { // If number is >100 OR <0
  array[x] = 7;
}
#

ALSO

#

in this piece of code:

while (x<y) {

        check=rand() % 101;
        if (check>100){
            if (check<0){
                array[x]=7;
            }

        }

        cout<<array[x]<<endl;
        x++;

    }```
#

you never said for array[x] = check which is what i suppose you want to do

#

for that reason, array[x] is undefined because you never assigned it anything, and thus, it will just throw random stuff like you saw

#

Finally, why do this:

while (x<y) {
        if((array[x]%2)==0){
            cout<<array[x]<<" is even"<<endl;

        }
        else if ((array[x]%2)>0){
            array1[x]=array[x];

        }
        x++;
    }
    x=0;
    while (x<y) {

            cout<<array1[x]<<" is odd "<<endl;

        x++;
    }

When you can do this?

x=0;
    while (x<y) {
        if((array[x]%2)==0){
            cout<<array[x]<<" is even"<<endl;

        }
        else if ((array[x]%2)>0){
            cout<<array[x]<<" is odd"<<endl;

        }
        x++;
    }```
#

that way you don't have to make a second array

#

also, in the original code, let's say index #2 is odd, array1 would look like this:

array1[0] = undefined
array1[1] = undefined
array1[2] = array[2]

#

and such, it will just spit a lot of garbage

#

putting all this together, this should be the final product:

#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, const char * argv[]) {

    int x=0;
    int y;
    int check;
    cin>>y;
    int array[y];

    while (x<y) {

        check = (rand() % 100) + 1;

        if (check>100 || check<0){
            array[x]=7;
        }

        array[x] = check;
        cout<<array[x]<<endl;
        x++;

    }

    x=0;
    while (x<y) {
        if((array[x]%2)==0){
            cout<<array[x]<<" is even"<<endl;

        }
        else if ((array[x]%2)>0){
            cout<<array[x]<<" is odd"<<endl;

        }
        x++;
    }
}```
#

which should do what you asked for

hallow flint
#

OMG TYSM

mighty ginkgo
#

you're free to try it

hallow flint
#

Thanks im learning how to code c++ and my dad told me to do this so i could learn some concepts i couldnt figure it out

mighty ginkgo
#

glad you could figure it out

#

and be more careful with arrays

hallow flint
#

1 minut brb

mighty ginkgo
#

any time you see garbage numbers getting output it is most likely because of undefined behaviour

icy trenchBOT
#

@hallow flint Has your question been resolved? If so, type !solved :)

hallow flint
#

ok im back

#

thanks or the advice

#

bye

#

!solved