39.4k views
21 votes
Declare an array named numbers with a constant size of 5 X 5.

1. Make/Use a function getNumbers to get the values for every element of the array.
2. Make/Use a function showNumbers to display the array in this form.

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

3. Make/Use a function addRows to add each row and display it.

Total for row 1 is: 15
Total for row 2 is: 40

And so on....

4. Make/Use a function highest to find the highest value in the array

User Sandraqu
by
4.0k points

1 Answer

7 votes

Answer:

In C++:

#include<iostream>

using namespace std;

void getNumberr(int myarray[][5]){

int arr[5][5];

for(int i = 0;i<5;i++){

for(int j = 0;j<5;j++){

arr[i][j] = myarray[i][j]; } } }

void showNumberr(int myarray[][5]){

for(int i = 0;i<5;i++){

for(int j = 0;j<5;j++){

cout<<myarray[i][j]<<" "; }

cout<<endl; } }

void addRows(int myarray[][5]){

for(int i = 0;i<5;i++){

int rowsum = 0;

for(int j = 0;j<5;j++){

rowsum+=myarray[i][j]; }

cout<<"Total for row "<<i+1<<" is: "<<rowsum<<endl; } }

void highest(int myarray[][5]){

int max = 0;

for(int i = 0;i<5;i++){

for(int j = 0;j<5;j++){

if(myarray[i][j]>max){

max = myarray[i][j];

} } }

cout<<"Highest: "<<max; }

int main(){

int myarray[5][5] = {{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}};

getNumberr(myarray);

showNumberr(myarray);

addRows(myarray);

highest(myarray);

return 0;

}

Step-by-step explanation:

The getNumberr function begins here

void getNumberr(int myarray[][5]){

This declares a new array

int arr[5][5];

The following iteration gets the array into the new array

for(int i = 0;i<5;i++){

for(int j = 0;j<5;j++){

arr[i][j] = myarray[i][j]; } } }

The showNumber function begins here

void showNumberr(int myarray[][5]){

This iterates through the row of the array

for(int i = 0;i<5;i++){

This iterates through the columns

for(int j = 0;j<5;j++){

This prints each element of the array

cout<<myarray[i][j]<<" "; }

This prints a new line at the end of each row

cout<<endl; } }

The addRow function begins here

void addRows(int myarray[][5]){

This iterates through the row of the array

for(int i = 0;i<5;i++){

This initializes the sum of each row to 0

int rowsum = 0;

This iterates through the columns

for(int j = 0;j<5;j++){

This adds the elements of each row

rowsum+=myarray[i][j]; }

This prints the total of each row

cout<<"Total for row "<<i+1<<" is: "<<rowsum<<endl; } }

The highest function begins here

void highest(int myarray[][5]){

This initializes the maximum to 0

int max = 0;

This iterates through the row of the array

for(int i = 0;i<5;i++){

This iterates through the columns

for(int j = 0;j<5;j++){

This checks for the highest

if(myarray[i][j]>max){

This assigns the maximum to max

max = myarray[i][j];

} } }

This prints the max

cout<<"Highest: "<<max; }

The main method begins here where the array is initialized and each function is called

int main(){

int myarray[5][5] = {{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}};

getNumberr(myarray);

showNumberr(myarray);

addRows(myarray);

highest(myarray);

return 0;

}

User Hasin Hayder
by
4.5k points