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
|
#include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
// Function prototypes
void showArray ( int a[ ], int size ); // shows the array in the format "int a [ ] = { 3, 7, 4, ... ,5, 6, 3, 4, 7 } "
int count5 ( int a[ ], int size ); // returns how many times the number 5 appears in the array
// **************************************************************************************************************************************
int main ()
{
// Array and reverse array
srand((int)time(NULL));
int i=0;
const int SIZE = 25;
int randvalue[SIZE];
cout << "Making an array of 25 random integers from 3 to 7!" << endl;
for(; i < SIZE; i++)
{
randvalue[i] = rand () % 5 + 3; // random number between 3 to 7
}
cout << "Original array a [ ] = {";
showArray(randvalue, SIZE);
cout << "}" << endl;
int j = SIZE-1;
i = 0;
while( i <= j)
{
swap(randvalue[i], randvalue[j]);
i++;
j--;
}
cout << "Reversed array a [ ] = {";
showArray(randvalue, SIZE);
cout << "}" << endl;
// *********************************************************************
// Function call for number of times 5 appears
int return5 = count5 (randvalue, 25);
cout << "The number 5 appears " << return5 << " times." << endl;
// **********************************************************************
return 0;
}
// Function definition for ARRAY
void showArray ( int a[ ], int size )
{
int sum = 0;
for(int i = 0; i < size; i++)
cout << a[i];
}
// Function definition for number of 5's
int count5 ( int a[ ], int size )
{
int count = 0;
for (int i = 0; i < size; i++) {
if (a[i] == 5)
count ++;
return count;
}
}
|