文章目錄
001 成績存入數組
【題目】m 個人的成績存放在 score 數組中,請編寫函數 fun, 它的功能是:將低於平均分的人作爲函數值返回,將低於平均分的分數放在 below 所指定的函數中。
【代碼】
#include <stdio.h>
int fun(double score[], int m, double below[]){
int i, k = 0;
double aver = 0.0;
for (i = 0; i < m; i++){
aver += score[i];
}
aver /= m;
for (i = 0; i < m; i++){
if (score[i] < aver){
below[k++] = score[i];
}
}
return k;
}
int main(){//main function is used to Test fun function
double score[] = {78,65,45,68,90};
double below[5];
int i = fun(score, 5, below);
printf("%d\n", i);
return 0;
}
002 被 7 或者 11 整除的數
【題目】請編寫函數 fun,它的功能是:求出 1 到 100 之內能被 7 或者 11 整除,但不能同時被 7 和 11 整除的所有整數,並將他們放在 a 所指的數組中,通過 n 返回這些數的個數。
【代碼】
#include <stdio.h>
void fun(int a[], int *n){
int i, j = 0;
for (i = 2; i < 100; i++){
if ((i % 7 ==0 || i % 11 == 0) && i % 77 != 0){
a[j++] = i;
}
}
*n = j;
}
int main(){//main function is used to Test fun function
int n = 0;
int a[1000];
fun(a, &n);
printf("%d\n",n);
return 0;
}
003 求出能整除 x 且不是偶數的整數
【題目】請編寫函數 void fun(int x, int pp[], int *n), 它的功能是:求出能整除 x 且不是偶數的各整數,並按從小到大的順序放在 pp 所指的數組中,這些除數的個數通過形參 n 返回。
【代碼】
#include <stdio.h>
void fun(int x, int pp[], int *n){
int i, j = 0;
for (i = 1; i < x; i++){
if (x % i == 0 && i %2 != 0){
pp[j++] = i;
}
*n = j;
}
}
int main(){//main function is used to Test fun function
int n = 0;
int pp[1000];
fun(6, pp, &n);
printf("%d\n",n);
return 0;
}
004 統計字母出現的次數
【題目】請編寫一個函數 void fun(char *tt, int pp[]), 統計在 tt 字符中" a “到” z " 26 各字母各自出現的次數,並依次放在 pp 所指的數組中。
【代碼】
#include <stdio.h>
#include <string.h>
void fun(char *tt, int pp[]){
int i, j = 0;
for (i = 0; i < strlen(tt); i++){
if (tt[i] >= 'a' && tt[i] <= 'z'){
j = tt[i] - 'a' ;
pp[j] = pp[j] + 1;
}
}
}
int main(){//main function is used to Test fun function
char tt[] = {'a','b','a'};
int i;
int pp[26];
//int pp[26] = {0};
for (i = 0; i < 26; i++){
pp[i] = 0;
}
fun(&tt, pp);
for (i = 0; pp[i] != '\0'; i++){
if (pp[i] > 0){
char c = i + 'a';
printf("%c = %d\n", c, pp[i]);
}
}
return 0;
}
005 指定素數輸入數組
【題目】請編寫一個函數 void fun(int m, int k, int xx[]), 該函數的功能是:將大於整數 m 且緊靠 m 的 k 個素數存入 xx 所指的數組中。
【代碼】
#include <stdio.h>
#include <math.h>
void fun(int m, int k, int xx[]) {
int i, j, count = 0;
for (i = m + 1;count < k ; i++) {
for (j = 2; j <= sqrt(i); j++) {
if (i % j == 0) {
break;
}
}
if (j > sqrt(i)) {
xx[count++] = i;
}
}
}
int main() {
int a[100];
fun(3,5,a);
int i;
for (i = 0; i < 5; i++) {
printf("%d ", a[i]);
}
}
006 刪除指定下標的字符
【題目】請編寫一個函數void fun(char a[],char[],int n),其功能是:刪除以各字符串中指定下標的字符。其中,a指向原字符串,刪除後的字符串存放在b所指的數組中,n中存放指定的下標。
【代碼】
#include <stdio.h>
#include <string.h>
void fun(char a[], char b[], int n) {
int i, j = 0;
for (i = 0; i < strlen(a); i++) {
if (i != n) {
b[j++] = a[i];
}
}
b[j] = '\0';
}
int main() {
char a[] = "Hello";
char b[10];
fun(a, b, 2);
puts(b);
return 0;
}
007 元素下標存放k中
【題目】請編寫一個函數int fun(int *s,int t,int *k),用來求出數組的最大元素在數組中的下標並存放在k所指的儲存單元中。
【代碼】
#include <stdio.h>
#include <string.h>
int fun(int *s, int t, int *k) {
int i, max;
max = s[0];
for (i = 1; i < t; i++) {
if (s[i] > max) {
max = s[i];
*k = i;
}
}
return 0;
}
int main() {
int a[4];
int i;
for (i = 0; i < 4; i++) {
scanf("%d", &a[i]);
}
int value = 0;
fun(&a, 4, &value);
printf("%d", value);
return 0;
}
008 計算並輸出下列多項式值
【題目】編寫函數fun,功能是:根據以下公式計算s,計算結果作爲函數值返回;n通過形參傳入。s=1+1/(1+2)+1/(1+2+3)+…+1/(1+2+3+4+…+n)
【代碼】
#include <stdio.h>
double fun(int n) {
int i, j, t;
double s = 0.0;
for (i = 1; i <= n; i++) {
t = 0;
for (j = 1; j <= i; j++) {
t += j;
}
s += 1.0/t;
}
return s;
}
int main() {
printf("%lf\n", fun(10));
return 0;
}
009 求值
【題目】編寫一個函數fun,它的功能是:根據以下公式求P的值,結果由函數值帶回。m與n爲兩個正整數,且要求m>n。p=m!/n!(m-n)!
【代碼】
#include <stdio.h>
double fun(int m, int n) {
double p, t = 1.0;
int i;
for (i = 1; i <= m; i++) {
t *= i;
}
p = t;
for (t = 1.0, i = 1; i <= n; i++) {
t *= i;
}
p /= t;
for (t = 1.0, i = 1; i <= m - n; i++) {
t *= i;
}
p /= t;
return p;
}
int main() {
printf("%lf",fun(10,9));
return 0;
}
010 簡單迭代
【題目】編寫函數fun,它的功能是:利用以下的簡單迭代方法求方程cos(x)-x=0的一個實根。
迭代步驟如下:(1)取x1初值爲0.0;(2)x0=x1,把x1的值賦各x0;
(3)x1=cos(x0),求出一個新的x1;
(4)若x0-x1的絕對值小於0.000001,則執行步驟(5),否則執行步驟(2);
(5)所求x1就是方程cos(x)-x=0的一個實根,作爲函數值返回。
程序將輸出Root=0.739085。
【代碼】
#include <stdio.h>
#include <math.h>
double fun() {
double x0, x1 = 0.0;
do {
x0 = x1;
x1 = cos(x0);
} while (fabs(x0 - x1) >= 1e-6);
return x1;
}
int main() {
printf("%lf", fun());
return 0;
}