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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void showArray ( int a[], int size );//Function prototype
void showReverse ( int a[], int size ); //Shows the array in reverse using the format "int
int lowest ( int a[ ], int size );//Finds and returns the lowest value in the array
int highest ( int a[ ], int size );
int sumArray ( int a[ ], int size );
float averageVal ( int a[ ], int size );
int count5 ( int a[ ], int size );
int firstMinusLast ( int a[ ], int size );
void showBeforeIndex( int a [ ], int size, int index);
void done ( );
int main(){
srand((time(0)));// to get truly random numbers
const int size = 25; //size of array never changes
int randNumber[size];// array to store random number
int index = 3;
cout <<"Making an array of 25 random integers from 3 to 7!" << endl;
//for loop that will create random numbers and store in array
for(int i=0; i < size; i++){
randNumber[i]= 3 + rand() % 5; // rand number 3 to 7, %5 gives numbers up to 4 and by adding 3 it gives up to 7
}
cout << "Original array a[] = {";
showArray(randNumber, size);// passes array and size to function
cout << "}\n\n";
cout << "Reversed array a[] = {";
showReverse(randNumber, size);// passes array and size to function
cout << "}\n\n";
cout << "Lowest value is: ";// passes array and size to function
int low =lowest(randNumber,size);
cout << low <<"\n\n";
cout <<"Highest value is: ";
int high = highest(randNumber,size );
cout << high << "\n\n";
cout << "The sum of all array elements is: ";
int sum = sumArray (randNumber,size);
cout << sum <<"\n\n";
cout << "The average of all array values is: ";
int avg = averageVal(randNumber,size);
cout << avg <<"\n\n";
cout << "The number 5 appears: ";
int count = count5(randNumber,size);
cout << count <<" times.\n\n";
cout << "The difference between the first and last array element is: ";
int diff = firstMinusLast(randNumber,size);
cout << diff <<"\n\n";
cout << "The array values before index 3 are ";
showBeforeIndex(randNumber,size,index);
done ( );
return 0;
}
void showArray ( int a[], int size ){ // shows the array in the format "int a [ ] = { 3, 7,
for(int i = 0; i < size;i++){
cout << a[i] <<" ";
}
}
void showReverse ( int a[], int size ){ // shows the array in reverse using the format "int
for (int i=0, j= size-1; i <= j;j--){
swap(a[i], a[j]);
cout << a[i] << " ";
}
}
int lowest ( int a[], int size ){// finds and returns the lowest value in the array
int lowest = a[0];
for(int i= 0; i<size; i++){
if (a[i] < lowest){
lowest = a[i];
}
}
return lowest;
}
int highest ( int a[], int size ){
int highest = a[0];
for(int i= 0; i<size; i++){
if (a[i] > highest){
highest = a[i];
}
}
return highest;
}
int sumArray ( int a[], int size ){
int sum = 0;
for(int i= 0; i < size; i++){
sum = sum + a[i];
}
return sum;
}
float averageVal ( int a[ ], int size ){
float average;
float sum = 0;
for(int i= 0; i < size; i++){
sum = sum + a[i];
}
average = sum / size;
return average;
}
int count5 ( int a[], int size ){
int count = 0;
for(int i= 0; i<size; i++){
while(a[i]!=0)
{
if(a[i]%10==5){
count++;
}
a[i]=a[i]/10;
}
}
return count;
}
int firstMinusLast ( int a[ ], int size ){
int difference = a[0]- a[size-1];
return difference;
}
void showBeforeIndex( int a [ ], int size, int index){
for(int i=0; i < index; i++)
cout << a[i];
}
void done ()
{
cout << "\n\nI am now a pro at functions!\n";
}
|