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; } ```

相关推荐

最新推荐

recommend-type

C语言参数个数可变函数详解

VA函数(variable argument function),参数个数可变函数,又称可变参数函数。C/C++编程中,系统提供给编程人员的va函数很少。*printf()/*scanf()系列函数,用于输入输出时格式化字符串;exec*()系列函数,用于在...
recommend-type

ansys maxwell

ansys maxwell
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

导入numpy库,创建两个包含9个随机数的3*3的矩阵,将两个矩阵分别打印出来,计算两个数组的点积并打印出来。(random.randn()、dot()函数)

可以的,以下是代码实现: ```python import numpy as np # 创建两个包含9个随机数的3*3的矩阵 matrix1 = np.random.randn(3, 3) matrix2 = np.random.randn(3, 3) # 打印两个矩阵 print("Matrix 1:\n", matrix1) print("Matrix 2:\n", matrix2) # 计算两个数组的点积并打印出来 dot_product = np.dot(matrix1, matrix2) print("Dot product:\n", dot_product) ``` 希望