C语言-数组作业

数组作业

    • 1.计算平均成绩
    • 2.Fibonacci List
    • 3.Mean&Median
    • 4.practice on chapter 6 Numeric Arrays Application exercises6.1-6.3
    • 5.杨辉三角
    • 5(Additional)
    • 6.寻找鞍点
    • 7.矩阵转置
      • Application program6.3 Arrays Matrix-vector multiplication
      • Numeric Arrays Application exercises6.7-6.10
    • 10.冒泡排序、交换最大排序、二进制搜索
    • 11.
    • 6.12
    • 6.13

这是一份数组作业的记录。

1.计算平均成绩

Enter the 100 student’s score for Data Structure and Algorithm course, then calculate the average score and display it.

#include
float function(int score[],int n)
{	
	int sum,i;
	double average;
	for(i=0;i<n;i++)
	{
		sum +=score[i];
	}
	average = sum/n;
	return average;
}

int main()
{
	int score[5];
	int x,i,n=5;
	FILE *fp;
	fp = fopen("6-1score.txt","r");
	for(i=0;i<n;i++){
		fscanf(fp,"%d",&score[i]);
	}
	/*
	printf("Please input 100 student's score for Data Structure and Algorithm course.\n");
	for(i=0;i
	float average = function(score,n);
	printf("The average score is %.2f.\n",average);
    fclose(fp);
 } 

C语言-数组作业_第1张图片

2.Fibonacci List

Calculate the value of the first thirty items (N=30) of Fibonacci list.
a1=a2=1 an=an-1+an-2
i.e.: 1,1,2,3,5,8,13

#include
#define N 30
int main()
{
 	int Fibonacci[N];
 	Fibonacci[0]=Fibonacci[1]=1;
 	int n;
 	for(n=3;n<N+1;n++)
 	{
 		Fibonacci[n-1]=Fibonacci[n-3]+Fibonacci[n-2];
	}
 	for(n=0;n<N;n++)
 	{
 		printf("%d\n",Fibonacci[n]);
	 }
 } 

C语言-数组作业_第2张图片

3.Mean&Median

mean and median of Measured wave heights(numerical method)

#include
#define MAX_NUM_PTS 100

int main()
{
	int x[MAX_NUM_PTS],num_pts,i,j,count_lower,count_higher,median;
	double sum,mean;
	FILE *infile;
	
	infile = fopen("average.dat.txt","r");
	fscanf(infile,"%d",&num_pts);
	for(i=0;i<num_pts;i++) fscanf(infile,"%d",&x[i]);
	
	sum = 0.0;
	for(i=0;i<num_pts;i++) sum += x[i];
	mean = sum/(double)num_pts;
	
	i = -1;
	do{
		j++;
		count_lower = 0;
		count_higher = 0;
		for(i=0;i<num_pts;i++){
			if (x[j]<=x[i]) 	count_higher++;
			if (x[j]>=x[i])		count_lower++;
		}
	}while(j<num_pts && (count_lower <= ((double)(num_pts)/2.)
			||count_higher <= ((double)(num_pts)/2.)));
	median = x[j];
	
	printf("The mean of the values is : %.3lf \n"
			"The meadian value is :      %d  \n",mean,median);
		
	
}

#include
    
void bubbleSort(int *a, int n) 
{
    int i,j,temp;
    for(j=0;j<n-1;j++) {
        for(i=0;i<n-j-1;i++) {
            if(a[i]>a[i+1]) {
                temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
            }
        }
    }
}
  
float median(int *a,int n)
{	
	int i,lem;
	float med;
	bubbleSort(a,n);
	
	if(n%2 == 0){//偶数 
		lem = n/2;
		med = (a[lem-1]+a[lem])/2;
	}else{//奇数 
		lem = (n+1)/2;//中间数的序号 
		med = a[lem-1];//下标-1 
	}
	return med;
}

float mean(int a[],int n)
{
	int sum=0,j;
	for (j=0;j<n;j++)
	{
		sum +=a[j];
	}
	float m = sum/n;
	return m;
}

int main()
{
	int a[100];
	int n=0;
	printf("Please input the measure of wave heights.\n");
	
	do{
		scanf("%d",&a[n]);
		n++;
	}while(getchar()!='\n');

	float m = mean(a,n);
	float med = median(a,n);
	printf("The mean and median of measured wave heights are %lf and %lf",m,med);
}

C语言-数组作业_第3张图片

4.practice on chapter 6 Numeric Arrays Application exercises6.1-6.3

#include
void display(int *mil_gal,int array_size)
{
	printf("Day no.                          Millions of gallons\n");
	for(int i=0;i<array_size;i++)
		printf("  %d                                   %d\n",i+1,mil_gal[i]);

	int rank[31];
	int x[20]={0};
	
	for(int i=0;i<array_size;i++){
		rank[i] = (mil_gal[i]/100%10)*10+mil_gal[i]/10%10;
		switch (rank[i]){
			case 8:
				x[8]+=1;
				break;
			case 9:
				x[9]+=1;
				break;
			case 10:
				x[10]+=1;
				break;
			case 11:
				x[11]+=1;
				break;
			case 12:
				x[12]+=1;
				break;
			case 13:
				x[13]+=1;
				break;
			case 14:
				x[14]+=1;
				break;
			case 15:
				x[15]+=1;
				break;
		}
	}
	printf("\nSewage per day                Frequency of occurence\n");
	for(int index=8;index<16;index++)
		printf("    %d1-%d0                            %d           \n",index,index+1,x[index]);
}


int get_data(int *sewage_amt)
{
	int array_size=0;
	FILE *fp;
	if((fp = fopen("EX6_1.DAT.txt", "r")) != NULL){
		while(getc(fp)!=EOF){
			fscanf(fp,"%d",&sewage_amt[array_size]);  
			array_size++;
		}
	}
	fclose(fp);
	return array_size;	
 } 

int main()
{
	int sewage_amt[100];
	int array_size = get_data(sewage_amt);
	display(sewage_amt,array_size);	
}

C语言-数组作业_第4张图片

5.杨辉三角

#include
#define N 10
 int main()
 {
 	int i,j,a[N][N];
 	for(i=0;i<N;i++){
 		a[i][i]=1;
 		a[i][0]=1;
 		if(i>=2){
 			for(j=1;j<i;j++){
 				a[i][j]=a[i-1][j-1]+a[i-1][j];
			}
		}
	}
	 
	for(i=0;i<N;i++){
		for(j=i;j<=N;j++){
			printf("  ");
		}
		for(j=0;j<=i;j++){
			printf("%5d",a[i][j]);
		}
		printf("\n");
	}
 }

C语言-数组作业_第5张图片

5(Additional)

C语言-数组作业_第6张图片

#include
#include
#define N 5
int main()
{
	int i;
	int j;
	int a[N][N];
	for(i=0;i<N;i++){ 
		for(j=0;j<N;j++){
			if(i<=j){
				a[i][j]=1;
			}else{
				a[i][j]=i-j+1;
			} 
		}
	}
	for(i=0;i<N;i++){
		for(j=0;j<N;j++){
			printf("%d ",a[i][j]);
		}printf("\n");
	}
	
	system("pause");
	return 0;
}

C语言-数组作业_第7张图片

6.寻找鞍点

Saddle Point Problems of 2D Arrays

#include

int main()
{
	//在矩阵中,一个数在所在行中是最大值,在所在列中是最小值,则被称为鞍点。
	int M,N;
	printf("请输入矩阵的行数和列数。\n");
	scanf("%d %d",&M,&N);
	printf("矩阵为%dx%d的矩阵。\n请依次输入矩阵的数值。\n",M,N);
	int a[100][100];
	printf("a = \n");
	for(int i=0;i<N;i++){
		for(int j=0;j<M;j++){
			scanf("%d",&a[i][j]);
		}
	}
	int count=0;
    for(int i=0;i<N;i++){
        int point=a[i][0];//固定行数 
		int x=i,y=0;
        for(int j=1;j<M;j++){
            if(a[i][j]>point){
                point=a[i][j];//找出该行最大数(i,y) 
                y=j;
            }
        }
        
        int flag=1;
        for(int k=0;k<N;k++){
            if(a[k][y]<point){//如果在第y列中有数比(i,y)小 
                flag=0;		  //则没有鞍点
                break;
            }
        }
        if(flag==1){
            count++;
            printf("该矩阵的鞍点为%d(%d,%d)\n",a[x][y],x,y);
        }
    }
    if(count==0){
        printf("该矩阵没有鞍点。\n");
    }

    return 0;
}

C语言-数组作业_第8张图片

7.矩阵转置

Get the reverse matrix (B) of matrix (A)

#include
#define M 2
#define N 3
int main()
{
	int i,j;
	int A[M][N]={{1,2,3},{4,5,6}};
	int B[N][M];
	
	for(i=0;i<M;i++){
		for(j=0;j<N;j++){
			B[j][i]=A[i][j];
		}
	}
	for(i=0;i<N;i++){
		for(j=0;j<M;j++){
			printf("%d ",B[i][j]);
		}
		printf("\n");
	} 
}

C语言-数组作业_第9张图片

Application program6.3 Arrays Matrix-vector multiplication

#include
#define MAX_NUM_ROWS 20
#define MAX_NUM_COLS 20
int main()
{
	int a[MAX_NUM_ROWS][MAX_NUM_COLS],x[MAX_NUM_COLS];
	int b[MAX_NUM_ROWS];
	int i,j,num_rows,num_cols;
	FILE *infile;
	
	infile=fopen("matvect.dat.txt","r");
	fscanf(infile,"%d %d",&num_rows,&num_cols);
	
	for(i=0;i<num_rows;i++){
		for(j=0;j<num_cols;j++){
			fscanf(infile,"%d",&a[i][j]);
		}fscanf(infile,"%d",&x[i]);
	}
	for(i=num_rows;i<num_cols;i++)
		fscanf(infile,"%d",&x[i]);
	printf("\nb vector");
	
	for(i=0;i<num_rows;i++){
		b[i]=0;
		for(j=0;j<num_cols;j++){
			b[i]+=a[i][j]*x[j];
		}
		printf("\n%d",b[i]);
	}
	
}

C语言-数组作业_第10张图片

Numeric Arrays Application exercises6.7-6.10

C语言-数组作业_第11张图片

#include
#include
int main() 

{   FILE *fpr;
	if((fpr=fopen("ae6.7data.txt","r"))==NULL){
		printf("File open error!\n");
		exit(0); 
	}
	FILE*fpw;
	if((fpw=fopen("ae6.7output.txt","w"))==NULL){
		printf("File open error!\n");
		exit(0); 
	}
	
	int M,N;
	fscanf(fpr,"%d %d",&M,&N);
	//读取矩阵
	int A[M][N],B[M][N],C[M][N],D[M][N];
	int i,j,n=1;

	for(i=0;i<M;i++)
	    for(j=0;j<N;j++){
	        fscanf(fpr,"%d",&A[i][j]);
		}
	for(i=0;i<M;i++)
	    for(j=0;j<N;j++){
			fscanf(fpr,"%d",&B[i][j]);
		}
		
	for(i=0;i<M;i++)
	    for(j=0;j<N;j++){
			fscanf(fpr,"%d",&C[i][j]);
		}
		
	fclose(fpr);
	
	fprintf(fpw,"三个矩阵的和为= \n");

	for(i=0;i<M;i++){
		for(j=0;j<N;j++){
			fprintf(fpw,"%d ",A[i][j]+B[i][j]+C[i][j]);
		}fprintf(fpw,"\n");
	}
	
	fclose(fpw);
}

C语言-数组作业_第12张图片
6.8

#include 
#define N 6
void multiple(int (*a)[N],int (*b)[N])
{
	int c[N][N];
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++){
			c[i][j]=0;
			for(int k=0;k<N;k++){
		        c[i][j]+=a[i][k]*b[k][j];
		    }
		}
		
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			printf("%d ",c[i][j]);
		}printf("\n");	
	}
}
int main()
{   FILE *fp;
    fp=fopen("a.txt","r");
    int i,j,k;    
	int a[N][N],b[N][N];
	
	for(i=0;i<N;i++)
	    for(j=0;j<N;j++){
	        fscanf(fp,"%d",&a[i][j]);
		}
	for(i=0;i<N;i++)
	    for(j=0;j<N;j++){
			fscanf(fp,"%d",&b[i][j]);
		}
	fclose(fp);
	multiple(a,b);

	return 0;
}

C语言-数组作业_第13张图片
C语言-数组作业_第14张图片


#include 
#include
void multiple(int *left,int *right,int *result,int f1,int f2,int s1,int s2)
{
    for (int i=0;i<f1;i++){
        for (int j = 0; j < s2; j++){
            result[i*f2+j] = 0;        
            for(int p=0;p<f2;p++)
            {
                result[i*f2+j] +=left[i*f2+p]*right[p*f2+j];
            }
        }
    }
}
int main()
{   FILE *fpr;
	if((fpr=fopen("a.txt","r"))==NULL){
		printf("File open error!\n");
		exit(0); 
	}
	FILE*fpw;
	if((fpw=fopen("output.txt","w"))==NULL){
		printf("File open error!\n");
		exit(0); 
	}

    int i,j;
	int M,N;  
	fscanf(fpr,"%d %d",&M,&N);

	int a[M][N],b[N][M],c[M][M];
	for(i=0;i<M;i++)
	    for(j=0;j<N;j++){
	        fscanf(fpr,"%d",&a[i][j]);
		}
	for(i=0;i<N;i++)
	    for(j=0;j<M;j++){
			fscanf(fpr,"%d",&b[i][j]);
		}
	fclose(fpr);
	
	fprintf(fpw,"n x m = \n");
	multiple(a[0],b[0],c[0],M,N,N,M);
	for(i=0;i<M;i++){
		for(j=0;j<M;j++){
			fprintf(fpw,"%d ",c[i][j]);
		}fprintf(fpw,"\n");
	}
	
	fprintf(fpw,"\nm x n = \n");
	multiple(b[0],a[0],c[0],N,M,M,N);
	for(i=0;i<M;i++){
		for(j=0;j<M;j++){
			fprintf(fpw,"%d ",c[i][j]);
		}fprintf(fpw,"\n");
	}

	fclose(fpw);
	return 0;

}

C语言-数组作业_第15张图片
C语言-数组作业_第16张图片


#include
int function(int a,int b,int c,int d,int e,int f,int g,int h,int i)
{
	int result = a*e*i+b*f*g+c*d*h-a*f*h-b*d*i-c*e*g;
	return result;
}
int main()
{
	int A[3][1];
	int x1,x2,x3;
	int a,b,c,d,e,f,g,h,i,j,k,l,x,y,z,m;
	FILE *fp;
	fp = fopen("6.10data.txt","r");
	for(i=0;i<3;i++){
		for(j=0;j<4;j++){
			fscanf(fp,"%d",&A[i][j]);
		}
	}
	printf("该方程组为:\n");
	for(i=0;i<3;i++){
			printf("%3dx1 + %3dx2 + %3dx3 = %3d\n",A[i][0],A[i][2],A[i][3],A[i][4]);
		}
	
	a=A[0][0];b=A[0][5];c=A[0][6];d=A[0][7];
	e=A[1][0];f=A[1][8];g=A[1][9];h=A[1][10];
	i=A[2][0];j=A[2][11];k=A[2][12];l=A[2][13];
	
	m = function(a,b,c,e,f,g,i,j,k);
	if(m==0)
		printf("x=0,y=0,z=0\n");
	x1=function(d,b,c,h,f,g,l,j,k)/(float)m;
	x2=function(a,d,c,e,h,g,i,l,k)/(float)m;
	x3=function(a,b,d,e,f,h,i,j,l)/(float)m;
	printf("\n方程的解为:x1=%d,x2=%d,x3=%d",x1,x2,x3);
	
	fclose(fp);
}

C语言-数组作业_第17张图片

10.冒泡排序、交换最大排序、二进制搜索

Sort algorithms (bubble sort and exchange maximum sort ) and binary search (using functions /Recursion)
排序算法(冒泡排序和交换最大排序)和二进制搜索(使用递归函数/)

#include 
#include 
#include
#define SIZE 10

void bubble_sort(int n,int *b)
	//BUBBLE SORT
{	int temp;
	for(int i=0;i<n;i++){
		for(int j=0;j<n-i-1;j++){
			if(b[j]>b[j+1]){
				temp=b[j+1];
				b[j+1]=b[j];
				b[j]=temp;
			}
		}
	}
}

//EXCHANGE MAXIMUM SORT	
void exchange_max_sort(int *c,int n)		
{	int max,wheremax;
	for(int i=n-1;i>=0;i--){
		max=c[i];
		for(int j=i;j>=0;j--){
			if(max<=c[j]){
				max=c[j];
				wheremax=j;
			}
		} 
		c[wheremax]=c[i];
		c[i]=max;
	}
}

//BINARY SEARCH
int binsearch(int *a, int key, int low, int high)
{
	int mid = (low+high) >> 1;
	if (low>high) {
		return -1;
	}
	if (key==a[mid]) {
		return mid;
	}else if (key > a[mid]) {
		return binsearch(a,key,mid+1,high);
	}else {
		return binsearch(a,key,low,mid-1);
	}
	return -1;
}

//PRINT RESULTS
void print(int n,int *a,int *b,int *c)
{
	printf("\n排序数组:\n"
			"序号  原数组	冒泡排序	交换最大值排序\n");

		for(int i=0;i<n;i++){
		printf("i=%1d,  a[i]=%2d,   b[i]=%2d,     	c[i]=%2d\n",
		i,a[i],b[i],c[i]);
	}
}

int main()
{
	int i,j,k,n=SIZE,b[SIZE],c[SIZE];

	int a[SIZE] = { 1,2,3,6,4,5,7,8,9 };
	for(i=0;i<n;i++)	b[i]=c[i]=a[i];
	printf("原数组:\n");
	for(i=0;i<n;i++){
		printf("a[%d]=%d ",i,a[i]);
	}
	bubble_sort(n,b);
	print(n,a,b,c);
	exchange_max_sort(c,n);
	
	printf("\n请输入你要查找的数字(找到返回下标,否则返回-1):");
	scanf("%d", &i);
	i = binsearch(b, i, 0, n-1);
	printf("%d\n", i);
	

}

C语言-数组作业_第18张图片

11.

6.11

#include
int main()
{
	float A[]={4.4,3.3,2.2,5.5,1.1,6.6,7.7,10.0,9.9,8.8};
	int temp,size=10;
	for(int i=0;i<size;i++){
		for(int j=0;j<size-i-1;j++){
			if(A[j]<A[j+1]){
				temp=A[j+1];
				A[j+1]=A[j];
				A[j]=temp;
			}
		}
	}
	for(int i=0;i<size;i++){
		printf("%.1lf ",A[i]);
	}
	
}

C语言-数组作业_第19张图片

6.12

C语言-数组作业_第20张图片

#include
#define M 5
#define N 4

void b(int (*A)[N])
{
    for(int j=0;j<N;j++)
    {
       int m,n,temp;
       for(m=M-1;m>0;m--){
            for(n=0;n<m;n++){
                  if(A[n][j]>A[n+1][j]){
                      temp=A[n][j];
                      A[n][j]=A[n+1][j];
                      A[n+1][j]=temp;
                  }
            } 
       } 
    } 
}
void a(int (*A)[N])
{
    for(int j=0;j<N;j++)
    {
       int m,n,temp;
       for(m=M-1;m>0;m--){
            for(n=0;n<m;n++){
                  if(A[n][j]<A[n+1][j]){
                      temp=A[n][j];
                      A[n][j]=A[n+1][j];
                      A[n+1][j]=temp;
                  }
            } 
       } 
    } 
}




void print(int (*A)[N])
{
	for(int i=0;i<M;i++){
		for(int j=0;j<N;j++){
			printf("%d ",A[i][j]);
		}printf("\n");
	}
}

int main()
{
	int A[M][N]={{3,33,333,3333},
				{5,55,555,5555},
				{1,11,111,1111},
				{4,44,444,4444},
				{2,22,222,2222}};
	a(A);
	print(A);
	printf("\n————————————\n");
	b(A);
	print(A);
}

C语言-数组作业_第21张图片

6.13

C语言-数组作业_第22张图片

#include//计算行列式
#define N 100

int main(void)
{
	int n,i,j,determinant,s[N][N] = {{0}};;
	printf("Please input n=");
	scanf("%d", &n);

	//生成回文数组 
 	for (i=0;i<n;i++) {
		for (j=0;j<n;j++) {
			s[i][j]=(i+1)*10+(j+1);//首位*10+个位 
		}
	}
	//输出回文数组 
	for (i=0;i<n;i++) {
		for (j=0;j<n;j++) {
			printf("%d ",s[i][j]);
		}
		printf("\n");
	}
	//求出a的行列式 
	int a[3][3] = {{11,12,13},{21,22,23},{31,32,33}};
	int sum1,sum2,sum;
	sum1=a[0][2]*a[1][0]*a[2][1] + a[0][1]*a[1][2]*a[2][0] + a[0][0]*a[1][1]*a[2][2];
	sum2=a[2][2]*a[0][1]*a[1][0] + a[2][1]*a[1][2]*a[0][0] + a[0][2]*a[1][1]*a[2][0];
	sum=sum1-sum2;
	printf("Determinant of a = %d", sum);
	return 0;

}

C语言-数组作业_第23张图片

你可能感兴趣的:(C语言)