1. Both main memory and secondary storage are types of memory. Describe the difference
between the two.
2. What is the difference between system software and application software?
3. Why must programs written in a high-level language be translated into machine
language before they can be run?
4. In C++, to display data on your monitor you use the << operator. What do you call this
operator?
5. What is wrong with this program and how you fix it so it compiles?
int main()
}
// A crazy mixed up program
return 0;
#include <iostream>
cout << "In 1942 Columbus sailed the ocean blue.";
{
using namespace std;
6. A program has the following variable definitions.
long miles;
int feet;
float inches;
Write one cin statement that reads a value into each of these variables.
7. Write C++ statements using combined assignment operators to perform the following:
a) Add 6 to x
b) subtract 4 from amount
c) Multiply y by 4
d) divide total by 27
e) store in x the remainder of x divided by 7
8. Complete the following table by writing the value of each expression in the Value column
EXPRESSION VALUE
----------- -----
28/4 - 2
6 + 12 * 2 -8
4 + 8 * 2
6 + 17 % 3 - 2
2 + 22 * (9 - 7)
9. The following program ues an if/else if statement to assign a letter grade
(A,B,C,D, or F) to a numeric test score. THE PROGRAM HAS ERRORS. Find as
many as you can.
#include <iostream>
using namespace std;
int main() {
int testscore;
cout << "Enter your test score and I will tell you \n";
cout << "the letter grade you earned: ";
cin >> testscore;
if (testscore < 60)
cout << "your grade is F.\n";
else if (testscore < 70)
cout << "your grade is D.\n";
else if (testscore < 80)
cout << "your grade is C.\n";
else if (testscore < 90)
cout << "your grade is B.\n";
else
cout << "THAT IS NOT A VALID SCORE.\n");
else if (testscore <= 100)
cout << your grade is A.\n");
return 0;
}
10. Write a program that asks the user to enter two numbers. The program should use the
conditional operator to determine which number is the smaller and which is the larger.
11. Write a program that reports the contents of a compressed-gas cylinder based
on the first letter of the cylinder's color. The program input is a character
representing the observed color of the cylinder: 'Y' or 'y' for yellow, 'O'
or 'o' for orange, and so on. Cylinder colors and associated contents are as
follows:
Orange ammonia
Brown carbon monoxide
Yellow hydrogen
Green oxygen
Your program should respond to input of a letter other than the first letters
of the given colors with the message, "contents unknown."
12. Write a program that uses a "for" statement to calculate the average of
several integers. Assume that the last value read is the sentinel value
99999. For example, 10 8 7 13 9 9999 indicates that the program should
calculate the average of all the values preceding 9999
13. Assume "value" is an integer variable. If the user enters 3.14 in response to the following
programming statement, what will be stored in value?
int value;
cin >> value;
a) 3.14
b) 3
c) 0
14. You studied Type Casting in Chapter 3. Type casting allows you to perform manual data type
conversion.
Assume the following definitions:
int a = 5, b = 12;
double x = 3.4, z = 9.1;
What are the values of the following expressions?
a) b/a
b) x * a
c) static_cast<double>(b/a)
d) static_cast<double>(b) / a
e) b / static_cast<int>x;
15. cout object provides a way to format the data that is displayed on the screen. To format data,
you include the header file <iomanip>. To set the field width, you use the stream manipulator setw.
See Chapter 3. For example, if you like to display the number 15 in a field of width 5 spaces, you
would write
int number = 15;
cout << setw(5) << number;
This will display the number in a field 5 spaces wide, right-justified.
Write cout statement with stream manipulators that perform the following:
a) Display the number 34.789 in a field of 9 spaces with 2 decimal places of precision
b) Display the number 67 left justified in a field of 7 spaces.
16. Indicate whether the following statements about relational expressions are correct or incorrect.
a) x <= y is the same as y > x
b) x != y is the same as y >= x
c) x >= y is the same as y <= x
17. Rewrite the following code using a do-while statement with no decisions in the loop body:
sum = 0;
for (odd = 1; odd < n; odd = odd + 2)
sum = sum + odd;
18. What three things do you need to do to use a function in your program? Explain each one of them
giving examples.
between the two.
2. What is the difference between system software and application software?
3. Why must programs written in a high-level language be translated into machine
language before they can be run?
4. In C++, to display data on your monitor you use the << operator. What do you call this
operator?
5. What is wrong with this program and how you fix it so it compiles?
int main()
}
// A crazy mixed up program
return 0;
#include <iostream>
cout << "In 1942 Columbus sailed the ocean blue.";
{
using namespace std;
6. A program has the following variable definitions.
long miles;
int feet;
float inches;
Write one cin statement that reads a value into each of these variables.
7. Write C++ statements using combined assignment operators to perform the following:
a) Add 6 to x
b) subtract 4 from amount
c) Multiply y by 4
d) divide total by 27
e) store in x the remainder of x divided by 7
8. Complete the following table by writing the value of each expression in the Value column
EXPRESSION VALUE
----------- -----
28/4 - 2
6 + 12 * 2 -8
4 + 8 * 2
6 + 17 % 3 - 2
2 + 22 * (9 - 7)
9. The following program ues an if/else if statement to assign a letter grade
(A,B,C,D, or F) to a numeric test score. THE PROGRAM HAS ERRORS. Find as
many as you can.
#include <iostream>
using namespace std;
int main() {
int testscore;
cout << "Enter your test score and I will tell you \n";
cout << "the letter grade you earned: ";
cin >> testscore;
if (testscore < 60)
cout << "your grade is F.\n";
else if (testscore < 70)
cout << "your grade is D.\n";
else if (testscore < 80)
cout << "your grade is C.\n";
else if (testscore < 90)
cout << "your grade is B.\n";
else
cout << "THAT IS NOT A VALID SCORE.\n");
else if (testscore <= 100)
cout << your grade is A.\n");
return 0;
}
10. Write a program that asks the user to enter two numbers. The program should use the
conditional operator to determine which number is the smaller and which is the larger.
11. Write a program that reports the contents of a compressed-gas cylinder based
on the first letter of the cylinder's color. The program input is a character
representing the observed color of the cylinder: 'Y' or 'y' for yellow, 'O'
or 'o' for orange, and so on. Cylinder colors and associated contents are as
follows:
Orange ammonia
Brown carbon monoxide
Yellow hydrogen
Green oxygen
Your program should respond to input of a letter other than the first letters
of the given colors with the message, "contents unknown."
12. Write a program that uses a "for" statement to calculate the average of
several integers. Assume that the last value read is the sentinel value
99999. For example, 10 8 7 13 9 9999 indicates that the program should
calculate the average of all the values preceding 9999
13. Assume "value" is an integer variable. If the user enters 3.14 in response to the following
programming statement, what will be stored in value?
int value;
cin >> value;
a) 3.14
b) 3
c) 0
14. You studied Type Casting in Chapter 3. Type casting allows you to perform manual data type
conversion.
Assume the following definitions:
int a = 5, b = 12;
double x = 3.4, z = 9.1;
What are the values of the following expressions?
a) b/a
b) x * a
c) static_cast<double>(b/a)
d) static_cast<double>(b) / a
e) b / static_cast<int>x;
15. cout object provides a way to format the data that is displayed on the screen. To format data,
you include the header file <iomanip>. To set the field width, you use the stream manipulator setw.
See Chapter 3. For example, if you like to display the number 15 in a field of width 5 spaces, you
would write
int number = 15;
cout << setw(5) << number;
This will display the number in a field 5 spaces wide, right-justified.
Write cout statement with stream manipulators that perform the following:
a) Display the number 34.789 in a field of 9 spaces with 2 decimal places of precision
b) Display the number 67 left justified in a field of 7 spaces.
16. Indicate whether the following statements about relational expressions are correct or incorrect.
a) x <= y is the same as y > x
b) x != y is the same as y >= x
c) x >= y is the same as y <= x
17. Rewrite the following code using a do-while statement with no decisions in the loop body:
sum = 0;
for (odd = 1; odd < n; odd = odd + 2)
sum = sum + odd;
18. What three things do you need to do to use a function in your program? Explain each one of them
giving examples.
相关推荐
C++程序设计谭浩强编著 清华大学出版社。 该书是谭编写的C++中比较好的,适合c++入门级别者使用。尤其他其中c向c++的过渡比较好。 《中国高等院校计算机基础教育课程体系规划教材:C++程序设计(第2版)》作者谭浩强深入...
大规模c++程序设计
《C++程序设计》张桦《C++程序设计》张桦《C++程序设计》张桦《C++程序设计》张桦《C++程序设计》张桦《C++程序设计》张桦
C++程序设计范例程序C++程序设计范例程序C++程序设计范例程序C++程序设计范例程序
代写C程序 C++ Linux Unix 数据结构 操作系统
C++程序设计电子讲义C++程序设计电子讲义C++程序设计电子讲义C++程序设计电子讲义
本资源为谭浩强C++程序设计PDF版本,资源分为章节介绍,提取了更加精炼的内容,资料中的源码资源可以在搜索“C++程序设计(谭浩强版+源码)”
c++程序c++程序c++程序c++程序c++程序c++程序c++程序
《C++程序设计原理与实践》假定你的最终目标是学会编写实际有用的程序。以基本概念和基本技术为重点:与传统的C++教材相比,《C++程序设计原理与实践》对基本概念和基本技术的介绍更为深入。这会为你编写有用、正确...
C++程序设计语言, C++11标准,包括1-4部分的所有内容。
C++程序设计教程第二版课后习题答案,C++程序设计教程第二版课后习题答案,C++程序设计教程第二版课后习题答案。
C++ 程序设计 计算器
04737C++程序设计精华.pdf
c++程序包一堆 c++程序包一堆 c++程序包一堆 c++程序包一堆 c++程序包一堆 c++程序包一堆c++程序包一堆 c++程序包一堆c++程序包一堆 c++程序包一堆c++程序包一堆 c++程序包一堆 c++程序包一堆c++程序包一堆 c++程序...
《C++程序设计》考试-阅读程序题.pdf
C++课堂程序大汇总C++课堂程序大汇总C++课堂程序大汇总C++课堂程序大汇总C++课堂程序大汇总C++课堂程序大汇总C++课堂程序大汇总C++课堂程序大汇总C++课堂程序大汇总C++课堂程序大汇总C++课堂程序大汇总C++课堂程序大...
新标准C++程序设计教材答案郭炜.pdf
该资源是C++程序设计教程第二版的课后习题答案,希望对大家有帮助,欢迎下载
C++程序设计_谭浩强_PDF电子书,入门的好书
C++程序设计复习笔记.doc