C++Primer fifth edition exercise answers [Chapter 6]

C++Primer Fifth Edition Exercise Answers [General List]: https://blog.csdn.net/Dust_Evc/article/details/114334124

6.1

The actual parameters are stored in the calling function, and the formal parameters are stored in the called function.

The actual parameter is the actual value of the function call and the initial value of the formal parameter.

6.2

(a) int f() {
          string s;
          // ...
          return s;
    }
(b) f2(int i) { /* ... */ }
(c) int calc(int v1, int v1) { /* ... */ }
(d) double square (double x)  return x * x; 

6.3

#include <iostream>

int fact(int i)
{
	return i > 1 ? i * fact(i - 1) : 1;
}

int main()
{
	std::cout << fact(5) << std::endl;
	return 0;
}

6.4

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

unsigned main()
{
	void interactive_fact();
	interactive_fact();

	string Repeat = "是否重复本程序? y/n";
	cout << Repeat << endl;

	char C = ' ';
	while (cin >> C && C == 'y')
	{
		interactive_fact();
		cout << Repeat << endl;
	}

	return 0;
}

void interactive_fact()
{
	unsigned num = 0, mul = 1;
	cout << "请输入一个1到20之间的数字:";
	cin >> num;

	string out_of_range = "该数字不在范围内!请重新输入:";
	while (num < 1 || num >20)
	{
		cout << out_of_range;
		cin >> num;
	}
	while (num > 0)
		mul *= num--;

	cout << "该数字的阶乘为:" << mul << endl;
}

6.5

#include <iostream>

int abs(int i)
{
    return i > 0 ? i : -i;
}

int main()
{
    std::cout << abs(-5) << std::endl;
    return 0;
}

6.6

**Formal parameters** are defined in the function parameter list;

**Local variables** are defined in the code block;

**Local static variables** are initialized when the program's execution path passes through the object definition statement for the first time, and are not destroyed until the program terminates.

6.7

int generate()
{
    static int ctr = 0;
    return ctr++;
}

6.8

Chapter6.h

#pragma once

#ifndef HEADER_H
#define HEADER_H
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

void interactive_fact(); 
#endif // !HEADER_H

6.9

fact.cpp

#include"Chapter6.h"

void interactive_fact()
{
	unsigned num = 0, mul = 1;
	cout << "请输入一个1到20之间的数字:" ;
	cin >> num;

	string out_of_range = "该数字不在范围内!请重新输入:";
	while (num < 1 || num >20)
	{
		cout << out_of_range;
		cin >> num;
	}
	while (num > 0)
		mul *= num--;
	
	cout << "该数字的阶乘为:" << mul << endl;
}

factMain.cpp

#include"Chapter6.h"

unsigned main()
{
	interactive_fact();

	string Repeat = "是否重复本程序? y/n";
	cout << Repeat << endl;

	char C = ' ';
	while (cin>>C && C == 'y')
	{
		interactive_fact();
		cout << Repeat << endl;
	}
	
	return 0;
}

6.10

6.11

 

 

 

6.40

 (a) Correct.
 (b) Error. Because once a parameter is assigned a default value, all subsequent parameters must have a default value.

6.41

 (a) Illegal. The first parameter is not the default parameter, at least one actual parameter is required.
 (b) Legal.
 (c) Legal, but inconsistent with the original intention. The character `*` is interpreted as `int` and passed into the second parameter. The original intention is to pass the third parameter.

6.42

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

string make_plural(size_t ctr, const string& word, const string& ending = "s")
{
	return (ctr > 1) ? word + ending : word;
}

int main()
{
	cout << "singual: " << make_plural(1, "success", "es") << " "
		<< make_plural(1, "failure") << endl;
	cout << "plural : " << make_plural(2, "success", "es") << " "
		<< make_plural(2, "failure") << endl;

	return 0;
}

 

Guess you like

Origin blog.csdn.net/Dust_Evc/article/details/114271410