adding positions of 3 arrays

thank you
Last edited on
I see 2 problems.

1) You call printarray (sumscores,n); on line 27 before calling makenewarray(score1,score2,score3, sumscores,n); on line 28.
It should be the other way around. You are getting uninitialized values in the output.

2) The makenewarray() has problems. That k you pass in should be the n in your function. The index for sumscores should be i, not k.
ie:
1
2
3
4
5
6
7
void makenewarray(int old1[], int old2[], int old3[], int sumscores[],int n){

//        int n;
        for(int i=0; i<n; i++)
                sumscores[i] = old1[i]+old2[i]+old3[i];// This should work better
            return;
}

Hope that helps!
Thanks a lot! I have another question. Is it legal to put 2 or more functions into one? For example, can I put smallestsofar and largestsofar under one function?
Could you write one function which does what both smallest() and largest() does in one shot?

Is this what you mean? Call one function to get both the smallest and largest values?

Sure. You could return one (or both) of the values through a variable passed to the function as a reference, the same way that your readthearrays() gets the value of n.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int findlargesmallrange( int smallest, int arr [], int &num, int largest,int arr1 [], int difference)

{

        int smallestsofar;
            smallestsofar=arr[0];
        int largestsofar;
                largestsofar= arr1[0];
        int range;

        for(int i=0; i<num; i++)
        if(smallestsofar>arr[i])
            smallestsofar=arr[i];
        else
            for (int i=0; i <num; i++)
            if (largestsofar<arr1 [i])
                largestsofar = arr1 [i];

                range = largestsofar - smallestsofar;

            return largestsofar;
            return smallestsofar;
            return range;
}


I called findsmallrange in the main
findlargesmallrange(score1,score2,score3,sumscores,n);

This is the error I get.
invalid conversion from 'int*' to 'int'|
Yeah, you didn't quite set that up right.

I was thinking that you wanted to replace (for example)
this:
1
2
int loVal = smallest(score1, n);
int hiVal = largest(score1, n);

with a single call to a fuction which finds both loVal and hiVal at once.
Something like...
void find_hi_lo( int arr[], int size, int& loVal, int& hiVal );
Which would get used like this:
1
2
3
4
5
6
7
int lo_val = 0, hi_val = 0;// local variables in your main() around line 30
find_hi_lo( score1, n, lo_val, hi_val );
cout << "1st array low value = " << lo_Val << " , high value = " << hi_val << endl;
find_hi_lo( score2, n, lo_val, hi_val );
cout << "2nd array low value = " << lo_Val << " , high value = " << hi_val << endl;
find_hi_lo( score3, n, lo_val, hi_val );
cout << "3rd array low value = " << lo_Val << " , high value = " << hi_val << endl;


I don't understand what the code you last posted is supposed to do.
Sorry for the confusing code.

1
2
3
for(int i=0; i<num; i++)
        if(smallestsofar>arr[i])
            smallestsofar=arr[i];


This is suppose to find the smallest number in the array. For example [1] [60] [49]. It will return 1.

1
2
3
for (int i=0; i <num; i++)
            if (largestsofar<arr1 [i])
                largestsofar = arr1 [i];

This is suppose to find the largest number in the array. For example [5] [6] [7], This should return 7.

range = largestsofar - smallestsofar;
This should find the difference of the large number of the array and the smallest number of the array. For example [20] [40] [100] 100-20=80

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int findlargesmallrange (int numb [], int num, int &largest, int &smallest, int &range){

        for(int i=0; i<num;i++)
            if(largest<numb [i])
                    largest=numb[i];
return largest;

        for (int j=0; j<num;j++)
                if (smallest>numb[j])
                    smallest = numb [j];
    return smallest;
            range = largest - smallest;
    return range;

}


Ok, I got the last part down but How do I call the function findlargesmallrange in the main? I keep getting invalid conversion from 'int*' to 'int'| when I do

findlargesmallrange ( score1,score2,score3,sumscores,n);
Topic archived. No new replies allowed.