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"); }
5 Replies  3695 Views  Last Post: 22 June 2011  08:06 AM
#1
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.
Replies To: Transferring only positive values from one array to another.
#2
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
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
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
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
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;
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.
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.
Page 1 of 1
