自学内容网 自学内容网

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;
}

原文地址:https://blog.csdn.net/weixin_35193147/article/details/140389790

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!