c语言统计函数
时间: 2023-12-16 18:02:42 浏览: 25
在C语言中,可以使用以下代码实现统计函数:
```c
#include <stdio.h>
int count(int arr[], int n, int x) {
int cnt = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
cnt++;
}
}
return cnt;
}
int main() {
int arr[] = {1, 2, 3, 3, 4, 5, 3};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 3;
int cnt = count(arr, n, x);
printf("%d\n", cnt);
return 0;
}
```
在这个例子中,我们定义了一个`count`函数,它接受一个整数数组、数组长度和一个目标整数作为参数,返回目标整数在数组中出现的次数。
在`main`函数中,我们声明一个整数数组`arr`,数组中包含多个重复的整数。然后,我们调用`count`函数,传入数组、数组长度和目标整数。最后,我们打印出目标整数在数组中出现的次数。
相关问题
c语言调用函数财务记账
以下是一个简单的C语言财务记账程序,其中包含了调用函数的过程:
```c
#include <stdio.h>
// 定义结构体
struct Record {
char date[20];
char type[20];
double amount;
};
// 定义函数
void addRecord(struct Record records[], int *count) {
printf("请输入日期:");
scanf("%s", records[*count].date);
printf("请输入类型:");
scanf("%s", records[*count].type);
printf("请输入金额:");
scanf("%lf", &records[*count].amount);
(*count)++;
}
void showRecords(struct Record records[], int count) {
printf("---------------------账单统计-------------------------\n");
printf("日期\t\t类型\t\t金额\n");
for (int i = 0; i < count; i++) {
printf("%s\t%s\t%.2lf\n", records[i].date, records[i].type, records[i].amount);
}
}
int main() {
struct Record records[100];
int count = 0;
int choice;
while (1) {
printf("********************会计记账管理系统********************\n");
printf("1.添加记录\n");
printf("2.查看记录\n");
printf("3.退出\n");
printf("请选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
addRecord(records, &count);
break;
case 2:
showRecords(records, count);
break;
case 3:
return 0;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
return 0;
}
```
使用c语言生成概率统计函数库
1. 随机数生成函数
```c
#include <stdlib.h>
#include <time.h>
void initRand() {
srand(time(NULL));
}
int randInt(int min, int max) {
return rand() % (max - min + 1) + min;
}
double randDouble(double min, double max) {
return (double)rand() / RAND_MAX * (max - min) + min;
}
```
2. 统计函数
```c
#include <stdio.h>
int countChar(const char* str, char c) {
int count = 0;
while (*str) {
if (*str == c) {
count++;
}
str++;
}
return count;
}
int countWord(const char* str) {
int count = 0;
while (*str) {
if (*str == ' ' || *str == '\n' || *str == '\t') {
count++;
}
str++;
}
return count;
}
int countLine(const char* str) {
int count = 0;
while (*str) {
if (*str == '\n') {
count++;
}
str++;
}
return count;
}
int countDigit(int n) {
int count = 0;
while (n) {
count++;
n /= 10;
}
return count;
}
int countPrime(int n) {
if (n <= 1) {
return 0;
}
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
```
3. 概率函数
```c
#include <math.h>
double normalPDF(double x, double mu, double sigma) {
return exp(-pow(x - mu, 2) / (2 * pow(sigma, 2))) / (sqrt(2 * M_PI) * sigma);
}
double normalCDF(double x, double mu, double sigma) {
return (1 + erf((x - mu) / (sigma * sqrt(2)))) / 2;
}
double binomialPDF(int k, int n, double p) {
int c = 1;
for (int i = 1; i <= k; i++) {
c *= n - i + 1;
c /= i;
}
return c * pow(p, k) * pow(1 - p, n - k);
}
double binomialCDF(int k, int n, double p) {
double cdf = 0;
for (int i = 0; i <= k; i++) {
cdf += binomialPDF(i, n, p);
}
return cdf;
}
double uniformPDF(double x, double a, double b) {
if (x < a || x > b) {
return 0;
}
return 1 / (b - a);
}
double uniformCDF(double x, double a, double b) {
if (x < a) {
return 0;
}
if (x >= b) {
return 1;
}
return (x - a) / (b - a);
}
```
4. 示例代码
```c
#include <stdio.h>
#include "stats.h"
int main() {
initRand();
// 随机数生成
printf("randInt(1, 10): %d\n", randInt(1, 10));
printf("randDouble(0, 1): %f\n", randDouble(0, 1));
// 统计函数
printf("countChar(\"hello world\", 'l'): %d\n", countChar("hello world", 'l'));
printf("countWord(\"hello world\\nhi there\\n\"): %d\n", countWord("hello world\nhi there\n"));
printf("countLine(\"hello world\\nhi there\\n\"): %d\n", countLine("hello world\nhi there\n"));
printf("countDigit(12345): %d\n", countDigit(12345));
printf("countPrime(7): %d\n", countPrime(7));
// 概率函数
printf("normalPDF(0, 0, 1): %f\n", normalPDF(0, 0, 1));
printf("normalCDF(1, 0, 1): %f\n", normalCDF(1, 0, 1));
printf("binomialPDF(2, 5, 0.5): %f\n", binomialPDF(2, 5, 0.5));
printf("binomialCDF(2, 5, 0.5): %f\n", binomialCDF(2, 5, 0.5));
printf("uniformPDF(0.5, 0, 1): %f\n", uniformPDF(0.5, 0, 1));
printf("uniformCDF(0.5, 0, 1): %f\n", uniformCDF(0.5, 0, 1));
return 0;
}
```