Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,836 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,564 people online right now. Registration is fast and FREE... Join Now!




copy array element

 
Reply to this topicStart new topic

copy array element

buthaina
2 Oct, 2008 - 09:06 AM
Post #1

New D.I.C Head
*

Joined: 2 Oct, 2008
Posts: 4

hi

I generated a prime numbers array. This array contains zeros so i wanted to copy the non zero elements of this array to another.

I used a "for" loop that looks for the nonzero elements and then puts them in another array as long as its not end of the array.
the results that i got was either i get an array of Zeros or my program goes crazy.

My brain wont function anymore and i dont know what to do next.
this is tiny part of my program and i cant do right. i am going crazy blink.gif

I would appreciate your help smile.gif

this is part my code :

unsigned int a[max]
unsigned in b[max]
int j=0 , i = 0 ;
int max = 500
for ( i = 0, i <= userinput || i < max , i ++)
if ( a[i] !=0 )
{ b[j] = a[i];
J++;
i++;}
else { i++; }
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Copy Array Element
2 Oct, 2008 - 10:24 AM
Post #2

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 292



Thanked: 2 times
Dream Kudos: 50
My Contributions
Check out these:

http://www.dreamincode.net/forums/showtopic13192.htm

http://www.dreamincode.net/forums/showtopic64341.htm

http://www.troubleshooters.com/codecorn/pr...rimenumbers.htm

wink2.gif

User is offlineProfile CardPM
+Quote Post

buthaina
RE: Copy Array Element
2 Oct, 2008 - 10:57 AM
Post #3

New D.I.C Head
*

Joined: 2 Oct, 2008
Posts: 4

Thanks alot for your help.. but perhaps i didnt get my piont around..

this is my entire code ..
if you ran this you will see a line that has prime numbers and zeros as well..
I wont to only copy the nonzero elements of that array into another ..


CODE

#include<stdio.h>

#define PRIMENUM 100000

void PrimeNum(unsigned int a[], unsigned int UserInput); /* Function prototype */

int main(void)
{
     unsigned int i, UserInput;
     unsigned int j = 0;
     unsigned int a[PRIMENUM];
     unsigned int b[PRIMENUM];
     printf(" Enter a Number:" );
     scanf( "%d", &UserInput);
     while(getchar()!='\n');

     if ( UserInput <=0 )

         {
            printf(" Invalid input, please type a postive non zero number:" );
            scanf( "%d", &UserInput);
            while(getchar()!='\n');
         }

     for (i = 0;i < PRIMENUM; i++)/*filss the array with elements*/
          a[i] = 2 + i;           /* 2,3,4,.., 100000 */

     PrimeNum(a, UserInput);
     for ( i = 0; i <= UserInput && i < PRIMENUM; i++)
         printf(" %d ", a[i]  );

     getchar();
     getchar();
    
     return 0;
}

/*Function void Prime_Num(int num[]) finds the prime integers */
void PrimeNum(unsigned int a[], unsigned int UserInput)
{
     unsigned int i;
     unsigned int j;

     for(i = 0; i <= UserInput && i < PRIMENUM; i++) /*loops through to find the prime integers for the rest of the array*/
     {
          if(a[i]!=0)
          {
               for(j = i + 1; j < PRIMENUM; j++)
               {
                    if(a[j]!= 0)
                    {
                         if((a[j] % a[i])== 0)  /*check if a[j]*/
                         a[j] = 0;                /*is a multiple of a[i]*/
                                                /*if it is a multiple then strike it out*/
                    }
               }
          }
        
}
  /* now i need to copy the nonzero elements of a[] to b[]*/
}

User is offlineProfile CardPM
+Quote Post

baavgai
RE: Copy Array Element
2 Oct, 2008 - 12:04 PM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,040



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Silly question time, why not just:

cpp

for ( i = 0; i <= UserInput && i < PRIMENUM; i++)
if (a[i]!=0) printf(" %d ", a[i] );


A couple minor additions to that and you get your copy, by the way.

User is offlineProfile CardPM
+Quote Post

buthaina
RE: Copy Array Element
2 Oct, 2008 - 04:03 PM
Post #5

New D.I.C Head
*

Joined: 2 Oct, 2008
Posts: 4

Yes, its true that i do that but i want an array that only holds prime numbers becuase i have to perform more operations on it.
Thanks, thou smile.gif

QUOTE(baavgai @ 2 Oct, 2008 - 01:04 PM) *

Silly question time, why not just:

cpp

for ( i = 0; i <= UserInput && i < PRIMENUM; i++)
if (a[i]!=0) printf(" %d ", a[i] );


A couple minor additions to that and you get your copy, by the way.


User is offlineProfile CardPM
+Quote Post

buthaina
RE: Copy Array Element
3 Oct, 2008 - 10:03 PM
Post #6

New D.I.C Head
*

Joined: 2 Oct, 2008
Posts: 4

Thanks ya'll.. I have figured that out and managed to copy the nonzero element smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 04:36PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month