output largest number in array

I can't figure out how to get this program to output the largest number in the array. I think my brain just doesn't feel like working and I'm sure I'm missing something REALLY simple.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #include <iostream>
using namespace std;
const int MAX_INTEGERS = 10;
 
void fill_array(int a[], int size, int& number);
double avgArray(const int a[], int number);
void show_avgArray(const int a[], int number);
int findLargest(const int a[], int number);
void showLargest(const int a[], int number);


int main( )
{
    using namespace std;
    int integers[MAX_INTEGERS], number;

    cout << "This program reads from ten integers what the average is and\n"
         << "what the largest number in the list of integers is\n";

    fill_array(integers, MAX_INTEGERS, number);
    show_avgArray(integers, number);
	showLargest(integers, number);


    return 0;
}

void fill_array(int a[], int size, int& number)
{
    cout << "Enter up to " << size << " integers.\n"
         << "Mark the end of the list with a 0.\n";
    int next, index = 0;
    cin >> next;
    while ((next >= 1) && (index < size))
    {
        a[index] = next;
        index++;
        cin >> next;
    }

    number = index;
}

double avgArray(const int a[], int number)
{
    double total = 0;
    for (int index = 0; index < number; index++)
        total = total + a[index];
    if (number > 0)
    {
        return (total/number);
    }
}

void show_avgArray(const int a[], int number)
{
    double average = avgArray(a, number);
    cout << "Average of the " << number 
         << " scores = " << average << endl;
}

int findLargest(const int a[], int number)
{
	int smallest;
	int largest = a[smallest];
	int i;

	for (i = smallest; i <= largest; i++)
	{
		if (a[i] > largest)
			largest = a[i];
	}
	return largest;

}

void showLargest(const int a[], int number)
{
	double largest = findLargest(a, number);
		cout << "The largest number in the list is: \n"
			 << largest << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int findLargest(const int a[], int number)
{
	int smallest;  // not initialized - holds unknown value
	int largest = a[smallest];
	int i;

        // i holds unknown value
        // why i <= largest?
	for (i = smallest; i <= largest; i++)
	{
		if (a[i] > largest)
			largest = a[i];
	}
	return largest;

}
Last edited on
Any recommendations on an easy way of doing this? If statements? Anything you can recommend to figure this out?
why not use a sort algorithm?, compare the sizes and keep moving the largest number to the right, at the end of the sort, show the last element, it should be the larggest....
Figured it out guys! thanks!

1
2
3
4
5
6
7
8
9
int findLargest(const int a[], int number)
{
	for(int i=0;i<5;i++)
    {
        if(a[i]>number)
        number=a[i];
    }
    
	return number;
Topic archived. No new replies allowed.