Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - C language: write a program to find the sum of diagonal elements of an n×n matrix.
C language: write a program to find the sum of diagonal elements of an n×n matrix.
Code:

# Contains &; iostream & ampgt;

Use namespace std

int main()

{

int sum = 0;

int n = 0;

Int * * array// Pointer to integer pointer.

cout & amplt; & amplt; Please enter the dimension of the matrix: "&; lt; & amplt; endl

CIN & amp; gt; & ampgt; n;

array = new int *[n]; //An array for storing pointers.

for(int k = 0; k & amplt; n; k++)

{

array[k]= new int[n]; //Point to an integer array.

}

cout & amplt; & amplt; Please enter n*n values:'&; lt; & amplt; endl

for(int I = 0; I & n; i++)

{

for(int j = 0; Johnson & Johnson company lt; n; j++)

{

CIN & amp; gt; & ampgt; Arrays and arrays. lt; I > [j];

cout & amplt; & amplt; Arrays and arrays. lt; I > [j]& amp; lt; & amplt; "";

}

cout & amplt; & amplt; endl

}

for(int I = 0; I & n; i++)

Arrays and. lt; I > & amplt; I > ;

cout & amplt; & amplt; "The sum of two diagonal elements is:"&; lt; & amplt; Sum and. lt; & amplt; endl

System ("suspended");

Returns 0;

}

Extended data:

Function Name: System

Function: Issue DOS command.

Usage: int system (char * command);

Program example:

# Contains &; & ampgt;

# Contains &; stdio.h & ampgt;

int main(void)

{

Printf ("DOS command will be generated and run soon \ n");

system(" dir ");

Returns 0;

}

For example, system ("pause") can freeze the screen, which is convenient for observing the execution results of the program; The system ("CLS") can clear the screen. Calling the color function can change the foreground and background of the console. The specific parameters are explained as follows.

For example, use system ("Color 0A"); Where 0 after the color is the background color code and A is the foreground color code. The color codes are as follows:

0= black 1= blue 2= green 3= lake blue 4= red 5= purple 6= yellow 7= white 8= gray 9= light blue A= light green B= light green C= light red D= light purple E= light yellow F= bright white.

(Note: Microsoft Visual C++6.0 supports the system)

The color attribute is specified by two hexadecimal digits-the first corresponding to the background and the second corresponding to the foreground. Every number

Can be any of the following values:

0= black 8= gray

1= blue 9= light blue

2= green A= light green

3= light green B= light green

4= red C= light red

5= purple D= lavender

6= yellow E= light yellow

7= white F= bright white

References:

Baidu Encyclopedia-System (Function)