CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2014
    Posts
    15

    Fill Array with even numbers only

    Hey guys i'm having a little issue here
    i need to fill two 2D arrays, the first one with even numbers from 1 to 50, the second one with odd numbers from 1 to 50. Heres what i have

    Code:
    #include <iostream>
    #include <ctime>
    #include <iomanip>
    using namespace std;
    
    int main()
    {  
    	srand(time(NULL));
    	int TA1[8][6];
    	int TA2[8][6];
        int x;
    	for ( int row=0;row<8;row++)   
    	{	for ( int col=0;col<6;col++)
    		{  
    			x = rand()%50 + 1;
    			if ( x%2 != 0 )
    				TA1[row][col] = x;
    			
    	
    	
    	    }
    	}
    	
    	for ( int row=0;row<8;row++)
    	{
    		for(int col=0;col<6;col++)
    			cout<<right<<setw(5)<< TA1[row][col] << " ";
    		
    		cout<<endl;
    	}
    
       for( int row=0; row<8;row++)
       {   
    	   x=0;
    
    	   for ( int col=0;col<6;col++)
    	   {    x=rand()%50 +1;
    	       
    	      if( x%2 ==0)  
    		   TA2[row][col] =x;
    
    		  else continue;
    
    	  
    
    	   }
       }
    
    for ( int row=0;row<8;row++)
    	{
    		for(int col=0;col<6;col++)
    			cout<<right<<setw(5)<< TA2[row][col] << " ";
    		
    		cout<<endl;
    	}
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Fill Array with even numbers only

    So... what exactly is the "little issue"?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Sep 2014
    Posts
    15

    Re: Fill Array with even numbers only

    Ohh sorry i forgot to mention it
    when i compile the program i get trash numbers in the array
    however if i just fill the array with normal random number( not specifying if odd or even) the program works
    any ideas on how to fix this?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Fill Array with even numbers only

    Ah, look carefully:
    Code:
    x = rand()%50 + 1;
    if ( x%2 != 0 )
        TA1[row][col] = x;
    You're saying: generate a number in the range [1, 50]. If the number is odd, assign it to TA1[row][col]. Otherwise, move on. As such, if it so happens that you get a long enough sequence of odd numbers, none of the elements of your array would have been assigned a value.

    Instead of checking if the number is odd, a better approach would be to generate an odd number. For example, notice that for an integer n, 2n+1 would be an odd number. Perhaps you could figure out a range for n such that you could apply this formula to obtain odd numbers, and hence generate pseudorandom numbers within that range.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Fill Array with even numbers only

    You can add internal loop and call rand until it generates desired number - odd or even.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Fill Array with even numbers only

    Quote Originally Posted by AhmadMough View Post
    when i compile the program i get trash numbers in the array
    When you "compile" the program you get one or more object modules (if there is no errors in code).
    Then the linker links the executable module from all these "object modules" and usually some additional "object or library modules".
    And when you run the program you may get "trash numbers in the array" (if your code is not logically correct!)
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured