C++基础练习 - Chapter 2 (英文版)

Review Questions

2.1 Why do we need the preprocessor derective “# include ”?

Answer: “# include ” directive causes the preprocessor to add-the contents of iostream file to the program.

2.2 How does a main() function in C++ differ from main() in C?

Answer: In C main() by default returns the void type, but in C++ it returns integer by default.

2.3 What do you think is the main advantage of the comment “//” in C++ as compared to the old C type comment?

Answer: " // " is more easy and time-saving than " /* */ ".

2.4 Describe the major parts of a C++ program.

Answer: Major parts of a C++ program:

  1. Include files
  2. Class declaration
  3. Member function definitions
  4. Main function profram

2.5 State whether the following statements are TRUE or FALSE.

a. Since C is a subset of C++, all C programs will run under C++ compilers.
b. In C++, a function contained within a class is called a member function.
c. Looking at one or two lines of code, we can easily recognize wether a program is written in C or C++.
d. In C++, it is very easy to add new features to the existing structure of an object,
e. The concept of using one operator for different purposes is known as operator overloading.
f. The output function “printf” cannot be used in C++ programs.

Answer: a. FALSE b. TRUE c. FALSE d. TRUE e. TRUE f. FALSE

Debugging Exercises

2.1 Identify the error in the following program.

#include <iostream>
using namespace std;

int main()
{
	int i = 0;
	i = i + 1;
	cout << i << " ";
	/*comment \*//i = i + 1;
	cout << i;
	return 0;
}

Answer: /*comment *//i = i + 1; -> Syntax error. (错误使用 注释 )

2.2 Identify the error in the following program.

#include <iostream>
using namespace std;

int main()
{
	short i = 2500, j = 3000;
	cout >> "i + j =" >> -(i + j);
	return 0;
}

Answer: cout >> “i + j =” >> -(i + j); -> Illegal structure operation.

2.3 What will happen when you run the following program?

#include <iostream>
using namespace std;
int main()
{
	int i = 10, j = 5;
	int modResult = 0;
	int divResult = 0;
	modResult = i % j;
	cout << "modResult = " << modResult << endl;
	
	divResult = i / modResult;
	cout << "divResult = " << divResult;
	return 0;
}

Answer: divResult = i / modResult; -> floating point Error or divide by zero
**Note: ** the program is still successfully compile, even it doesn’t show any Error, in my PC.

2.4 Find errors, if any, in the following C++ statements.

(a). cout << "x = " x;
(b). m = 5; // n =10; // = m + n;
©. cin >> x; >> y;
(d). cout << “Enter value:”; cin >> x
(e). /*Addition*/ z = x + y;
(f). cout << \h “Name:” << name;

Answer:
a. Statement Missing, cout << "x = " << x;
b. No Error
c. Expression syntax error, cin >> x >> y;
d. No Error
e. No Error
f. Illegal Character ‘\h’ , cout << “\n Name:” << name;

Programming Exercises

2.1 Write a program to display the following output using a single cout statement.

Maths = 90
Physics = 77
Chemistry = 99

Answer:

#include <iostream>
using namespace std;

int main()
{
	// method 1:
	int Maths = 90;
	int Physics = 77;
	int Chemistry = 99;
	
	cout << "Maths = " << Maths << "\n" << "Physics = " << Physics << "\n" << "Chemistry = " << Chemistry <<endl;
	cout << endl;
	
	// method 2:
	string subjects[3] = {"Maths", "Physics", "Chemistry"};
	int scores[3] = {90, 77, 99};
	for (int i = 0; i<3; i++)
	{
	    cout << subjects[i] << "=" << scores[i] << endl;
	}
	return 0;
}

2.2 Write a program to read two numbers from the keyboard and display the larger value on the screen.

Answer:

#include <iostream>
using namespace std;

int main()
{
	float num1, num2;
	
	cout << "Enter two values:";
    
    cin >> num1 >> num2;
    
    if(num1 <= num2)
        cout << "num2 is bigger, num2 = " << num2 << endl;
    else
        cout << "num1 is bigger, num1 = " << num1 << endl;

	cout << endl;
	
	return 0;
}

Note: 此代码并没有考虑特殊字符等其他非数字的输入情况,随着后续知识的增加,在以后的代码中会考虑的更全面。

2.3 Write a program to input an integer from the keyboard and display on the screen “WELL DONE” that many times.

Answer:

#include <iostream>
using namespace std;

int main()
{
	int num;
	string str = "WELL DONE";
	
	cout << "Enter an integer:";
    
    cin >> num;
    
   for(int i = 0; i <= num; i++)
   {
        
        cout << str << endl;
    }
	cout << endl;
	
	return 0;
}

思考: 大家可以尝试一下在循环条件里将 i<=num 改为 i != num时,分别输入 -1, 看看分别时什么结果,并解释为什么。

2.4 Write a program to read the values a, b and c and display x, where

x = a / (b - c).
Test the program for the following values:
(a). a = 250, b = 85, c = 25
(b). a = 300, b = 70, c =70

Answer:

#include <iostream>
using namespace std;

int main()
{
	float a, b, c, x;
	
	cout << "Enter 3 numbers:";
    
    cin >> a >> b >> c;
    
    if ((b - c) != 0)
    {
        x = a / (b - c);
        cout << "x = a / (b - c) = " << x << endl;
    }
    else
    {
        cout << "Cannot divided by Zero !";
    }
 
	cout << endl;
	
	return 0;
}

2.5 Wrtie a C++ program that will ask for a temperature in Fahrenheit and display it in Celsius.

Answer:

#include <iostream>
using namespace std;

int main()
{
	float F, C;
	
	cout << "Enter a temperature in Fahrenheit scale:";
    
    cin >> F;
    
    C = (F - 32) / 1.8;
    
    cout << "Temperature in Celsius = (Fahrenheit - 32°F) / 1.8 = " << C << endl;
    
	cout << endl;
	
	return 0;
}

hint:Celsius = (Fahrenheit - 32°F) / 1.8

2.6 Redo Exercise 2.5 using a class called temp and member function.

Answer:

#include <iostream>
using namespace std;

class temp{
    float F, C;
    
    public: 
        float conversion(float F);
};

float temp::conversion (float F)
{
    C = (F - 32) / 1.8;
    return C;
}


int main()
{
	float f;
	temp cvt;
	
	cout << "Enter a temperature in Fahrenheit scale:";
    
    cin >> f;
    
    cout << "Temperature in Celsius = (Fahrenheit - 32°F) / 1.8 = " << cvt.conversion(f) << endl;
    
	cout << endl;
	
	return 0;
}

相关推荐

  1. C++基础练习 - Chapter 2英文版

    2024-07-13 04:54:02       30 阅读
  2. C++基础练习 - Chapter 3

    2024-07-13 04:54:02       17 阅读
  3. C++ 基础练习 - 第一章(英文版

    2024-07-13 04:54:02       19 阅读
  4. C++基础-编程练习题2

    2024-07-13 04:54:02       34 阅读
  5. C语言练习(2)

    2024-07-13 04:54:02       135 阅读
  6. OpenXR 超详细spec--Chapter 2 基本原理

    2024-07-13 04:54:02       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 04:54:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 04:54:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 04:54:02       58 阅读
  4. Python语言-面向对象

    2024-07-13 04:54:02       69 阅读

热门阅读

  1. 系统Doze白名单常用接口

    2024-07-13 04:54:02       21 阅读
  2. 小试epoll

    2024-07-13 04:54:02       26 阅读
  3. HTTP模块

    2024-07-13 04:54:02       23 阅读
  4. git diff,stash,submodule,format-patch

    2024-07-13 04:54:02       27 阅读
  5. linux系统安全加固

    2024-07-13 04:54:02       19 阅读
  6. ACE之ACE_Time_Value

    2024-07-13 04:54:02       23 阅读
  7. 力扣 150题 逆波兰表达式求值 记录

    2024-07-13 04:54:02       30 阅读
  8. cin和getline的区别

    2024-07-13 04:54:02       22 阅读
  9. STM32F103RC使用HAL库配置USART进行数据收发

    2024-07-13 04:54:02       27 阅读
  10. 07-7.5.3 处理冲突的方法

    2024-07-13 04:54:02       24 阅读
  11. Vue的import什么时候用大括号

    2024-07-13 04:54:02       22 阅读
  12. Spring Boot 框架知识汇总

    2024-07-13 04:54:02       25 阅读
  13. SpringBoot源码阅读(11)——后处理器2

    2024-07-13 04:54:02       23 阅读
  14. redis的发布与订阅

    2024-07-13 04:54:02       24 阅读
  15. Vue Router 4:构建高效单页面应用的路由管理

    2024-07-13 04:54:02       24 阅读
  16. c++【入门】病狗问题

    2024-07-13 04:54:02       21 阅读