Majd Nabeel
Majd Nabeel

Reputation: 1

about arrays in c++

i've been studying c++ for 3 months , and i studied the arrays , i wrote a program such that will take inputs from user , then the program will store these numbers in a special array , then the program will split them into two arrays , one for even numbers, the other one for odd numbers , my question is , when i tried to display them , there was something wrong happened , but i could not figure it out , can you help me please ?

int main () {
    int even[5];
    int odd[5];
    int num;

    cout << "enter 4 numbers!";
    for(int i=0; i<4; i++) {
        cin >> num;
        if( num%2 == 0){
            cout << "its an even number!";
            even[i] += num;
        }
        else{
            cout << "its an odd number!";
            odd[i] += num;
        }
    }
    cout << "The odd number/s is/are: ";
    for( int u=0; u<4; u++){
        cout << odd[u] << endl;
    }

    cout << endl;
    cout << "The even number/s is/are: " << endl;
    for(int z=0; z<4; z++){
        cout << even[z] << endl;
    }
}

Thank you for helping me!

Upvotes: 0

Views: 111

Answers (3)

bloodscript
bloodscript

Reputation: 136

You access values of the array that are declared but not initialized. So there are any values in it. like 6.49e154. Always initialize when declaring!

Upvotes: 0

Rishi
Rishi

Reputation: 1395

I have a slightly different approach:

#include <iostream>
using namespace std;

int main () {
    int array[4]; // array size needs to be 4 only and not 5
    /*int num;*/ // not required
    bool is_odd[4] = {false, false, false, false};


    cout << "enter 4 numbers!\n";
    for(int i=0; i<4; i++) {
        cin >> array[i];
        if( array[i]%2 == 0){
            cout << "its an even number!\n";
        }
        else{
            cout << "its an odd number!\n";
            is_odd[i] = true;
        }
    }
    cout << "The odd number/s is/are:\n";
    for( int u=0; u < 4; u++){
        if (is_odd[u] == true)
            cout << array[u] << endl;
    }

    cout << endl;
    cout << "The even number/s is/are:\n" << endl;
    for(int u=0; u < 4; u++) {
        if (is_odd[u] == false)
            cout << array[u] << endl;
    }
}

Check the answer here: check-answer

Upvotes: 0

Sudheesh Singanamalla
Sudheesh Singanamalla

Reputation: 2297

From your question, it looks like you're trying to split a given integer array into two arrays even and odd. The problem here is the way in which you're allocating the values into the new arrays, You have a counter i which is responsible to put the values into even[i] and odd[i]. So you have a lot of broken sections even[0] might exist but the odd[1] might be the first odd value you obtain. You should have individual counters for storing these values. So the corrections to your code would look as follows

int main () {
    int even[5];
    int odd[5];
    int num;
    int evencount = 0;
    int oddcount = 0;

    cout << "enter 4 numbers!";
    for(int i=0; i<4; i++) {
        cin >> num;
        if( num%2 == 0){
            cout << "its an even number!";
            even[evencount++] = num;
        }
        else{
            cout << "its an odd number!";
            odd[oddcount++] = num;
        }
    }
    cout << "The odd number/s is/are: ";
    for( int u=0; u < oddcount; u++){
        cout << odd[u] << endl;
    }

    cout << endl;
    cout << "The even number/s is/are: " << endl;
    for(int z=0; z<evencount; z++){
        cout << even[z] << endl;
    }
}

Upvotes: 2

Related Questions