当前位置:网站首页>Pointers: maximum, minimum, and average
Pointers: maximum, minimum, and average
2022-07-06 14:35:00 【|Light|】
requirement
Programming , Enter a one-dimensional integer array , Output the maximum value 、 Minimum and average .( Use a pointer to achieve )
Code
#include<stdio.h>
/* * This function is used to input a one-dimensional integer array , The input data is stored in the formal parameter a Array * Input data in 0 As an end sign ,0 It is not stored in the array nor included in the total number of input data * The return value is the number of input data */
int input(int a[])
{
int n=0;
int b = 0;
do
{
scanf("%d",&b);
if(b == 0)
break;
else
{
a[n] = b;
n++;
}
}
while(b != 0);
return n;
}
/* * This function is used to calculate the formal parameter array a Maximum of 、 minimum value 、 Average * Maximum 、 minimum value 、 The average value passes through the formal parameter pointer variable pmax、pmin、pavg To pass on * n Is a formal parameter array a Number of data in */
void fun(int a[],int *pmax,int *pmin,int *pavg,int n)
{
int i,j,k=0;
*pmax = a[0];
*pmin = a[0];
for(i=1;i<n;i++)
{
if(*pmin > a[i])
{
*pmin = a[i];
}
if(*pmax < a[i])
{
*pmax = a[i];
}
k = k + a[i];
*pavg = (k + a[0])/n;
}
}
main function
int main()
{
int a[200],n,max,min,avg;
n=input(a);
fun(a,&max,&min,&avg,n);
printf(" The maximum value is %d, The minimum value is %d, The average value is %d\n",max,min,avg);
return 0;
}
test
Test input
1 3 5 7 0
Output
The maximum value is 7, The minimum value is 1, The average value is 4
边栏推荐
猜你喜欢
《统计学》第八版贾俊平第四章总结及课后习题答案
四元数---基本概念(转载)
Constants, variables, and operators of SystemVerilog usage
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
内网渗透之内网信息收集(五)
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
Only 40% of the articles are original? Here comes the modification method
JDBC read this article is enough
Markdown font color editing teaching
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
随机推荐
Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
【指针】求字符串的长度
内网渗透之内网信息收集(五)
循环队列(C语言)
New version of postman flows [introductory teaching chapter 01 send request]
What language should I learn from zero foundation. Suggestions
Intranet information collection of Intranet penetration (I)
【指针】数组逆序重新存放后并输出
Statistics 8th Edition Jia Junping Chapter 7 Summary of knowledge points and answers to exercises after class
Intranet information collection of Intranet penetration (3)
内网渗透之内网信息收集(二)
Circular queue (C language)
Database monitoring SQL execution
XSS之冷门事件
【指针】删除字符串s中的所有空格
SystemVerilog discusses loop loop structure and built-in loop variable I
How to test whether an object is a proxy- How to test if an object is a Proxy?
JDBC transactions, batch processing, and connection pooling (super detailed)
JDBC事务、批处理以及连接池(超详细)
【指针】使用插入排序法将n个数从小到大进行排列