Transferring only positive values from one array to another.

Page 1 of 1

5 Replies - 4343 Views - Last Post: 22 June 2011 - 08:06 AMRate 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=236652&amp;s=f00ff1773f03e243946748dc3b42ebee&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 defcon18

Reputation: 1
• Posts: 28
• Joined: 06-June 11

Transferring only positive values from one array to another.

Posted 22 June 2011 - 07:04 AM

I keep getting a memory address when I try to use this to transfer the data. I will also need to count the values in the array after I get only the positive values inside.

```int function4 (int x[], int cnt, int arr2[]){
int i;
for (i = 0; i <= cnt; i++){
if (x[i] >= 0){
for (i=0; i<cnt; i++){
arr2[i]=x[i];}
cout << arr2 << endl;}
return cnt;}

int main() {

int x[100]={1,-2,-3,4,-56,-7,8, 10, 11, -12, 13};
int n=11;
int y[100];
int num=0;

num=function4(x, n, y);
cout << num << endl;
system("pause");
}
```

Is This A Good Question/Topic? 0

Replies To: Transferring only positive values from one array to another.

• Saucy!

Reputation: 6219
• Posts: 23,965
• Joined: 23-August 08

Re: Transferring only positive values from one array to another.

Posted 22 June 2011 - 07:07 AM

```for (i = 0; i <= cnt; i++){
```

You're going past the end of your array here. Arrays are indexed from 0 to one less than the number of elements.

#3 defcon18

Reputation: 1
• Posts: 28
• Joined: 06-June 11

Re: Transferring only positive values from one array to another.

Posted 22 June 2011 - 07:10 AM

Thanks missed that, but why won't my code transfer the positive values from one array to another?

#4 trixt.er

• D.I.C Regular

Reputation: 52
• Posts: 428
• Joined: 28-September 08

Re: Transferring only positive values from one array to another.

Posted 22 June 2011 - 07:44 AM

defcon18, on 22 June 2011 - 08:10 AM, said:

Thanks missed that, but why won't my code transfer the positive values from one array to another?

Because your boolean logic is incorrect.
Consider this revision to your function;
```int function4 (int x[], int &j, int cnt, int arr2[])
{
int i;
for (i = 0; i < cnt; i++)
{
if (x[i] < 0)
{
/*for (i=0; i<cnt; i++)
{
arr2[i]=x[i];
}
cout << arr2 << endl; */
arr2[j] = x[i];
j++;
}
}
return cnt;
}

```

Notice the x[i] < 0. We don't need another for loop. You can add a j parameter that is pass by reference to your function. This
variable can be used to store the number of negative numbers that where found. This variable can be used to print out the
contents of the negative numbered array in mine like so;

```int main()
{
int x[100]={1,-2,-3,4,-56,-7,8, 10, 11, -12, 13};
int n=11;
int y[100];
int num = 0;
int numNegatives = 0;

num = function4(x, numNegatives, n, y);
cout << num << endl;
for (int i = 0; i < numNegatives; i++){
cout << y[i] << " ";
}
cout << endl;
system("pause");
}

```

So I don't even need the num variable. Remember that & means pass by reference. Any changes made within a function will be persistent within the calling function (e.g. main).

#5 defcon18

Reputation: 1
• Posts: 28
• Joined: 06-June 11

Re: Transferring only positive values from one array to another.

Posted 22 June 2011 - 07:54 AM

The problem with that is that I am only submitting the function not the main program. Since I have to do that my function must call the same thing as the main program. So I cannot modify the number of variables the function calls. I managed to get count working though, but I still am confused on how to transfer the values between arrays.

```int function4 (int x[], int cnt, int arr2[]){
int i, count=0;
for (i=0; i < cnt; i++){
if (x[i] > 0)
count++;
arr2[count]=x[count];}
cout << count << " " << arr2 << endl;
return count;
```

This post has been edited by defcon18: 22 June 2011 - 07:56 AM

#6 trixt.er

• D.I.C Regular

Reputation: 52
• Posts: 428
• Joined: 28-September 08

Re: Transferring only positive values from one array to another.

Posted 22 June 2011 - 08:06 AM

Well it's just how you're indexing into your array during the assignments;

```int function4 (int x[], int cnt, int arr2[])
{
int i, count = 0;
for (i = 0; i < cnt; i++)
{
if (x[i] < 0)
{
arr2[count] = x[i];
count++;
}
}
cout << count << " " << arr2 << endl;
return count;
}

```

The above code will check to see if x[i] < 0 (e.g. a negative number). If the
number is negative assign that number (x[i]) to the arr2 at count. Since initially count is zero
we will store x[i] where x[i] < 0 at arr2[0]. Count will then update itself.