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 84
|
#include <iostream>
#include <stdlib.h>
using namespace std;
const int MAX_INTEGERS = 20; //Max numbers in array always constant
void fill_array(int a[], int size, int& number);
int findLargest(const int a[], int number);
void showLargest(const int a[], int number);
int findSmallest(const int a[], int number);
void showSmallest(const int a[], int number);
int main()
{
if (system("CLS")) system("clear"); //clears garbage from screen
int integers[MAX_INTEGERS], number;
cout << "This program outputs an array backwards." << endl;
cout << "This program also shows you the largest and smallest integer in the array." << endl;
fill_array(integers, MAX_INTEGERS, number);
showLargest(integers, number);
showSmallest(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 != 0) && (index < size)) //prompts user for integers until 0 is input
{
a[index] = next;
index++;
cin >> next;
}
number = index;
cout << "The reverse order is" << endl;
for(int i = size-1; i > 0; --i);
{
cout << a[size];
}
cout << endl;
}
int findLargest(const int a[], int number) //Function computes largest number
{
for(int i=0;i<5;i++)
{
if(a[i]>number)//if number is smaller than array, larger number
number=a[i];
}
return number;
}
void showLargest(const int a[], int number) //Function to display largest number to screen
{
double largest = findLargest(a, number);
cout << "The largest number in this list is: "<< largest << endl;
}
int findSmallest(const int a[], int number) //Function computes smallest number
{
for(int i=0;i<5;i++)
{
if(a[i]<number)//if number is larger than array, smallest number
number=a[i];
}
return number;
}
void showSmallest(const int a[], int number) //Function to display smallest number to screen
{
double smallest = findSmallest(a, number);
cout << "The smallest number in this list is: "<< smallest << endl;
}
| |