0% found this document useful (0 votes)
17 views

C Program HW2

The document contains code snippets for 5 programming problems: 1) character count, 2) matrix vector multiplication, 3) bubble sort, 4) spreadsheet calculations, 5) binary search. The code includes loops, arrays, and input/output operations to solve each problem.

Uploaded by

蕭筠儒
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

C Program HW2

The document contains code snippets for 5 programming problems: 1) character count, 2) matrix vector multiplication, 3) bubble sort, 4) spreadsheet calculations, 5) binary search. The code includes loops, arrays, and input/output operations to solve each problem.

Uploaded by

蕭筠儒
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

2-3

第三題 bubble sort


法一
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
/* run this program using the console pauser or add your own getch,
system("pause") or input loop */
int main(int argc, char *argv[]) {
int a;
scanf("%d",&a);
int A[a];
int j;

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 */

int main(int argc, char *argv[]) {


int a,b;
scanf("%d %d\n",&a,&b);
int matrix[a][b];
int i,j;
for (i=0;i<a;i++){
for (j=0;j<b;j++){
scanf("%d",&matrix[i][j]);
}
}
int c[b];
for (j=0;j<b;j++){
scanf("%d",&c[j]);
}

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");
}
}

第五題 binary search


int main(int argc, char *argv[]) {
int a,b;
int i,j;
scanf("%d",&a);
int arr1[a];
for(i=0;i<a;i++){
scanf("%d",&arr1[i]);
}
scanf("%d",&b);
int arr2[b];
for(j=0;j<b;j++){
scanf("%d",&arr2[j]);
}
int count=0;
for(j=0;j<b;j++){
for (i=0;i<a;i++){
if (arr1[i]==arr2[j]){
count++;
break;
}
}
}
printf("%d",count);
}
第一題
#include <stdlib.h>
#include <stdio.h>

/* run this program using the console pauser or add your own getch,
system("pause") or input loop */

int main(int argc, char *argv[]) {


char letter1[26] = {"abcdefghijklmnopqrstuvwxyz"};
char letter2[26] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
char string[999999];
int i,j;
int Count[26]={0};
while(scanf("%s",&string)!=EOF){
for(j=0;string[j]!='\0';j++){
for(i=0;i<26;i++){
if (string[j]== letter1[i] || string[j]== letter2[i]){
Count[i]++;
}

}
}
}
for (i=0;i<26;i++){
printf("%c : %d\n",letter1[i],Count[i]);
}

You might also like