Arrays Example Input 10 students scores Calculate the

  • Slides: 37
Download presentation
Arrays

Arrays

Example Input 10 students’ scores , Calculate the average score of all students, and

Example Input 10 students’ scores , Calculate the average score of all students, and print the scores that lower than average. s=0; for (i=1; i<=10; i++) { scanf(“%d”, &score); if(score<ave) s=s+score; printf(“%5 d”, score) } ; ave=s/10. 0; }

One dimension array 1. Declaration typename arrayname [const expression] E. g. int a[5]; for

One dimension array 1. Declaration typename arrayname [const expression] E. g. int a[5]; for (i=0; i<=4; i++) a[0] a[1] a[2] a[3] a[4] a[5] × 10 20 30 40 50 a[0]=10; a[1]=20; a[2]=30; a[3]=40; a[4]=50; a[i]=(i+1)*10; for (i=0; i<=4; i++) scanf("%d", &a[i]); for (i=0; i<=4; i++) printf("%dn", a[i]);

char c[26]; c[0] c[1] c[2]. . . c[24 ] c[25 ] 'A' 'B' 'C'

char c[26]; c[0] c[1] c[2]. . . c[24 ] c[25 ] 'A' 'B' 'C' 'D' : : float score[30]; score [0] score [1] score [2]. . . score[28] score[29] : :

2. Initialization int a[5]={10, 20, 30, 40, 50}; int b[5]={10}; int b[5]={10, 0, 0};

2. Initialization int a[5]={10, 20, 30, 40, 50}; int b[5]={10}; int b[5]={10, 0, 0}; int m[5]={0}; char c[26]={'a', '*', '&', 'D', '', '' }; char c[26]={'a', '*', '&', 'D', 0, 0, 0};

3. Example (1) Input 10 students’ scores , Calculate the average score of all

3. Example (1) Input 10 students’ scores , Calculate the average score of all students, and print the scores that lower than average. main() { float score[10], s=0. 0, ave; int i; for (i=0; i<=9; i++) {scanf("%f", &score[i]); s=s+score[i]; } ave=s/10; printf("The average is %5. 2 fn", ave); for(i=0; i<=9; i++) if (score[i]<ave) printf("%5. 0 f", score[i]); }

(2)Input a series of characters , count digits, white space and others.

(2)Input a series of characters , count digits, white space and others.

int ndigit[10]={0}, nwhite=0, nother=0; char c; ndigit[0] ndigit[1] ndigit[2] ndigit[3] ndigit[4] ndigit[5] ndigit[6] ndigit[7]

int ndigit[10]={0}, nwhite=0, nother=0; char c; ndigit[0] ndigit[1] ndigit[2] ndigit[3] ndigit[4] ndigit[5] ndigit[6] ndigit[7] ndigit[8] ndigit[9] ndigit[c-'0'] '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' c '0' -'0' '1' -'0' '2' -'0' '3' -'0' '4' -'0' '5' -'0' '6' -'0' '7' -'0' '8' -'0' '9' -'0' c -'0' '0'≤c ≤ '9' ++digit[c-'0'] ==0 c∈'n', 't', ' ' ==0 nwhite++ nother++

#include <stdio. h> main() { int c, i, nwhite=0, nother=0; int ndigit[10]={0}; while ((c

#include <stdio. h> main() { int c, i, nwhite=0, nother=0; int ndigit[10]={0}; while ((c = getchar()) != EOF) if (c >= '0' && c <= '9') ++ndigit[c-'0']; else if (c == ' ' || c == 'n' || c == 't') ++nwhite; else ++nother; for (i = 0; i < 10; i ++) printf("the numbers of %d is %dn ", i , ndigit[i]); printf(" white space is %d, others is %dn", nwhite, nother); }

(3) Give the order of n numbers from small to big. n=6 8 5

(3) Give the order of n numbers from small to big. n=6 8 5 8 10 5 8 5 10 8 5 7 7 3 1 10 3 10 1 3 1 10 8 5 7 3 1 5 8 7 3 1 5 7 8 3 1 5 7 3 8 1 a[0] 10 a[1] a[2] a[3] a[4] a[5] a[0] a[1] a[2] a[3] a[4] 5 7 3 1 8 1 2

a[0] a[1] a[2] a[3] a[0] a[1] a[2] a[0] a[1] 5 7 3 1 5

a[0] a[1] a[2] a[3] a[0] a[1] a[2] a[0] a[1] 5 7 3 1 5 3 1 3 5 1 3 1 1 3 5 3 7 1 5 3 1 7 3 1 5 3 4 5

int i, j, temp; for (i=1; i<=n-1; i++) { Bubble: n-1 for(j=0; j<n-i; j++)

int i, j, temp; for (i=1; i<=n-1; i++) { Bubble: n-1 for(j=0; j<n-i; j++) Every bubble compare times: 1 n-1 2 n-2 3 n-3 i n-i if (a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } }

(4)Write a program to print the maximum of ten integers. #define main()N 10 main()

(4)Write a program to print the maximum of ten integers. #define main()N 10 main() {int a[10], i, max; {int a[N], i, max; for(i=0; i<10; i++) for(i=0; i<N; i++) scanf("%d", &a[i]); max=a[0]; for(i=1; i<10; i++) for(i=1; i<N; i++) if (max<a[i]) max=a[i]; for(i=0; i<10; i++) for(i=0; i<N; i++) printf("%5 d", a[i]); printf("n. The maximum is: %dn", max); } }

(5) Exchange the elements of an array. a[0] a[1] a[2] a[3] a[4] a[5] a[6]

(5) Exchange the elements of an array. a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] 2 4 6 8 10 1 3 5 7 9 9 7 5 3 1 10 8 6 4 2 for(i=0; i<n/2; i++) { t=a[i]; a[i]=a[n-i-1]; a[n-i-1]=t; }

Select sort: n=5 i a[0] a[1] a[2] a[3] a[4] 0 2 1 4 2

Select sort: n=5 i a[0] a[1] a[2] a[3] a[4] 0 2 1 4 2 5 3 3 4 1 1~4 ① 1 4 5 3 2 1 2 5 3 4 43 2~4 ② 3~4 ③ 4~4 ④ 1 1 2 2 3 3 5 4 4 5 i i+1~n-1 pos 40 431 32

#define n 10 main() {int i, j, pos, t, a[n]; for(i=0; i<n; i++) scanf(“%d”,

#define n 10 main() {int i, j, pos, t, a[n]; for(i=0; i<n; i++) scanf(“%d”, &a[i]); for(i=0; i<n-1; i++) {pos=i; for(j=i+1; j<n; j++) if(a[pos]>a[j]) pos=j; if(pos!=i) {t=a[i]; a[i]=a[pos]; a[pos]=t; } } for(i=0; i<n; i++) printf(“%6 d”, a[i]); }

Exercise 1. Write a program to print YANG-Hui Triangle. 1 1 1 2 3

Exercise 1. Write a program to print YANG-Hui Triangle. 1 1 1 2 3 4 1 3 6 …… 1 4 1

One Dimension Array 1. Who can tell me when we will use one dimension

One Dimension Array 1. Who can tell me when we will use one dimension array in a C program? A set of data : their number is known (fixed), and their type is same. 2. How to get a one dimension array variable’s value from keyboard, and how to output it? Use loop statements input or output every elements.

Two Dimension Array 1. Declaration type name array name [const expr 1 ] [

Two Dimension Array 1. Declaration type name array name [const expr 1 ] [ const expr 2] E. g. int b[3][4]; b[0][0] b[0][1] b[0][2] b[0][3] b[1][0] b[1][1] b[1][2] b[1][3] b[2][0] b[2][1] b[2][2] b[2][3] 0 1 2 3 4 5 6 7 8 9 10 11

2. Initialization (1) int a[2][3]={{1, 2, 3}, {4, 5, 6}} int a[2][3]={1, 2, 3,

2. Initialization (1) int a[2][3]={{1, 2, 3}, {4, 5, 6}} int a[2][3]={1, 2, 3, 4, 5, 6} int a[ ][3]={1, 2, 3, 4, 5, 6} (2) int a[2][3]={1, 2, 3, 4} (3) int a[2][3]={{1, 2}, {1}}; int a[2][ ]={1, 2, 3, 4, 5, 6} × a[0][0] 1 1 1 a[0][1] 2 2 2 a[0][2] 3 3 0 a[1][0] 4 4 1 a[1][1] 5 0 0 a[1][2] 6 0 0 (1) (2) (3)

E. g. Calculate the sum of the diagonal elements in the 5× 5 square

E. g. Calculate the sum of the diagonal elements in the 5× 5 square matrix. main() {int m [5][5], i, j, sum=0; for(i=0; i<5; i++) for(j=0; j<5; j++) scanf("%d", &m [i][j]); for(i=0; i<5; i++) for(j=0; j<5; j++) if(i==j ||i+j==4) sum=sum+m [i][j]; 5 6 7 8 9 4 2 4 5 3 1 6 7 2 5 3 8 2 4 1 6 9 5 4 7 m[0][0] m[0][1] m[0][2] m[0][3] m[0][4] m[1][0] m[1][1] m[1][2] m[1][3] m[1][4] m[2][0] m[2][1] m[2][2] m[2][3] m[2][4] for(i=0; i<5; i++) m[3][0] m[3][1] m[3][2] m[3][3] m[3][4] { for(j=0; j<5; j++) printf("%5 d", m [i][j]); m[4][0] m[4][1] m[4][2] m[4][3] m[4][4] printf("n"); } printf("The sum of diagonal elements is %dn", sum); }

E. g. Calculate the sum of the outboard elements of 3× 4 matrix. 3

E. g. Calculate the sum of the outboard elements of 3× 4 matrix. 3 8 9 10 2 5 -3 5 7 0 -1 4 a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] #define m 3 #define n 4 main() { int i, j, sum=0; int a [m][n]; for(i=0; i<m; i++) for(j=0; j<n; j++) { scanf("%d", &a [i][j]); if (i==0||i==m-1||j==0||j==n-1) sum=sum+a [i][j]; } printf("The sum is %dn", sum); }

E. g. Input 6 students’ scores , every student has 5 scores , Calculate

E. g. Input 6 students’ scores , every student has 5 scores , Calculate the average score of every student. main() { int i, j; float s, ave[6]={0}, score[6][5]; for (i=0; i<6; i++) { s=0; for(j=0; j<5; j++) { scanf("%f", &score[i][j]); s=s+score [i][j]; } ave[i]=s/5; } for(i=0; i<6; i++) printf("No. %d average is %5. 2 fn", i+1, ave[i]); }

Exercise 1. Write a program to print YANG-Hui Triangle. 1 1 1 2 3

Exercise 1. Write a program to print YANG-Hui Triangle. 1 1 1 2 3 4 1 3 6 …… 1 4 1 2. Select the lowest element and its row number in each column.

Character arrays The array's elements are characters. E. g. char c[100]; Character string :

Character arrays The array's elements are characters. E. g. char c[100]; Character string : bracket the letter, digit and E. g. escape sequence with "". "China" , "BASIC", "a+b=c", "Li_Li", "39. 4", "%dn"

Character string can be stored in Character arrays. E. g. char c[20]={'C', 'h', 'i',

Character string can be stored in Character arrays. E. g. char c[20]={'C', 'h', 'i', 'n', 'a', ''}; char c[20]={"China"}; char c[20]="China";

Input a character string: (1)input characters one by one. char str[9]; int i; for

Input a character string: (1)input characters one by one. char str[9]; int i; for (i=0; i<8; i++) scanf(″%c″, &str[i]); str[i]=''; (2) input a character string end with space or return. scanf("%s", str); scanf(″%s%s″, str 1, str 2); Thank you↙ (3) input a character string end with return (#include <stdio. h>) gets(str);

Output a character string: (1)output characters one by one. char str[9]; int i; strlen(str)

Output a character string: (1)output characters one by one. char str[9]; int i; strlen(str) for (i=0; i<8; i++) printf("%c", str[i]); (2) output one or more than one character string end with ''. printf("%s", str); printf(”%s %s ”, str 1, str 2); (3)output a character string end with ''. (#include <stdio. h>) puts(str);

Character string function (#include <string. h>) (1)strcpy(s, ct) : copy string ct to string

Character string function (#include <string. h>) (1)strcpy(s, ct) : copy string ct to string s, including '' strcpy(str 1, "China") str 1="china"; × (2) strcat(s, ct) : concatenate string ct to end of string s char str 1[20], str 2[10]; comp↙ uter ↙ computer gets(str 1); gets(str 2); strcat(str 1, str 2); puts(str 1); str 1 'c' 'o' 'm' 'p' '' str 2 'u' 't' 'e' 'r' '' str 1 'c' 'o' 'm' 'p' 'u' 't' 'e' 'r' ''

(3) strcmp(cs, ct) : compare string cs to string ct, return <0 if cs<ct,

(3) strcmp(cs, ct) : compare string cs to string ct, return <0 if cs<ct, 0 if cs==ct, or >0 if cs>ct. input 5 strings, output the smallest string. #include <stdio. h> #include <string. h> main() { char str[10], temp[10]; int i; gets(temp); for (i=1; i<5; i++) { gets(str); if (strcmp(temp, str)>0) strcpy(temp, str); } printf(" the smallest string is: %sn ", temp); }

(4) strlen(cs): return length of cs. #include <string. h> main() { char str[]=”How do

(4) strlen(cs): return length of cs. #include <string. h> main() { char str[]=”How do you do!”; printf(”%d”, strlen(str)); }

E. g. Write a program, print the length of an input text line. can't

E. g. Write a program, print the length of an input text line. can't use function strlen() s 'H' 'o' 'W' ' ' 'd' 'o' ' ' 'y' 'o' 'u' '' 'd' 'o' '!' '' s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9] s[10] s[11] s[12] s[13] s[14] How do you do! ↙ #include <stdio. h> main() { char s [81]; int i=0; gets(s); while (s[i]!= ′ ′) i++; printf(”The length is %d. ”, i); }

E. g. write a program, copy a string from variable s to variable t.

E. g. write a program, copy a string from variable s to variable t. s 'c' 'o' 'm' 'p' 'u' 't' 'e' 'r' '' (Can not use function strcpy()) t 'c' 'o' 'm' 'p' 'u' 't' 'e' 'r' '' #include <stdio. h> main() { char s [81], t [81]; int i=0; gets(s); while (s [i]!= ′ ′) t [i++]=s[i] {t[i]=s [i]; i++; } t[i]= ′ ′; printf(”string s is : %sn”, s); printf(”string t is : %sn”, t); }

#include <stdio. h> main() { char s [81], t [81]; int i=0; gets(s );

#include <stdio. h> main() { char s [81], t [81]; int i=0; gets(s ); while (( t [i]=s [i] )!= ′ ′) i++; printf("string s is : %sn", s); printf("string t is : %sn", t); }

E. g. Write a program that reads a set of text lines and prints

E. g. Write a program that reads a set of text lines and prints the longest. while (there's another line) if (it's longer than the previous longest) (save its length) print longest line

#include <stdio. h> #include <string. h> main() { char str[81], longest[81]; int max=0, len;

#include <stdio. h> #include <string. h> main() { char str[81], longest[81]; int max=0, len; gets(str); len=strlen(str); while (len!=0) {if(len>max) { strcpy(longest, str); max=len; } gets(str); len=strlen(str); } printf("The longest string is : %sn", longest); printf("It's length is : %dn", max); }

Exercise • 1. write a program that reverses the character strings. • 2. write

Exercise • 1. write a program that reverses the character strings. • 2. write a program that connects two character strings. • 3. Write a program to remove trailing blanks and tabs from a character string.