Home > Net >  How to Fill Arrays Without Overwriting
How to Fill Arrays Without Overwriting

Time:10-22

I'm trying to create and fill a new array from an already given array. But when I try to do this with for loop and array=i; the array just gives out the last value of the original array.

void evensOdds(){


int xs[18]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,555,558,333};
int Odd = 0;
int Even = 0;

// Counts the odds and evens in the array.
for (int i=0; i<18; i  ){

    if (xs[i]%2 == 0) {
        Even  ;
        }
    else {
        Odd  ;
        }
}

// Defines the odd - even arrays.
const int NEven = Even;
const int NOdd = Odd;
int odd_xs[NOdd];
int even_xs[NEven];

// Fills the arrays with proper values.
for (int i=0; i<18; i  ){

    if (xs[i]%2 == 0) {
        even_xs[NEven]= xs[i];
        }
    else {
        odd_xs[NOdd]= xs[i];
        }
}  

std::cout<<"Number of odds in the array:  "<<Odd<<std::endl;
std::cout<<"Number of evens in the array:  "<<Even<<std::endl;

std::cout<<"Odd numbers are= "<<odd_xs[NOdd]<<std::endl;
std::cout<<"Even numbers are= "<<even_xs[NEven]<<std::endl;

return;
}

So the output of this code's cout line is

odd_xs array = 55
even_xs array = 66

Whereas it should've been something like

odd_xs array = 1, 11, 15, 55
even_xs array = 2, 4, 6, 8, 10, 66

How can I do this? Thanks in advance!

CodePudding user response:

You always write to even_xs[NEven] for example, so the value will always be overwritten and at the end only one value will be stored in even_xs[NEven]

Generally there is not enough code provided, for example you would need a loop to print out every element of your array, but we can't see if you are using a loop or just a single cout statement for printing

CodePudding user response:

Assuming you want to put even values in xs into the even_xs array and the odd ones into the odd_xs array, you probably want something like this:

NEven = 0;
NOdd = 0;

for (int i = 0; i < 18; i  ) {
  if (xs[i] % 2 == 0) {
    even_xs[NEven  ] = xs[i];    
  }
  else {
    odd_xs[NOdd  ] = xs[i];
  }
}

BTW: this code is actually rather C code, in C you would do this differently.

CodePudding user response:

Your code is rather C than C .

This is the idiomatic way in C , using appropriate standard C classes.

#include <iostream>
#include <vector>

void evensOdds() {
  int xs[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,555,558,333 };

  std::vector<int> odd_xs;
  std::vector<int> even_xs;

  for (auto value : xs) {

    if (value % 2 == 0) {
      even_xs.push_back(value);
    }
    else {
      odd_xs.push_back(value);
    }
  }

  std::cout << "Number of odds in the array:  " << odd_xs.size() << std::endl;
  std::cout << "Number of evens in the array:  " << even_xs.size() << std::endl;

  std::cout << "Odd numbers are = "; 

  for (auto value : odd_xs)
    std::cout << value << " ";

  std::cout << "\n";

  std::cout << "Even numbers are = ";
  for (auto value : even_xs)
    std::cout << value << " ";

  std::cout << "\n";

  return;
}

int main()
{
  evensOdds();
}
  • Related