204k views
2 votes
(20 points). Some matrixes have very special patterns, for example, in the following matrix, the numbers in the first row are equal to the number of their respective column; the numbers in the first column are equal to the square of the number of their respective row; when the row number equals to the column number, the elements are equal to 1; the rest elements are the sum of the element just above them and the one to their left. Write a user-defined function program in the program, you need to use while loops), and then use the function you write to create the following matrix (show the command you use). 1 4 9 16 2 1 10 26 3 4 1 27 4 5 8 13 9 22 1 23

1 Answer

2 votes

Answer:

#include <iostream>

using namespace std;

void matrix(){

int row = 5, col = 6;

int myarr[row][col];

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

for (int x = 0; x < 6; x++){

if (i == 0){

myarr[i][x] = (x+1)*(x+1);

}else if ( x == 0){

myarr[i][x] = (i+1)*(i+1)*(i+1);

} else if ( i == x){

myarr[i][x] = (i+1);

} else{

myarr[i][x] = myarr[i-1][x] + myarr[i][x-1];

}

}

}

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

for (int x = 0; x < 6; x++){

cout<< myarr[i][x] << " ";

}

cout<< "\\";

}

}

int main(){

matrix();

}

Step-by-step explanation:

The C++ source code defines a two-dimensional array that has a fixed row and column length. The array is a local variable of the function "matrix" and the void function is called in the main program to output the items of the array.

User Ericbrownaustin
by
7.9k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.