C language (? Two-dimensional array, string array, multidimensional array) class notes plus homework

Source: Internet
Author: User
Tags strcmp

//

Main.m

1-19 Course Notes

Two-dimensional arrays, string arrays, multidimensional arrays

Lecturer: Xiao Hui

Author: Wang Xuewen

Created by Lanouhn on 15/1/19.

Copyright (c) 2015 Lanouhn. All rights reserved.

//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {

/*

One-dimensional arrays

int a[10] = {1, 2, 3, 4, 5};

Two-dimensional array, one dimension more than one-dimensional array (one more element subscript)

int b[2][3] = {1, 2, 3, 4, 5, 6};

Definition of a two-dimensional array

Format:

Data type array name [number of rows] [number of columns] = {value 1, value 2, ...} ;

Number of elements in a two-dimensional array = number of rows * columns;

Other forms of definition

1. Can be omitted when assigning value, the default value is 0

int array1[2][3] = {1, 2, 3};

2. The number of rows can be omitted, the number of elements and the number of columns is determined;

int array2[][3] = {1, 2, 3, 4};

The number of columns cannot be omitted

int array3[2][] = {1, 2, 3, 4, 5}; Error

int array4[][] = {1, 2, 3, 4}; Error

Use of two-bit arrays

Gets the format of the element

Array name [number of rows] [number of columns]

Note: The number of rows and columns starts at 0

int array[3][2] = {1, 2, 3, 4, 5, 6};

Get element

ARRAY[0][0];

ARRAY[0][1];

ARRAY[1][0];

ARRAY[1][1];

ARRAY[2][0];

ARRAY[2][1];

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

for (int j = 0; J < 2; J + +) {

printf ("array[%d][%d] =%d\n", I, J, Array[i][j]);

}

}

Assigning a value to an element of an array

Define an array of 5 rows and 6 columns, assign values to each array randomly, take a range [55,88], and find the location of the maximum and minimum values

Clear Cache Command + SHIFT + K;

int a[5][6] = {0};

int max = 0, min = 89;

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

for (int j = 0; J < 6; J + +) {

A[I][J] = arc4random ()% 34 + 55;

printf ("a[%d][%d] =%d", I, J, A[i][j]);

if (Max < a[i][j]) {

max = A[i][j];

}

if (min > A[i][j]) {

min = A[i][j];

}

}

}

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

for (int j = 0; J < 6; J + +) {

if (max = = A[i][j]) {

printf ("\ n max a[%d][%d] =%d\n", I, J, A[i][j]);

}

if (min = = A[i][j]) {

printf ("min a[%d][%d] =%d\n", I, J, A[i][j]);

}

}

}

int a[3][4] = {0};

int b[4][3] = {0};

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

for (int j = 0; J < 4; J + +) {

A[I][J] = arc4random ()% 11 + 50;

printf ("a[%d][%d] =%d", I, J, A[i][j]);

} printf ("\ n");

}

printf ("After Exchange: \ n");

for (int j = 0; J < 4; J + +) {

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

B[j][i] = A[i][j];

printf ("b[%d][%d] =%d", j, I, B[j][i]);

}printf ("\ n");

}

A character array in which the elements of the array contents are characters (char)

Array of strings: elements stored in an array are strings

Character array, also called string

Array of strings: The elements in the array are character arrays, array arrays, so the string array is a two-dimensional array

Character array

Char string1[6] = {' A ', ' P ', ' P ', ' l ', ' e ', ' n '};

String

Char string2[6] = "Apple";

Array of strings

Char String3[3][7] = {"Apple", "Xiaomi", "Nokia"};

Output string array

printf ("%s\n", string1);

1. Character output

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

for (int j = 0; J < 7 && string3[i][j]! = ' + '; j + +) {

printf ("%c", String3[i][j]);

}printf ("\ n");

}

printf ("%s\n", string2);

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

printf ("%s\n", String3[i]);

}

Create an array to hold the names of the students around you and find out whose names are the longest.

Char a[4][20] = {"Weizijian", "Wanhao", "Yaochengzheng", "Wangxuewen"};

unsigned long max = 0;

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

if (Max < strlen (A[i])) {

max = strlen (A[i]);

}

}

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

if (max = = strlen (A[i])) {

printf ("Longest name:%s\n", A[i]);

}

}

Input string

char c = 0;

scanf ("%c", &c);

Input string

Char m[10] = {0};

scanf ("%s", m); No need to add &

Input string array

Char k[3][10] = {0};

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

scanf ("%s", K[i]);

}

scanf ("%s%s%s", K[0], k[1], k[2]);

Sort the names of the students around them

Char name[][20] = {"Wanhao", "Weizijian", "Wangxuewen", "Yaochengzheng"};

for (int i = 0; i < 4-1; i++) {

for (int j = 0; J < 4-1-I; j + +) {

if (strcmp (Name[i], name[i+1]) > 0) {

Char a[20] = {0};

strcpy (A, name[i]);

strcpy (Name[i], name[i + 1]);

strcpy (Name[i + 1], a);

}

}

}

Output results

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

printf ("%s\n", Name[i]);

}

Bubble sort

int array[5] = {12, 33, 22, 1, 20};

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

for (int j = 0; J < 5-1-I; j + +) {

if (Array[j] > array[j + 1]) {

int temp = Array[j];

ARRAY[J] = array[j + 1];

Array[j + 1] = temp;

}

}

}

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

printf ("%d\n", Array[i]);

}

*/

1. (* *) There is a two-dimensional array of 3 rows and 4 columns, which requires programming to find the largest element and output the row and column in which it resides.

int a[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 5, 4, 8, 3};

int max = 0;

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

for (int j = 0; J < 4; J + +) {

if (Max < a[i][j]) {

max = A[i][j];

//            }

//        }

//    }

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

for (int j = 0; J < 4; J + +) {

if (max = = A[i][j]) {

printf ("Maximum value in%d rows%d \ n", i + 1, j+ 1);

//            }

//        }

//    }

2, (* *) enter 6 strings and output them in order from small to large.

Char a[6][20] = {0};

printf ("Please enter 6 strings:");

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

scanf ("%s", A[i]);

//    }

for (int i = 0; i < 6-1; i++) {

for (int j = 0; J < 6-1-I; j + +) {

if (strcmp (A[j], a[j + 1])) {

Char b[20] = {0};

strcpy (b, a[j]);

strcpy (A[j], a[j + 1]);

strcpy (A[j + 1], b);

//            }

//        }

//    }

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

printf ("%s\n", A[i]);

//    }

3, (* *) randomly generated a three-dimensional array, the average of programming depth, stored in a two-dimensional array (equivalent to a Rubik's Cube from the above).

int a[4][5][6] = {0};

Float B[4][5] = {0};

//

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

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

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

A[i][j][k] = arc4random ()% 20;

printf ("a[%d][%d][%d] =%3d", I, J, K, A[i][j][k]);

}printf ("\ n");

//        }

//    }

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

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

float sum = 0.0;

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

sum = sum + a[i][j][k];

B[J][K] = sum;

//            }

printf ("b[%d][%d] =%5.2f", J, K, B[j][k]/4);

}printf ("\ n");

//    }

return 0;

}

C language (? Two-dimensional array, string array, multidimensional array) class notes plus homework

Related Article

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.