Experience the fun of pair development (5) -- (the question of finding the largest sub-array and the question of the Two-dimensional array connected at the beginning and end), and pairing two-dimensional arrays
Peer partner: Xin 1-2 -2 class Gao Yang, Xin 1-1 -1 class Han xuedong
I. Question requirements and design ideas
Requirement: On the basis of the previous step, the array is changed to a two-dimensional array, followed by a link at the beginning and end to form a chain, and then the greatest child array is summed;
Design Philosophy: the problem of first-and-tail connection of one-dimensional arrays has been solved, and the problem of the Child array with the largest sum of two arrays has also been solved, that is, the two program ideas are integrated, then, the class project was completed in just a short time.
Ii. Source Code
1 // erweishuzuqiuzuida. cpp: Defines the entry point for the console application. 2 // Gao Yang Han Xue Dong 3 4 // # include "stdafx. h "5 # include" fstream. h "6 # include" iostream. h "7 # include" stdio. h "8 9 # define MAXSIZE 50 10 11 12 void read (int array [] [MAXSIZE], int & len1, int & len2) // read File Information, and write the array 13 {14 ifstream infile ("array.txt"); 15 if (! Infile) 16 cout <"failed to read! "<Endl; 17 else 18 {19 infile> len1> len2; 20 for (int I = 0; I <len1; I ++) 21 {22 for (int j = 0; j <len2; j ++) 23 {24 infile> array [I] [j]; 25} 26} 27} 28} 29 void display (int array [] [MAXSIZE], int len1, int len2, int size1, int size2) // display the array information 30 {31 for (int I = len1; I <= size1; I ++) 32 {33 for (int j = len2; j <= size2; j ++) 34 {35 cout <array [I] [j] <"\ t"; 36} 37 cout <endl; 38} 39} 40 int * shuchu (int m [], int szcdx, int xhy) // m [] indicates the array to be tested, and szchx indicates the array length, xhy indicates the cyclic condition 41 {42 int t, p; 43 int max, sum; 44 // The cached array is assigned a value of 45 int c [10000]; 46 int v [10000]; 47 int o = 2 * szcdx; 48 int * temp = new int [o]; 49 50 for (t = szcdx-xhy-1; t <szcdx; t ++) 51 {52 c [t-szcdx + xhy + 1] = m [t]; 53} 54 // cycle 55 for (t = xhy; t> = 0; t --) 56 {57 sum = 0; 58 for (p = 0; p <= t; p ++) 59 {60 sum = sum + c [p]; 61} 62 v [t] = sum; 63} 64 // The maximum cyclic output value is 65 max = v [0]; 66 for (t = 0; t <xhy + 1; t ++) 67 {68 if (max <= v [t]) 69 {70 max = v [t]; 71} 72 // printf ("% d ", v [t]); 73 temp [t] = v [t]; 74} 75 return temp; 76} 77 int maxs (int s [], int length) // output maximum 78 {79 int d = s [0]; 80 for (int f = 0; f <length; f ++) 81 {82 if (d <= s [f]) 83 {84 d = s [f]; 85} 86} 87 return d; 88} 89 int main (int argc, char * argv []) 90 {91 int len1, len2; // len1 is the number of rows, len2 is the number of columns 92 93 int x [3]; 94 int y [3]; 95 int * k; 96 int * l; 97 int array [MAXSIZE] [MAXSIZE]; 98 read (array, len1, len2); 99 cout <"matrix: "<endl; 100 display (array, 101, len1-1, len2-1); 102 103 104 for (int I = 0; I <3; I ++) 105 {106 x [I] = array [0] [I]; 107 108} 109 int e = 3; 110 int w [6]; 111 int u [6]; 112 int q = 0; 113 printf ("the sum of the first sub-array of the array:"); 114 for (I = 2; I> = 0; I --) 115 {116 117 k = shuchu (x, 3, I); 118 for (int r = 0; r <e; r ++) 119 {120 121 w [q] = k [r]; 122 printf ("% d", w [q]); 123 q ++; 124} 125 e --; 126} 127 for (int j = 0; j <3; j ++) 128 {129 y [j] = array [1] [j]; 130} 131 printf ("\ n"); 132 e = 3; 133 q = 0; 134 printf ("the sum of the Child arrays in the second row of the array :"); 135 for (I = 2; I> = 0; I --) 136 {137 138 l = shuchu (y, 3, I); 139 for (int r = 0; r <e; r ++) 140 {141 142 u [q] = l [r]; 143 printf ("% d", u [q]); 144 q ++; 145} 146 e --; 147} 148 149 150 printf ("\ n"); 151 int h [6]; 152 printf ("the array contains the child arrays of two rows and:"); 153 for (int m = 0; m <6; m ++) 154 {155 h [m] = w [m] + u [m]; 156 printf ("% d", h [m]); 157} 158 159 160 int k1 = maxs (w, 6); 161 int k2 = maxs (u, 6); 162 int k3 = maxs (h, 6 ); 163 164 int maxx = k1; 165 if (maxx <= k2) 166 {167 maxx = k2; 168} 169 if (maxx <= k3) 170 {171 maxx = k3; 172} 173 printf ("\ n Max and % d \ n", maxx); 174 175 176 177 178 int r1; 179 int p1; 180 for (int t = 0; t <2; t ++) 181 {182 p1 = array [0] [0]; 183 r1 = array [1] [0]; 184 for (q = 0; q <2; q ++) 185 {186 187 array [0] [q] = array [0] [q + 1]; 188 array [1] [q] = array [1] [q + 1]; 189 190} 191 array [0] [2] = p1; 192 array [1] [2] = r1; 193 for (int I = 0; I <3; I ++) 194 {195 x [I] = array [0] [I]; 196 197} 198 int e = 3; 199 int w [6]; 200 int u [6]; 201 int q = 0; 202 printf ("the sum of the first sub-array of the array:"); 203 for (I = 2; I> = 0; I --) 204 {205 206 k = shuchu (x, 3, I); 207 for (int r = 0; r <e; r ++) 208 {209 210 w [q] = k [r]; 211 printf ("% d", w [q]); 212 q ++; 213} 214 e --; 215} 216 for (int j = 0; j <3; j ++) 217 {218 y [j] = array [1] [j]; 219} 220 printf ("\ n"); 221 e = 3; 222 q = 0; 223 printf ("the sum of the Child arrays in the second row of the array :"); 224 for (I = 2; I> = 0; I --) 225 {226 227 l = shuchu (y, 3, I); 228 for (int r = 0; r <e; r ++) 229 {230 231 u [q] = l [r]; 232 printf ("% d", u [q]); 233 q ++; 234} 235 e --; 236} 237 238 239 printf ("\ n"); 240 int h [6]; 241 printf ("the array contains the child arrays of two rows and:"); 242 for (int m = 0; m <6; m ++) 243 {244 h [m] = w [m] + u [m]; 245 printf ("% d", h [m]); 246} 247 248 249 int k1 = maxs (w, 6); 250 int k2 = maxs (u, 6); 251 int k3 = maxs (h, 6 ); 252 253 int maxx = k1; 254 if (maxx <= k2) 255 {256 maxx = k2; 257} 258 if (maxx <= k3) 259 {260 maxx = k3; 261} 262 printf ("\ n Max and % d \ n", maxx); 263} 264 265 return 0; 266 267 268}
Iii. Running results
Iv. Experiences
After reading the dream code, there is a saying in this book: "When you put a new part of the system, I always want to see if I will regret it if I get it for 10 years in the next five years-whether you can expand it or replace it. "As the problem goes deeper, I deeply understand the importance of the Foundation, I realized how easy the code scalability will bring to future modifications. The other is the question of 1 + 1> 2. pairing development brings together two people's ideas, both in programming efficiency and in thinking design, faster than one person, I hope that this will happen during team development.
5. Image and truth