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
|
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
const int columns = 4; //Declare constants
const int rows = 3;
void Fill_Array (int [][columns] , int , int ); // Fill_Array prototype
void Copy_Array (int [][columns], int [][rows], int, int); // Copy_Array prototype
void Print_A (int [][columns] , int , int ); // Print_A prototype
void Print_B (int [][rows] , int , int ); // Print_B prototype
void Print_Statistics (int [][columns], int, int); // Print_Statistics prototype
int main() { // main function
int A[rows][columns]; // array A[3][4]
int B[columns][rows]; // Array B [4][3]
Fill_Array ( A, rows, columns);; // test Fill_Array
Copy_Array (A, B, rows, columns ); // test Copy_Array
Print_A(A, rows, columns); //test Print_A
Print_B (B, rows, columns); // test Print_B
Print_Statistics(A, rows, columns); // test Print_Statistics
return 0; // Program ends
} // end main
void Fill_Array (int A[][columns], int rows, int columns) // Fill_Array function, takes array A and fills it
{
srand(time (0)*time(0) - time(0) ); // seed set by clock
for (int i =0; i < 3; i++){ // loop for number of rows
for (int j = 0; j < 4 ; j = j + 2){ //for odd numbers, first number (j = 0) is odd and so is the third number (j = 2)
A[i][j] = 2*(1 + rand() % 24) + 1; // generates random odd number between 1 and 49
} // end odd loop
for (int j = 1; j < 4; j = j + 2) // for even numbers (j = 1, j = 3) and so on
A[i][j] = 2*(1 + rand() % 25) ; // generates random even number between 2 and 50
} // end loop for both
} // end function
void Copy_Array (int A[][columns], int B[][rows], int rows, int columns ) // Copy_Array function, takes B array, copies A into B
{int tem;
for (int i = 0; i < 3 ; i++){ // loop for number of rows for A, columns for B
for (int j = 0; j < 4; j++){ // loop for number of columns for A, rows for B
A[i][j] == B[j][i]; // rows and columns are reversed
} // end loop
} // end loop
} // end function
void Print_A (int A[][columns], int rows, int columns) // Prints A
{
cout << " The elements int array A are: " <<endl;
for (int i = 0; i < 3; i++){ // rows loop
for (int j = 0; j < 4; j++){ // columns loop
cout << A[i][j] << " "; // prints A
if (j == 3) // skips line every fourth number
cout << endl; // end line
} // end loop
} // end loop
} // end function
void Print_B (int B[][rows], int rows, int columns) // Prints B, takes B as an argument
{
cout << endl; // end line
cout << " The elements int array B are: " <<endl;
for (int i = 0; i < 4; i++){ // rows loop
for (int j = 0; j < 3; j++){ // columns loop
cout << B[i][j] << " "; // prints B
if (j == 2) // skips line every third character
cout << endl; // end line
} // end loop
} // end loop
} // end function
void Print_Statistics (int A[][columns], int, int ) // Print statistics
{
cout << endl; // end line
int evensum = 0, oddsum = 0, evenaverage, smallest, element; //declaration of integers, the sum of even and odd numbers, smallest, and temporary element
double oddaverage; // the average of odd numbers is sometimes decimal
cout << "The average of even numbers is: " << endl;
for (int i = 0; i < 3; i++){ // rows loop
for (int j = 1; j < 4; j = j + 2) // columns loop
evensum = (A[i][j] + evensum); // sum of even numbers
}
evenaverage = evensum/6; // average of even numbers (sum divided by the number of even numbers)
cout << evenaverage; // Prints even numbers average
cout << endl; // end line
cout << "The average of odd numbers is: " << endl;
for (int i = 0; i < 3; i++){ //columns loop
for (int j = 0; j < 4; j = j + 2) // rows loop
oddsum = (A[i][j] + oddsum); // odd numbers sum
}
oddaverage = static_cast <double>(oddsum) / 6; //odd number average, sum divided by number of odd numbers
cout << oddaverage; // prints odd numbers average
cout << endl; // end line
smallest = A[0][0]; // begin by supposing first term is smallest
for (int i = 0; i < 3; i++){ // rows loop
for (int j = 0; j < 4; j++){ // columns loop
element = A[i][j]; // assigning the element to an int
if (element < smallest) // if this element is smaller than smallest
smallest = element; // this element becomes smallest
} // end loop
} // end loop
cout << "the smallest element is: " << endl;
cout << smallest; // cout smallest number
} // end function
|