Using Array

Hello I am new to this website and a beginner using c++. I wondering if anybody can possibly help me. (Sorry bad English not native Language) I have everything down except for the highest # and lowest number being displayed.
the arrays are 23, 4, 76, 89, 12, 43, 37, 67, 21, 14.
i have the average which is 38.6 but i stuck getting highest number and lowest.
Last edited on
There are several methods of finding the maximum and minimum entries in an array:

1) Sort the array and the largest and smallest elements will be at the ends,
2) Iterate through the (unsorted) array and seeing which entry is the biggest and which is the smallest. To do this, start with two variables (max and min) and set both to the value of array[0]. Iterate through the array from the second element (since we chose the first element as the starting value for our max and min, we don't need to check it) to the last element, and compare each entry in the array to max and min. If the entry is bigger than max, that entry becomes the new max. If the entry is smaller than min, that entry becomes the new min.
i just tried using while loop i end up getting the smallest highest but at the very end it doesnt want to display
Could you post your code?
// Lab 11
#include <iostream>
#include <iomanip>
#define MAX 10
#define NUM_PER_LINE 6
using namespace std;

int main()
{
int i;
int arr[MAX] = { 0 }; // aray initialized to 0
int sum = 0;
double average = 0;
int smallest = 0, highest = 0;
int j;


for (i = 0; i < MAX; i++) {
cout << "\nEnter values # " << ( i + 1) << " : " ;
cin >> arr[i];
}

//print 6 numbers per line

cout << "\nArrays are ";
cout << "\n\n" ;


for (i = 0, j = 0; i < MAX; i++, j++) {
//j counts numbers printed
if (j == 6) {
j = 0; //rest j
cout << endl; "\n"; //new line
}
cout << setw(6) << arr[i];

}


for (i = 0.0, j = 0.0; i < MAX; i++, j++){


sum += arr[i];

average = sum / 10.0;

while (smallest || highest);
cin >> sum;

if (smallest || highest)
cout << " is Smallest " << endl;
else
cout << " is Highest " << endl;

}


cout << "Average = " << average << endl;
cout << "Smallest = " << smallest << endl;
cout << "Highest = " << highest << endl;



}



now when i run it it gave me highest no lowest. im cant seem to figure. i know 89 is highest and 4 is lowest
Please use [code][/code] tags and format your code.

cout << endl; "\n"; //new line
What is the "\n" for? It isn't being printed and you are already using endl.

for (i = 0.0, j = 0.0; i < MAX; i++, j++)
What is j's purpose and why are you trying to assign a decimal value to an int?

average = sum / 10.0;
This should be after your for loop, not in it.

1
2
3
4
5
6
7
while (smallest || highest);
cin >> sum;

if (smallest || highest)
cout << " is Smallest " << endl;
else
cout << " is Highest " << endl;

I don't understand this.

All you need to do in the last for loop is compare arr[i] to max and min (which should be initialized to arr[0] before the loop):

1
2
3
4
5
6
min = max = arr[0];
for (int i = 1; i < MAX; i++)
{
    if (arr[i] > max) max = arr[i];
    if (arr[i] < min) min = arr[i];
}
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
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int findSmallestRemainingElement (int array[], int size, int index);
void swap (int array[], int first_index, int second_index);
void sort (int array[], int size)
{
for ( int i = 0; i < size; i++ )
{
int index = findSmallestRemainingElement( array, size, i );
swap( array, i, index );
}
}
int findSmallestRemainingElement (int array[], int size, int index)
{
int index_of_smallest_value = index;
for (int i = index + 1; i < size; i++)
{
if ( array[ i ] < array[ index_of_smallest_value ] )
{
index_of_smallest_value = i;
}
}
return index_of_smallest_value;
}

void swap (int array[], int first_index, int second_index)
{
int temp = array[ first_index ];
array[ first_index ] = array[ second_index ];
array[ second_index ] = temp;
}

void displayArray (int array[], int size)
{
cout << "{";
for ( int i = 0; i < size; i++ )
{

if ( i != 0 )
{
cout << ", ";
}
cout << array[ i ];
}
cout << "}";
}

int main ()
{
int array[ 10 ];
srand( time( NULL ) );
for ( int i = 0; i < 10; i++ )
{
// keep the numbers small so they're easy to read
array[ i ] = rand() % 100;
}
cout << "Original array: ";
displayArray( array, 10 );
cout << '\n';
sort( array, 10 );
cout << "Sorted array: ";
displayArray( array, 10 );
cout << '\n';
cin.get();
return 0;
}
Last edited on
Topic archived. No new replies allowed.