第一范文网 - 专业文章范例文档资料分享平台

用C++解决问题第十版Chapter 5 Functions for All Subtasks

来源:用户分享 时间:2025/12/4 23:28:57 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

Chapter 5

Functions for All Subtasks

1. Solutions to Selected Practice Programs and Programming Projects Detailed solutions selected projects are presented here. The rest are essentially the same problems, except for what is being converted. Notes about the remaining problems are included.

One of the more important things in programming is planning, even for the simplest program. If the planning is thorough, the coding will be easy, and the only errors likely to be encountered are syntax errors, usually caused by either typing errors, a boundary condition problem (frequently, an off by one error), or (we hope not) lack of knowledge of the language details.

Practice Program 1: Statistics

Compute average and standard deviation of 4 entries. General remarks are in the code file which I present here: #include using namespace std; /*

Task: Write a function that computes average (I will call this the arithmetic mean or simply the mean) and standard deviation of four scores. The average or mean, avg, is computed as

avg = ( s1 + s2 + s3 + s4 ) / 4

The standard deviation is computed as

1

Copyright ? 2018 Pearson Education, Inc.

Savitch

Problem Solving w/ C++, 10e Instructor’s Resource Guide

Chapter 5

where a = avg. Note that some statisticians may wish to use 3 instead of 4. We will use 4.

Input: scores s1 s2 s3 s4

Output: standard deviation and mean.

Required: The function is to have 6 parameters. This function calls two others that compute the mean and the std deviation. A driver with a loop should be written to test the function at the user's option. */

//function declaration (or prototype)

//When used, the math library must be linked to the //executable.

#include // for sqrt using namespace std;

void average (double s1, double s2, double s3, double s4, double& avg) {

avg = ( s1 + s2 + s3 + s4 ) / 4; }

// Preconditions: average function must have been called on //the data, and the value of the average passed into the //parameter a

( s1 ? a ) ? ( s ) ? ( s3 ? a ) 2 ? a std_deviation = 4

14

Copyright ? 2018 Pearson Education, Inc.

Savitch

Problem Solving w/ C++, 10e Instructor’s Resource Guide

Chapter 5

//Postconditions: Standard deviation is passed back in //the variable stdDev

void sD (double s1, double s2, double s3, double s4, double a, double& stdDev) {

stdDev = sqrt( (s1 - a)*(s1 - a) + (s2 - a)*(s2 - a) + (s3 - a)*(s3 - a) + (s4 - a)*(s4 - a) )/4 ; }

void statistics( double s1, double s2, double s3, double s4, double& avg, double& stdDev );

//Preconditions: this function is called with any set of //values. Very large or very small numbers are subject to //errors in the calculation. Analysis of this sort of error //is beyond the scope of this chapter.

//PostConditions: avg is set to the mean of s1, s2, s3, s4 //and stdDev is set to the standard deviation of s1..s4 //function definition:

void statistics( double s1, double s2, double s3, double s4, double& avg, double& stdDev ) {

average ( s1, s2, s3, s4, avg ); sD ( s1, s2, s3, s4, avg, stdDev ); }

int main() {

double s1, s2, s3, s4, avg, stdDev; char ans;

14

Copyright ? 2018 Pearson Education, Inc.

Savitch

Problem Solving w/ C++, 10e Instructor’s Resource Guide

Chapter 5

do {

cout << \ << \ << endl

<< \ cin >> s1 >> s2 >> s3 >> s4;

statistics( s1, s2, s3, s4, avg, stdDev);

cout << \ << \ << \

<< \

cout << \ cin >> ans;

} while ( 'Y' == ans || 'y' == ans ); return 0; }

A typical run follows: 21:44:35:~/AW$ a.out

Enter 4 decimal numbers, I will give you the mean and standard deviation of the data 12.3 13.4 10.5 9.0

mean of 12.3 13.4 10.5 9 is 11.3

the standard deviation of these numbers is 0.841873 y or Y continues, any other terminates y

Enter 4 decimal numbers, I will give you the mean and standard deviation of the data 1 2 3 4

14

Copyright ? 2018 Pearson Education, Inc.

Savitch

Problem Solving w/ C++, 10e Instructor’s Resource Guide

Chapter 5

mean of 1 2 3 4 is 2.5

the standard deviation of these numbers is 0.559017 y or Y continues, any other terminates n

21:45:05:~/AW$

Practice Program 2: Convert Feet/Inches to Meters Conversion of feet/inches to meters:

//Task: Convert feet/inches to meters

//Input:a length in feet and inches, with possible decimal //part of inches

//Output: a length in meters, with 2 decimal places, which //are the 'centimeters' specified in the problem.

//Required: functions for input, computation, and output. //Include a loop to repeat the calculation at the user's //option.Remarks: The computation is a simple conversion from //feet + inches to feet with a decimal part, then to meters. //Output is restricted to 2 decimal places. //

//By 'meters and centimeters' the author means that the //output is to be meters with two decimal places - which is //meters and centimeters. I mention this because my students //always stumble at this because of a lack of knowledge of //the metric system. #include using namespace std;

void input ( int& feet, double& inches ); //Precondition: function is called

14

Copyright ? 2018 Pearson Education, Inc.

搜索更多关于: 用C++解决问题第十版Chapter 5 Functions 的文档
用C++解决问题第十版Chapter 5 Functions for All Subtasks.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c9upfl60rfu3bj0w6iip07zlrl1bk8m0130e_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top