-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA21_2-D_arraycomputations.cpp
167 lines (140 loc) · 3.8 KB
/
A21_2-D_arraycomputations.cpp
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
159
160
161
162
163
164
165
166
167
// 2-D Array Computations
// By Emily Dayanghirang and Alina Corpora
#include <iostream>
#include <iomanip>
using namespace std;
// Global constants
const int ROWS = 3;
const int COLUMNS = 5;
const double NUMS = ROWS * COLUMNS;
// Function prototypes
double getTotal(int [][COLUMNS], int);
double getAverage(int [][COLUMNS], int);
double getRowTotal(int [][COLUMNS], int);
double getColumnTotal(int [][COLUMNS], int, int);
double getHighestInRow(int [][COLUMNS], int);
double getLowestInRow(int [][COLUMNS], int);
int main()
{
int twoDArray[ROWS][COLUMNS] = {{17,25,2,10,4},
{22,78,32,56,18},
{6,46,19,97,86}
};
double average;
// Print 2-D array
cout << "\n The 2-D Array is: \n";
for(int row = 0; row < ROWS; row++)
{
for(int column = 0; column < COLUMNS; column++)
{
cout << " " << twoDArray[row][column] << " ";
}
cout<<"\n";
}
// Get total
cout << "\nThe total of all the values "
<< "in the 2-D array: " << getTotal(twoDArray,ROWS)
<< endl << endl;
// Get average
average = getAverage(twoDArray,ROWS);
cout<<"The average of all the values in the "
<< "2-D array: "<< setprecision(4)
<< average << endl << endl;
// Row totals
for(int row = 0; row < ROWS; row++)
{
cout << "The total for row " << row+1 << ": "
<< getRowTotal(twoDArray,row) << endl;
}
cout << endl;
// Column totals
for(int col = 0; col < COLUMNS; col++)
{
cout << "The total for col " << col+1 << ": "
<< getColumnTotal(twoDArray,col,ROWS) << endl;
}
cout << endl;
// Highest in row
for(int row = 0; row < ROWS; row++)
{
cout << "The highest value for row " << row+1 << ": "
<< getHighestInRow(twoDArray,row) << endl;
}
cout << endl;
// Lowest in row
for(int row = 0; row < ROWS; row++)
{
cout << "The lowest value for row " << row+1 << ": "
<< getLowestInRow(twoDArray,row) << endl;
}
}
// Get total
double getTotal(int twoDArray[][COLUMNS], int rows)
{
double total = 0;
for(int row = 0; row < rows; row++)
{
for(int column = 0; column < COLUMNS; column++)
{
total += twoDArray[row][column];
}
}
return total;
}
// Get average
double getAverage(int twoDArray[][COLUMNS], int rows)
{
double total = 0;
double average;
for (int row = 0; row < rows; row++)
{
for (int column = 0; column < COLUMNS; column++)
{
total += twoDArray[row][column];
}
}
average = total/NUMS;
return average;
}
// Get row total
double getRowTotal(int twoDArray[][COLUMNS], int row)
{
double rowTotal = 0;
for(int column = 0; column < COLUMNS; column++)
{
rowTotal += twoDArray[row][column];
}
return rowTotal;
}
//Get column total
double getColumnTotal(int twoDArray[][COLUMNS], int columns, int rows)
{
double total = 0;
for(int row = 0; row < rows; row++)
{
total += twoDArray[row][columns];
}
return total;
}
// Get highest in row
double getHighestInRow(int twoDArray[][COLUMNS], int row)
{
double highestInRow = twoDArray[row][0];
for(int column = 0; column < COLUMNS; column++)
{
if(highestInRow < twoDArray[row][column])
highestInRow = twoDArray[row][column];
}
return highestInRow;
}
// Get lowest in row
double getLowestInRow(int twoDArray[][COLUMNS], int row)
{
double lowestInRow = twoDArray[row][0];
for(int column = 0; column < COLUMNS; column++)
{
if(lowestInRow > twoDArray[row][column])
lowestInRow = twoDArray[row][column];
}
return lowestInRow;
}