Experiment 6: how to create and use arrays 1. Understand the type definition of arrays in C language. 2. Master the features of array creation and use. Experiment content program 1 input two arbitrary integers in the form of strings on the computer and write a program to evaluate the product of the two integers. Program 2 if an element AIJ in matrix am 'n' is the minimum value of row I and the maximum value in column J, this element is called a saddle point in the matrix. Assume that the two-dimensional array is used to store the matrix am'n'. Write an algorithm to find all saddle points in the matrix.
1 # include <iostream> 2 # include <stdio. h> 3 using namespace STD; 4 # define maxn 1000 5 char a [maxn], B [maxn]; 6 struct bignum {7 int data [maxn/4]; 8 int dig; // number of digits, each with 4 digits 9}; 10 bignum char2big (char s []) // convert the number from Char to bignum 11 {12 INT Len = strlen (s), I; 13 bignum big; 14 big. dig = (len-1)/4 + 1; // number of digits of the converted int array 15 char * P = s; 16 for (I = 0; I <big. dig; I ++) {17 char T [5]; 18 if (I = 0) {// retrieves every four digits and puts them in the string 19 int TT = Len % 4 = 0? 4: Len % 4; 20 strncpy (T, P, TT); 21 t [TT] = '\ 0'; 22 p + = Len % 4; 23} 24 else {25 strncpy (T, P, 4); 26 p + = 4; 27} 28 sscanf (T, "% d", & big. data [I]); // read number 29} 30 return big; 31} 32 bignum multi (bignum AI, bignum Bi, bignum & CI) from t) // computing ai * bi33 {34 CI. dig = AI. dig + bi. dig; 35 int I, j, Pa = AI. dig, Pb = Bi. dig; 36 for (I = Bi. dig-1; I> = 0; I --) {// ai * bi37 for (j = AI. dig-1; j> = 0; j --) {38 int T = CI. dig-1-(Bi. dig-1-i)-(AI. dig-1-j); 39 int sum = AI. data [J] * bi. data [I] + ci. data [T]; 40 Ci. data [T] = sum % 10000; 41 CI. data [T-1] + = sum/10000; // carry 42} 43} 44 return ci; 45} 46 void printans (bignum CI) // output result 47 {48 int I; 49 for (I = 0; I <CI. dig; I ++) 50 if (CI. data [I]! = 0) 51 printf ("% d", CI. data [I]); 52 printf ("\ n"); 53} 54 int main () 55 {56 while (CIN> A> B) {57 bignum AI = char2big (a); // convert to bignum type 58 bignum Bi = char2big (B); 59 printans (AI); 60 printans (BI ); 61 bignum CI = {0}; 62 multi (AI, Bi, CI); 63 printans (CI); 64} 65 return 0; 66}
1 # include <iostream> 2 # include <stdio. h> 3 using namespace STD; 4 # define maxn 1010 5 # define Max 0x7fffffff 6 # define Min-0x7fffffffff 7 int A [maxn] [maxn]; 8 9 void input (int A [] [maxn], int M, int N) // input two-dimensional matrix 10 {11 int I, j; 12 for (I = 1; I <= m; I ++) 13 for (j = 1; j <= N; j ++) 14 scanf ("% d ", & A [I] [J]); 15} 16 17 void getmad (int A [] [maxn], int M, int N) // output all Saddle Points of the Two-dimensional matrix 18 {19 int I, j; 20 for (I = 1; I <= m; I ++) {21 int X, Y; // Saddle Point Location 22 int min = max, max = min; 23 for (j = 1; j <= N; j ++) // find the minimum value of this line 24 if (a [I] [J] <min) 25 min = A [I] [J], x = I, y = J; 26 // determine whether the maximum vertex of this column is 27 for (j = 1; j <= m; j ++) 28 if (a [J] [Y]> A [x] [Y]) 29 break; 30 if (j <= m) 31 continue; 32 else33 printf ("the element in column % d of row % d is saddle point \ n", x, y); 34} 35} 36 37 int main () 38 {39 int M, N; 40 scanf ("% d", & M, & N); // number of rows and columns in the input matrix 41 input (a, m, n); // input two-dimensional matrix 42 getmad (a, m, n); // output all Saddle Points of the Two-dimensional matrix 43 return 0; 44}
Create and use Arrays