Skip to content

Instantly share code, notes, and snippets.

@hckim16
Created September 20, 2017 21:47
Show Gist options
  • Select an option

  • Save hckim16/b1a601aefb54ab8c4975a57fdd89bd11 to your computer and use it in GitHub Desktop.

Select an option

Save hckim16/b1a601aefb54ab8c4975a57fdd89bd11 to your computer and use it in GitHub Desktop.
Simple code for working 2D array
#include <iostream>
#include <iomanip>
using namespace std;
// global constants for array initialization
const int COL = 5;
const int ROW = 5;
int row;
int col;
int data [ROW][COL] = {{34, 45, 29, 40 ,85},
{12, 559, 9, 39, 58},
{65, 34, 76, 98, 13},
{22, 44, 99, 88, 77},
{81, 83, 95, 72, 31}};
// function protocals
void getTotal(const int array[][COL], int row);
void getAverage(const int array[][COL], int row);
void getRowTotal(const int array[][COL], int row);
void getColunmTotal(const int array[][COL], int row);
void getHighestInRow(const int array[][COL], int row);
void getLowestInRow(const int array[][COL], int row);
int main()
{
cout << "The contents of table are: " << endl;
// functions called in main
getTotal(data, ROW);
getAverage(data, ROW);
getRowTotal(data, ROW);
getColunmTotal(data, ROW);
getHighestInRow(data, ROW);
getLowestInRow(data, ROW);
return 0;
}
// function definition to calculate total of all values
void getTotal(const int array[][COL], int row)
{
int total = 0;
for (int row = 0; row < 5; row ++)
{
for (int col = 0; col < 5; col ++)
total += data[row][col];
}
cout << "Total is " << total << endl;
}
// function that averages all value in 2D array
void getAverage(const int array[][COL], int row)
{
int total = 0;
int average;
for (int row = 0; row < 5; row ++)
{
for (int col = 0; col < 5; col ++)
total += data[row][col];
}
average = total/25;
cout << "Average is " << average << endl;
}
// function calculates total per row
void getRowTotal(const int array[][COL], int row)
{
for (int row = 0; row < 5; row++)
{
int total = 0;
for (int col = 0; col < 5; col ++)
total += data[row][col];
cout << "Total for Row " << (row + 1) << " is " << total << endl;
}
}
// function totals per column
void getColunmTotal(const int array[][COL], int row)
{
for (int col = 0; col < 5; col++)
{
int total = 0;
for (int row = 0; row < 5; row ++)
total += data[row][col];
cout << "Total for Column " << (col + 1) << " is " << total << endl;
}
}
// function finds highest value in reach row
void getHighestInRow(const int array[][COL], int row)
{
for (int row = 0; row < 5; row++)
{
int highest = data [row][col];
for (int col = 1; col < 5; col ++)
{
if (data[row][col] > highest)
highest = data[row][col];
}
cout << "Highest for Row " << (row + 1) << " is " << highest << endl;
}
}
// function finds lowest value in each row
void getLowestInRow(const int array[][COL], int row)
{
for (int row = 0; row < 5; row++)
{
int lowest = data [row][col];
for (int col = 1; col < 5; col ++)
{
if (data[row][col] < lowest)
lowest = data[row][col];
}
cout << "Lowest for Row " << (row + 1) << " is " << lowest << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment