### 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
// 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

`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.

 ``1234567`` ``````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):

 ``123456`` ``````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]; }``````
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` ``````#include #include #include 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.