C Program HW2
C Program HW2
for (j=0;j<a;j++){
scanf("%d",&A[j]);
}
int b=0;
int i;
int tmp;
for (i=1;i<a;i++){
for(j=0;j<=(a-1-i);j++){
if (A[j]>A[j+1]){
tmp=A[j];
A[j]=A[j+1];
A[j+1]=tmp;
b++;
}
}
}
for (j=0;j<(a-1);j++){
printf("%d ",A[j]);
}
printf("%d",A[a-1]);
printf("\n%d",b);
}
第二題 Matrix Vector Multiplication
#include <stdio.h>
#include <stdlib.h>
#define SIZE 1000
/* run this program using the console pauser or add your own getch,
system("pause") or input loop */
for (i=0;i<a;i++){
int total=0;
for (j=0;j<b;j++){
total+=matrix[i][j]*c[j];
}
printf("%d\n",total);
}
}
第四題 Spread sheet
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch,
system("pause") or input loop *
int main(int argc, char *argv[]) {
int r,c;
scanf("%d %d\n",&r,&c);
int matrix[r+1][c+1];
int i,j;
for (i=0;i<r;i++){
for (j=0;j<c;j++){
scanf("%d",&matrix[i][j]);
}
}
for (i=0;i<r;i++){
int total=0;
for (j=0;j<c;j++){
total+=matrix[i][j];
}
matrix[i][c]=total;
}
for (j=0;j<=c;j++){
int sum=0;
for (i=0;i<r;i++){
sum+=matrix[i][j];
}
matrix[r][j]=sum;
}
for (i=0;i<=r;i++){
for (j=0;j<c;j++){
printf ("%d ",matrix[i][j]);
}
printf ("%d",matrix[i][c]);
printf("\n");
}
}
/* run this program using the console pauser or add your own getch,
system("pause") or input loop */
}
}
}
for (i=0;i<26;i++){
printf("%c : %d\n",letter1[i],Count[i]);
}