Arrays

Hi guys, Im having issues with my array program. I would like to get the largest and smallest numbers in my array but only get "10, 10,". Any ideas?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include<iostream>

using namespace std;

int large(int r[])
{
	int largest = 0;
	int smallest = 0;
	int newLarge, newSmall;
	for (int i = 0; i < 6; i++)
	{
		if (r[i] > largest)
			r[i] = newLarge;
		else if (r[i] > newLarge)
			r[i] = newLarge;
		else if (r[i] < newLarge)
			r[i] = newSmall;
		else if (r[i] < newSmall)
			r[i] = newSmall;
	}

	cout << newLarge << ", " << endl;
	cout << newSmall << ", " << endl;
	return 0;
}

int main()
{
	int n[] = { 10 ,15, 9, 3, 26, 41 };
	large(n);

	system("pause");
	return 0;
}
Before entering your loop, set largest and smallest equal to the first element in the array you're passing. Loop through the remainder of the array. If you find any numbers larger or smaller, set your update your largest/smallest variable.
closed account (28poGNh0)
your half problem is that you assing variable to a variable but inversly!
r[i] = newLarge; // wrong way
newLarge = r[i]; // right way
Thanks guys i got it running. Here is the finished product. I made it a two array and gave it an average just incase anyone like me is looking for examples to study from.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>

using namespace std;

const int row = 2;
const int colm = 3;

int r[row][colm];

int large(int r[row][colm])
{
    int largest = r[0][0];
    int smallest = r[0][0];
    int newLarge, newSmall;
    int average;
    for (int i = 0; i < row; i++)
        for(int j=0; j<colm; j++)
    {
        if (r[i][j] > largest)
            newLarge = r[i][j];
        else if (r[i][j] > newLarge)
            newLarge = r[i][j];
        else if (r[i][j] < newLarge)
            newSmall = r[i][j];
        else if (r[i][j] < newSmall)
            newSmall = r[i][j];
        average += r[i][j]/2;
    }
    
    cout << newLarge << " is the largest " << endl;
    cout << newSmall << " is the smallest " << endl;
    cout << average << " is the average" <<endl;
    return newLarge, newSmall, average;
}

int main()
{
    int r[row][colm] = { {10 ,15, 6,}, {11, 26, 41} };
    large(r);
    
    return 0;
}
Topic archived. No new replies allowed.