当前位置:网站首页>6-7 average the surrounding elements of a two-dimensional array
6-7 average the surrounding elements of a two-dimensional array
2022-06-22 09:35:00 【Xia ~ Chen】
This topic requires the average value of the surrounding elements of a two-dimensional array .
Function interface definition :
double avg(int a[][N]);The function value returns the average value of the surrounding elements .
Sample referee test procedure :
#include <stdio.h>
#define M 3
#define N 4
double avg(int a[][N]);
int main()
{
int i,j,a[M][N];
double av;
for(i=0;i<M;i++)
for(j=0;j<N;j++)
scanf("%d",&a[i][j]);
av=avg(a);
printf("%f",av);
return 0;
}
/* Please fill in the answer here */sample input :
1 2 3 4
5 6 7 8
9 1 2 3sample output :
Here is the corresponding output . for example :
3.800000Ideas : This problem requires us to find the average value of the surrounding elements of a two-dimensional array , Just traverse the surrounding elements and get the average value , Relatively simple , The code is as follows :
double avg(int a[][N])
{
int i, j,k=0;//i j Is a cyclic variable k It is used to calculate the sum of several numbers , Easy to calculate the average value
double sum=0;// Sum up
double av;// Average.
for (i = 0; i < M; i++)// Outer loop control line , Inner loop control column
{
for (j = 0; j < N; j++)
{
if (i == 0 || j == 0 || i == (M-1) || j == (N-1))// As long as it is the first or last line . First of all
// The number of columns and the last column are peripheral numbers .
{
k++;
sum += a[i][j];
}
}
}
av = sum/ k;
return av;
}边栏推荐
- [cmake命令笔记]find_path
- Lexical Sign Sequence
- Final典型案例
- mknod
- Have you ever paid attention to those classes in the collection that are thread safe?
- 接口抽象类的比较
- SQL编程task02作业-基础查询与排序
- 機器學習|nltk_Data下載錯誤|nltk的stopwords語料下載錯誤解决方法
- 求余弦的大小
- C language question brushing | three item operation to realize capital judgment (16)
猜你喜欢

Debian10配置RSyslog+LoganAlyzer日志服务器

Byte/byte? Don't get dizzy!

使用ELK保存Syslog、Netflow日志和审计网络接口流量

Common SQL statements in MySQL

模糊查询和聚合函数

机器学习|nltk_Data下载错误|nltk的stopwords语料下载错误解决方法

A readme of an old open source person - how to do open source well

为啥要使用梯度下降法
![Let you get started [uni app]](/img/e7/bc33d54a345901f67afa856769045f.png)
Let you get started [uni app]
![[hdu] P6964 I love counting](/img/ff/f8e79d28758c9bd3019816c8f46723.png)
[hdu] P6964 I love counting
随机推荐
经典&&案例
两个线程各执行100次i++,得到的可能值
Why use gradient descent method
Have you ever paid attention to those classes in the collection that are thread safe?
Feedforward and backpropagation
Machine learning | nltk_ Data download error |nltk's stopwords corpus download error solution
Sparse array ^ create ^ restore ^ save ^ fetch -- family bucket
How C processes use non static methods
MySQL中常用的SQL语句
Detr: end to end object detection with transformers (from Li Mu and Zhu)
逻辑回归和线性回归
[ZOJ] P3228 Searching the String
Yiwen approaches ZMQ
通过docker安装mysql(5.7+8.0)并配置主从复制(GTID+增强半同步)
copy_ from_ User and copy_ to_ user
Fuzzy query and aggregate function
HDU - 7072 双端队列+对顶
[Architect (Part 39)] connecting MySQL database developed by server
The circular queue is implemented in super detail. I understand it in a few seconds
DHCP Relay