11 Replies - 7211 Views - Last Post: 11 November 2008 - 11:03 PM Rate Topic: -----

#1 EricFG  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5228
  • View blog
  • Posts: 27,014
  • 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++
}


Was This Post Helpful? 0
  • +
  • -

#3 EricFG  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

Was This Post Helpful? 1

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 EricFG  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#8 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Fill an array with just even numbers

Posted 11 November 2008 - 09:29 PM

Or this ... :D

#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
}

Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • 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 :P
Was This Post Helpful? 0
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • 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 :P


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
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • 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 :D

Shalom,

David
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1