# Fill an array with just even numbers

Page 1 of 1

## 11 Replies - 11867 Views - Last Post: 11 November 2008 - 11:03 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=71507&amp;s=f6cf35621ad785d5a0a144c16610ccfa&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 EricFG

Reputation: 1
• Posts: 7
• Joined: 10-November 08

# Fill an array with just even numbers

Posted 11 November 2008 - 08:12 PM

I need to fill an array with just even numbers and I am not sure exactly what to do. I know to get even numbers you can use %2, but I do not know how to include that in filling my array. Do I need to implement the %2 into my "array[i] = rand() % 48"?

``` void fill_even(int array[], int *count)

{

int i;
int howmany;
srand(time(NULL));

printf("Enter the number of components to be filled <= %d  ",SIZE);
scanf("%d", &howmany);

for (i= 0; i < howmany; i++){
if (array[i] % 2 == 0)
array[i] = rand() % 48;
}

*count = howmany;

printf("\n");

}

```

Is This A Good Question/Topic? 0

## Replies To: Fill an array with just even numbers

### #2 no2pencil

• Professor Snuggly Pants

Reputation: 6630
• Posts: 30,892
• Joined: 10-May 07

## Re: Fill an array with just even numbers

Posted 11 November 2008 - 08:16 PM

*count = howmany; <-- What exactly is this for?

Also, an important part to this is, how are you getting these numbers? It looks to me like you are prompting the user for how large the make the array. What numbers are being checked for evens?

I would think you would check for evens, if even, throw it into the array.

```int numtocheck=0;
...
if(numtocheck % 2 == 0) {
array[i]=numtocheck;
i++
}

```

### #3 EricFG

Reputation: 1
• Posts: 7
• Joined: 10-November 08

## Re: Fill an array with just even numbers

Posted 11 November 2008 - 08:20 PM

*count = howmany is there because count is a pointer to how many in my main(void) switch. I will try that out. Also, I am prompting the user how big the array should be and I only want even numbers to fill the array.

This post has been edited by EricFG: 11 November 2008 - 08:22 PM

### #4 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Fill an array with just even numbers

Posted 11 November 2008 - 08:23 PM

It would be inefficient to call rand(), check to see if it returned an even answer and run it again if not.

### #5 EricFG

Reputation: 1
• Posts: 7
• Joined: 10-November 08

## Re: Fill an array with just even numbers

Posted 11 November 2008 - 08:29 PM

It's compiling, but not generating the even numbers to fill the array. Should I remove the rand()?

``` void fill_even(int array[], int *count)

{

int numtocheck;
int i;
int howmany;
srand(time(NULL));

printf("Enter the number of components to be filled <= %d  ",SIZE);
scanf("%d", &howmany);

for (i= 0; i < howmany; i++){

if(numtocheck % 2 == 0) {
array[i]=numtocheck;
i++;
}
array[i] = rand() % 50;
}

*count = howmany;

printf("\n");

}

```

[/quote]

This post has been edited by EricFG: 11 November 2008 - 08:30 PM

### #6 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: Fill an array with just even numbers

Posted 11 November 2008 - 08:32 PM

x2 each random number in the range a/2..b/2 desired

This post has been edited by David W: 11 November 2008 - 08:34 PM

### #7 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Fill an array with just even numbers

Posted 11 November 2008 - 08:42 PM

The issue with your current code is that numtocheck is not initialized before being assigned to the array. Try this:

```#include <iostream>
#include<time.h>
using namespace std;

int main()
{
srand(time(NULL));
int arr[10];
for (int i = 0 ; i < 10; i++)
{
arr[i] = ((rand() % 10) *2); //plant even numbers within array
//(change 10 to whatever the max range you want
)
}
for (int i = 0 ; i < 10; i++)
{
cout << arr[i] << " "; // display said numbers
}
return 0;
}

```

### #8 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: Fill an array with just even numbers

Posted 11 November 2008 - 09:29 PM

Or this ...

```#include <iostream>
#include <iomanip>
#include <time.h>

using namespace std;

// Global constants
const int MAX  = 100;
const int LOW  = 10;
const int HIGH = 20;

int main()
{
srand( time(NULL) );

int ary[MAX];
for( int i = 0; i < MAX; ++i )
ary[i] = 2*(rand()%((HIGH-LOW)/2 +1 )) + LOW; // get only even numbers

for( int i = 0 ; i < MAX; ++i )
cout << left << setw(8)<< ary[i]; // display array

cin.get(); // wait for 'Enter' to be pressed
}
```

### #9 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Fill an array with just even numbers

Posted 11 November 2008 - 09:32 PM

I refuse to make a more complicated solution to compete with you

### #10 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: Fill an array with just even numbers

Posted 11 November 2008 - 10:50 PM

Quote

I refuse to make a more complicated solution to compete with you

Shalom KYA and Eric,

No competition intended ...

It was just an example for the OP, Eric, of a few things like using Global constants to make a more general solution.

So, now, to Eric ...

if you want to make a change to the size of the array or the limits of the random numbers, you can just adjust the 'constants' at the top of your program before you recompile it ...(instead of having to search though the code to make sure you make all the right changes.)

Shalom shalom,

David

### #11 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Fill an array with just even numbers

Posted 11 November 2008 - 11:00 PM

i was jokin'. competition spurs the marketplace and academic achievement.

We could template this as well. Seems overboard though.

### #12 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: Fill an array with just even numbers

Posted 11 November 2008 - 11:03 PM

Quote

...i was jokin'.

Good to see that a team effort is appreciated here at DIC

Shalom,

David