C language programming exercise reference answer chapter 4 (2) Two-dimensional array

Source: Internet
Author: User

/* The maximum absolute value of each line in the 4.16 5*5 matrix, which is exchanged with the diagonal lines of the same row */

# Include "stdio. H"

# Include "math. H"
Void main ()
{
Int A [5] [5] = {1, 3, 4,-5}, {3, 5,-2, 2}, {4, 1, 2, 2 },
{,-, 6}, {, 4 }};
Int I, K, Max, sub, temp;
/* I loop variable, control row, K loop variable, control column, Max current maximum absolute value, sub current maximum absolute value element subscript
Temp temporary variable for exchange */

Printf ("output \ n");/* output */
For (I = 0; I <= 4; I ++)
{
For (k = 0; k <= 4; k ++)
Printf ("% 4D", a [I] [k]);
Printf ("\ n ");
}
/* Switch */
For (I = 0; I <= 4; I ++)
{
/* Assume that the first element is the largest */
Max = FABS (A [I] [0]); sub = 0;
/* Find the element with the largest absolute value and write down the subscript */
For (k = 1; k <= 4; k ++)
{
If (FABS (A [I] [k])> MAX)
{
Max = FABS (A [I] [k]); sub = K;
}
}
/* Switch */
Temp = A [I] [I]; A [I] [I] = A [I] [sub]; A [I] [sub] = temp;
}
/* Output after switching */
Printf ("output \ n" after switching ");
For (I = 0; I <= 4; I ++)
{
For (k = 0; k <= 4; k ++)
Printf ("% 4D", a [I] [k]);
Printf ("\ n ");
}

}

/* 4.17 store any four numbers in a one-dimensional array, such as 5, 1, 8, and 6, and generate the following matrix
5 5 5 5 5 5 5
5 1 1 1 1 5
5 1 8 8 8 1 5
5 1 8 6 8 1 5
5 1 8 8 8 1 5
5 1 1 1 1 5
5 5 5 5 5 5 5
*/
# Include "stdio. H"
# Include "conio. H"
Void main ()
{
Int fournumbers [4], array [7] [7], I, row, column;
Printf ("Enter 4 integers \ n ");
Scanf ("% d", & fournumbers [0], & fournumbers [1], & fournumbers [2], & fournumbers [3]);
For (I = 0; I <= 3; I ++)
{
For (ROW = I; row <= 6-i; row ++)
{
For (column = I; column <= 6-i; column ++)
Array [row] [column] = fournumbers [I];
}
}
/* Output matrix */
For (ROW = 0; row <= 6; row ++)
{
For (column = 0; column <= 6; column ++)
Printf ("% 4D", array [row] [column]);
Printf ("\ n ");
}
Getch ();
}
/* Exercise 4.19 encrypts a line of message. Each letter is converted to the third letter in the alphabet that shifts right, a-d, B-E ,...... z-C */

# Include "stdio. H"
Void main ()
{

Int C;
While (C = getchar ())! = '\ N ')
{
If (C> = 'A' & C <= 'Z') | (C> = 'A' & C <= 'Z '))
{
C = C + 3;
If (C> 'Z' & C <= 'Z' + 3) | C> 'Z ')
C = c-26;

}
Putchar (C );

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.