当前位置:网站首页>C Primer Plus Chapter 10, question 14 3 × 5 array
C Primer Plus Chapter 10, question 14 3 × 5 array
2022-07-03 04:29:00 【multydoffer】
Write a program , Prompt the user for input 3 Group number , Each group contains 5 individual double Number of types ( Assume that all users respond correctly , Non numeric data will not be entered ). The procedure shall accomplish the following tasks :
a. Store the data entered by the user in 3×5 In the ground array
b. Calculate each group (5 individual ) The average of the data
c. Calculate the average of all data
d. Find out 15 The maximum of the data
e. Print the results
Each task should be completed with a separate function ( Use variable length arrays as formal parameters ). To complete the task b, To write a function that calculates and returns the average value of a one-dimensional array , Call this function with a loop 3 Time . For functions that handle other tasks , You should take the entire array as an argument , To complete the task c and d The function of should return the result to the calling function .
Here is the code :
#include <stdio.h>
double average(int n, double arr[n]);
double max(int n, double arr[n]);
int main(void)
{
double arr[3][5];
int i;
printf("Enter an array of 3x5 (double):");
for(i = 0; i < 15; i++)
scanf("%lf", (double *)arr + i);
for(i = 0; i < 3; i++)
printf("Average for each row is: %.2lf\n", average(5, (double *)(arr + i)));
printf("Average for total is: %.2lf\n", average(15, (double *)arr));
printf("The max in your array is: %.2lf\n", max(15, (double *)arr));
return 0;
}
double average(int n, double arr[n])
{
double total = 0;
double * end = arr + n;
while(arr < end)
total += *arr++;
return total / n;
}
double max(int n, double arr[n])
{
double max = *arr;
double * end = arr + n;
while(arr < end)
{
if(max < *arr)
max = *arr;
arr++;
}
return max;
}边栏推荐
- Kubernetes source code analysis (I)
- Php+mysql registration landing page development complete code
- 540. Single element in ordered array
- 拆一辆十万元的比亚迪“元”,快来看看里面的有哪些元器件。
- Priv app permission exception
- Library management system based on SSM
- How to process the current cell with a custom formula in conditional format- How to address the current cell in conditional format custom formula?
- [fairseq] 报错:TypeError: _broadcast_coalesced(): incompatible function arguments
- Jincang KFS data bidirectional synchronization scenario deployment
- 2022-02-14 (394. String decoding)
猜你喜欢

Data Lake three swordsmen -- comparative analysis of delta, Hudi and iceberg

解决bp中文乱码

Two points -leetcode-540 A single element in an ordered array

2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills

540. Single element in ordered array

智能合约安全审计公司选型分析和审计报告资源下载---国内篇

GFS distributed file system (it's nice to meet it alone)

Solve BP Chinese garbled code

How to choose cross-border e-commerce multi merchant system
![[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN](/img/7e/50fa6f65b5a4f0bb60909f57daff56.png)
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
随机推荐
解决bp中文乱码
redis 持久化原理
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
How to choose cross-border e-commerce multi merchant system
Small program animation realizes the running lantern and animation object
[mathematical logic] predicate logic (toe normal form | toe normal form conversion method | basic equivalence of predicate logic | name changing rules | predicate logic reasoning law)
Competitive product analysis and writing
A outsourcing boy's mid-2022 summary
The latest activation free version of Omni toolbox
有道云笔记
Design and implementation of kubelet garbage collection mechanism to protect nodes from being preempted by containers image GC high threshold
[set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
Solve BP Chinese garbled code
金仓KFS数据双向同步场景部署
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
Matplotlib -- save graph
Redis persistence principle
[fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
金仓数据库KingbaseES 插件kdb_exists_expand
How to process the current cell with a custom formula in conditional format- How to address the current cell in conditional format custom formula?