Experimental task 1
#include <> #define N 4 #define M 2 void test1() { int x[N] = {1, 9, 8, 4}; int i; printf("sizeof(x) = %d\n", sizeof(x)); for (i = 0; i < N; ++i) printf("%p: %d\n", &x[i], x[i]); printf("x = %p\n", x); } void test2() { int x[M][N] = {{1, 9, 8, 4}, {2, 0, 4, 9}}; int i, j; printf("sizeof(x) = %d\n", sizeof(x)); for (i = 0; i < M; ++i) for (j = 0; j < N; ++j) printf("%p: %d\n", &x[i][j], x[i][j]); printf("\n"); printf("x = %p\n", x); printf("x[0] = %p\n", x[0]); printf("x[1] = %p\n", x[1]); printf("\n"); } int main() { printf("Test 1: One-dimensional array of type int \n"); test1(); printf("\nTest 2: Two-dimensional array of type int \n"); test2(); return 0; }
Question 1: A one-dimensional array x is stored continuously in memory. The array name x corresponds to the same value as &x[0].
Question 2: The int-type two-dimensional array x is stored in memory "consecutively in rows". x[0], & x[0][0] are literally the same value. x[0] and x[1] are different by 16. This means that one row occupies the length of memory.
Experimental Task 2
#include <> #define N 100 void input(int x[], int n); double compute(int x[], int n); int main() { int x[N]; int n, i; double ans; while(printf("Enter n: "), scanf("%d", &n) != EOF) { input(x, n); ans = compute(x, n); printf("ans = %.2f\n\n", ans); } return 0; } void input(int x[], int n) { int i; for(i = 0; i < n; ++i) scanf("%d", &x[i]); } double compute(int x[], int n) { int i, high, low; double ans; high = low = x[0]; ans = 0; for(i = 0; i < n; ++i) { ans += x[i]; if(x[i] > high) high = x[i]; else if(x[i] < low) low = x[i]; } ans = (ans - high - low)/(n-2); return ans; }
One-dimensional array x Formal reference: int x[]. Be a real parameter:x
The input function is to input an array, and the compute function is to average the elements of the array after removing the maximum and minimum values.
Experimental Task 3
#include <> #define N 100 void output(int x[][N], int n); void init(int x[][N], int n, int value); int main() { int x[N][N]; int n, value; while(printf("Enter n and value: "), scanf("%d%d", &n, &value) != EOF) { init(x, n, value); output(x, n); printf("\n"); } return 0; } void output(int x[][N], int n) { int i, j; for(i = 0; i < n; ++i) { for(j = 0; j < n; ++j) printf("%d ", x[i][j]); printf("\n"); } } void init(int x[][N], int n, int value) { int i, j; for(i = 0; i < n; ++i) for(j = 0; j < n; ++j) x[i][j] = value; }
Two-dimensional array x Formal reference: int x[]. [] Be a real parameter: x
Issue 1 Second dimension array size cannot be omitted
Question 2 input input array, output output array
Experimental Task 4
#include <> #define N 100 int input(int x[],int n); double median(int x[],int n); int main(){ int x[N]; int n; double ans; while (printf("Enter n:"),scanf("%d",&n)!=EOF){ input(x,n); ans =median(x,n); printf("ans=%g\n\n",ans); } return 0; } int input(int x[],int n){ int i; for (i=0;i<n;i++){ scanf("%d",&x[i]); } } double median(int x[],int n){ int i,t,j; for(j=0;j<n-1;j++){ for(i=0;i<n-1;i++){ if(x[i]>x[i+1]){ t=x[i+1]; x[i+1]=x[i]; x[i]=t; } } } if(n%2){return x[n/2];; } else return (x[n/2-1]+x[n/2])/2.0; }
Experimental Task 5
#include <> #define N 100 void input(int x[][N], int n); void output(int x[][N], int n); void rotate_to_right(int x[][N],int n); int main() { int x[N][N]; int n; printf("Enter n."); scanf("%d", &n); input(x, n); printf("Original matrix:\n"); output(x, n); rotate_to_right(x,n); printf("Transformed matrix:\n"); output(x, n); return 0; } void input(int x[][N], int n) { int i, j; for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) scanf("%d", &x[i][j]); } } void output(int x[][N], int n) { int i, j; for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) printf("%4d", x[i][j]); printf("\n"); } } void rotate_to_right(int x[][N],int n){ int i,j,t; for (i=0;i<n;i++){ t=x[i][n-1]; for(j=n-1;j>=0;j--){ if(j>0){ x[i][j]=x[i][j-1]; } else{ x[i][j]=t; } } } }
Experimental Task 6
#include <> #define N 100 void dec_to_n(int x, int n); int main() { int x; while(printf("Enter a decimal integer."), scanf("%d", &x) != EOF) { dec_to_n(x, 2); // Function Call: Convert x to binary output. dec_to_n(x, 8); // Function Call: Convert x to octal output. dec_to_n(x, 16); // Function Call: Convert x to hexadecimal output. printf("\n"); } return 0; } void dec_to_n(int x, int n){ int a[N]; int i=0; while(x!=0){ a[i]=x%n; i++; x/=n; } for(;i>0;i--){ switch(a[i-1]){ case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: printf("%d",a[i-1]);break; case 10:printf("A");break; case 11:printf("B");break; case 12:printf("C");break; case 13:printf("D");break; case 14:printf("E");break; case 15:printf("F");break; default:printf("?");break;} } printf("\n"); }
Experimental Task 7
#include <> #define N 100 void input(int x[][N], int n); void output(int x[][N], int n); int is_magic(int x[][N], int n); int main() { int x[N][N]; int n; while(printf("Enter n."), scanf("%d", &n) != EOF) { printf("Input Square:\n"); input(x, n); printf("Output square:\n"); output(x, n); if(is_magic(x, n)) printf("is the Rubik's cube matrix \n\n"); else printf("Not a Rubik's cube matrix \n\n");} return 0;} void input(int x[][N], int n) { int i, j; for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) scanf("%d", &x[i][j]);}} void output(int x[][N], int n) { int i, j; for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) printf("%4d", x[i][j]); printf("\n");}} int is_magic(int x[][N], int n){ int j,i,t,k=0,sum=0,sum1=0,sum2=0; int a[N*N]; for(t=1;t<=n*n;t++){ for(i=0;i<n;i++){ for(j=0;j<n;j++){ if(x[i][j]==t) break;}}} if(t<=n*n) return 0;//The check element consists of 1 through n*n for(i=0;i<n;i++){ for(j=0;j<n;j++){ sum+=x[i][j]; } a[k]=sum;sum=0; k++;} for(k=0;k<n-1;k++){ if(a[k]!=a[k+1]) return 0;}//Checking rows and equality k=0; for(j=0;j<n;j++){ for(i=0;i<n;i++){ sum+=x[i][j]; } a[k]=sum;sum=0; k++;} for(k=0;k<n-1;k++){ if(a[k]!=a[k+1]) return 0;}//Checking column sums for equality for(i=0;i<n;i++){ sum1+=x[i][i];} for(i=n-1;i>=0;i--){ sum2+=x[i][i]; } if(sum2!=sum1) return 0;//Check that the sum of the diagonals is equal return 1;}