楼主: Nicolle
333 24

Solutions to Absolute C++ Fifth Edition by Walter Savitch [推广有奖]

版主

巨擘

0%

还不是VIP/贵宾

-

TA的文库  其他...

Python Programming

SAS Programming

Structural Equation Modeling

威望
16
论坛币
12329660 个
学术水平
2838 点
热心指数
2822 点
信用等级
2646 点
经验
433424 点
帖子
18725
精华
83
在线时间
7107 小时
注册时间
2005-4-23
最后登录
2018-11-19

Nicolle 学生认证  发表于 2018-11-8 11:33:24 |显示全部楼层
  1. Absolute C++5th Edition Solutions

  2. This repository is solutions to the problems in Absolute C++ Fifth Edition by Walter Savitch.

  3. The IDE used to program these solutions is: CodeBlocks 16.01.

  4. [Disclaimer: Though I've tried my best to solve the problems as asked by the questions, all the solutions in this repo might not work as expected. That might be because of my limited knowledge and that also has to do a lot with me being just an enthusiastic learner of Computer Programming rather than a professional in this field. Some results produced are affected by the compilers one has used to compile and run the program. One who take references from the code in this repo must be vigilant for such possible conditions/errors during program execution.]

  5. Creator: Roshan Parajuli
复制代码

本帖隐藏的内容

Absolute-Cpp-Solutions-master.zip (32.33 MB)



本帖被以下文库推荐

stata SPSS
Nicolle 学生认证  发表于 2018-11-8 11:36:55 |显示全部楼层
  1. /* Suppose you can buy a chocolate bar from the vending machine for $1 each. Inside
  2. every chocolate bar is a coupon. You can redeem seven coupons for one chocolate
  3. bar from the machine. You would like to know how many chocolate bars you can
  4. eat, including those redeemed via coupon, if you have n dollars.
  5. For example, if you have 20 dollars then you can initially buy 20 chocolate bars.
  6. This gives you 20 coupons. You can redeem 14 coupons for two additional chocolate
  7. bars. These two additional chocolate bars give you two more coupons, so you
  8. now have a total of eight coupons. This gives you enough to redeem for one final
  9. chocolate bar. As a result you now have 23 chocolate bars and two leftover coupons. */
  10. #include <iostream>
  11. using namespace std;
  12. int main(){
  13.     int dollars;
  14.     cout << "Enter the number of dollars: ";
  15.     cin >> dollars;
  16.     int chocolates = dollars;
  17.     int coupons = dollars;
  18.     while(coupons >= 7){
  19.         int extra_chocolates = coupons/7;
  20.         chocolates = chocolates + extra_chocolates;
  21.         int extra_coupons = coupons % 7;
  22.         coupons = (coupons/7) + extra_coupons;
  23.     }
  24.     cout << "\nYou can have: " << chocolates << " chocolates.";
  25.     cout << "\nThe number of coupons is: " << coupons;
  26.     return 0;
  27. }
复制代码
回复

使用道具 举报

hjtoh 发表于 2018-11-8 11:38:28 |显示全部楼层
楼主是编程老手呀
已有 1 人评分论坛币 收起 理由
Nicolle + 20 精彩帖子

总评分: 论坛币 + 20   查看全部评分

回复

使用道具 举报

Nicolle 学生认证  发表于 2018-11-8 11:38:58 |显示全部楼层
2.4

  1. /*Write a program that finds and prints all of the prime numbers between 3 and 100.
  2. A prime number is a number that can only be divided by one and itself (i.e., 3, 5,
  3. 7, 11, 13, 17, �).
  4. One way to solve this problem is to use a doubly-nested loop. The outer loop can
  5. iterate from 3 to 100, while the inner loop checks to see whether the counter value
  6. for the outer loop is prime. One way to decide whether the number n is prime is
  7. to loop from 2 to n - 1 ; if any of these numbers evenly divides n , then n cannot be
  8. prime. If none of the values from 2 to n - 1 evenly divide n , then n must be prime.
  9. (Note that there are several easy ways to make this algorithm more efficient.) */
  10. #include <iostream>
  11. using namespace std;
  12. int main(){
  13.     for(int i=3; i<=100; i++){
  14.         bool prime = true;
  15.         for(int j=2; j<i; j++){
  16.             if((i % j) == 0){
  17.                 prime = false;
  18.                 break;
  19.             }
  20.         }
  21.         if(prime == true){
  22.             cout << endl << i;
  23.         }
  24.     }
  25.     return 0;
  26. }
复制代码
回复

使用道具 举报

Nicolle 学生认证  发表于 2018-11-8 11:40:51 |显示全部楼层
2.5
  1. /*In cryptarithmetic puzzles, mathematical equations are written using letters. Each
  2. letter can be a digit from 0 to 9, but no two letters can be the same. Here is a
  3. sample problem:
  4. SEND + MORE = MONEY
  5. A solution to the puzzle is S = 9, R = 8, O = 0, M = 1, Y = 2, E = 5, N = 6, D = 7
  6. Write a program that fi nds solutions to the following cryptarithmetic puzzle:
  7. TOO + TOO + TOO + TOO = GOOD
  8. The simplest technique is to use a nested loop for each unique letter (in this case T,
  9. O, G, D). The loops would systematically assign the digits from 0�9 to each letter.
  10. For example, it might first try T = 0, O = 0, G = 0, D = 0, then T = 0, O = 0, G =0,
  11. D = 1, then T = 0, O = 0, G = 0, D = 2, etc. up to T = 9, O = 9, G = 9, D = 9.
  12. In the loop body, test that each variable is unique and that the equation is satisfied.
  13. Output the values for the letters that satisfy the equation. */
  14. #include <iostream>
  15. using namespace std;
  16. int main(){
  17.     int T, O, G, D;
  18.     for(int T = 0; T<=9; T++){
  19.         for(int O =0; O<=9; O++){
  20.             for(int G=0; G<=9; G++){
  21.                 for(int D=0; D<=9; D++){
  22.                     if(T == O || T == G || T == D || O == G || O == D || G == D){
  23.                         continue;
  24.                     }
  25.                     else if(400* T + 40 * O + 4 * O == 1000 * G + 100 * O + 10 * O + D){
  26.                         cout << endl << "T: " << T;
  27.                         cout << endl << "O: " << O;
  28.                         cout << endl << "G: " << G;
  29.                         cout << endl << "D: " << D;
  30.                         cout << endl;
  31.                         break;
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.     }
  37.     return 0;
  38. }
  39. //Hint From: http://www.rpgcodex.net/forums/index.php?threads/cryptarithmetic-puzzle-and-c.75483/
复制代码
回复

使用道具 举报

Nicolle 学生认证  发表于 2018-11-8 11:42:02 |显示全部楼层
2.6
  1. /*Buoyancy is the ability of an object to float. Archimedes� Principle states that the
  2. buoyant force is equal to the weight of the fluid that is displaced by the submerged
  3. object. The buoyant force can be computed by
  4. Fb = V * g
  5. where Fb
  6. is the buoyant force, V is the volume of the submerged object, and g is
  7. the specific weight of the fluid. If Fb
  8. is greater than or equal to the weight of the
  9. object, then it will float, otherwise it will sink.
  10. Write a program that inputs the weight (in pounds) and radius (in feet) of a sphere
  11. and outputs whether the sphere will sink or float in water. Use g = 62.4 lb/ft 3 as
  12. the specific weight of water. The volume of a sphere is computed by (4/3)pr3 . */
  13. #include <iostream>
  14. #include <cmath>
  15. using namespace std;
  16. int main(){
  17.     double const PI = 3.1416;
  18.     double const specific_weight_of_liquid = 62.4;
  19.     double weight;
  20.     double radius;
  21.     cout << "Enter the weight of sphere in pounds: ";
  22.     cin >> weight;
  23.     cout << "\nEnter the radius of sphere in feet: ";
  24.     cin >> radius;
  25.     double volume;
  26.     volume = (4/3)*PI*pow(radius, 3);
  27.     double buoyant_force;
  28.     buoyant_force = volume * specific_weight_of_liquid;
  29.     if(buoyant_force >= weight){
  30.         cout << "\nThe sphere will float in the liquid.";
  31.     }
  32.     else{
  33.         cout << "\nThe sphere will sink in the liquid.";
  34.     }
  35.     cout << endl;
  36.     return 0;
  37. }
复制代码
回复

使用道具 举报

Nicolle 学生认证  发表于 2018-11-8 11:42:54 |显示全部楼层
2.7

  1. /*Write a program that calculates the total grade for N classroom exercises as a percentage.
  2. The user should input the value for N followed by each of the N scores
  3. and totals. Calculate the overall percentage (sum of the total points earned divided
  4. by the total points possible) and output it as a percentage. Sample input and output
  5. are shown as follows:
  6. How many exercises to input? 3
  7. Score received for exercise 1: 10
  8. Total points possible for exercise 1: 10
  9. Score received for exercise 2: 7
  10. Total points possible for exercise 2: 12
  11. Score received for exercise 3: 5
  12. Total points possible for exercise 3: 8
  13. Your total is 22 out of 30, or 73.33%. */
  14. #include <iostream>
  15. using namespace std;
  16. int main(){
  17.     int number_of_exercises;
  18.     cout << "How many exercises to input?";
  19.     cin >> number_of_exercises;
  20.     double obtained = 0;
  21.     double total = 0;
  22.     for(int i=1; i<=number_of_exercises; i++){
  23.         cout << "\nScore received for exercise " << i << " :";
  24.         double score;
  25.         cin >> score;
  26.         obtained += score;
  27.         cout << "\nTotal points possible for exercise " << i << " :";
  28.         double total_points;
  29.         cin >> total_points;
  30.         total += total_points;
  31.     }
  32.     cout << "\nYour total is " << obtained << " out of " << total <<
  33.         ", or " << (static_cast<double>(obtained)/total)*100 << "%";
  34.     cout << endl;
  35.     return 0;
  36. }
复制代码
回复

使用道具 举报

Nicolle 学生认证  发表于 2018-11-8 11:44:03 |显示全部楼层
2.8

  1. /*Write a program that finds the temperature, as an integer, that is the same in
  2. both Celsius and Fahrenheit. The formula to convert from Celsius to Fahrenheit
  3. is as follows:
  4. Fahrenheit = 9
  5. 5
  6. Celsius + 32
  7. Your program should create two integer variables for the temperature in Celsius
  8. and Fahrenheit. Initialize the temperature to 100 degrees Celsius. In a loop, decrement
  9. the Celsius value and compute the corresponding temperature in Fahrenheit
  10. until the two values are the same. */
  11. #include <iostream>
  12. using namespace std;
  13. int main(){
  14.     int fahrenheit;
  15.     int celcius = 100;
  16.     int temperature = 0;
  17.     bool found = false;
  18.     while(found == false){
  19.         fahrenheit = ((9/static_cast<double>(5))*celcius)+32;
  20.         if(fahrenheit == celcius){
  21.             found = true;
  22.             temperature = temperature + celcius;
  23.             break;
  24.         }
  25.         celcius--;
  26.     }
  27.     cout << temperature;
  28.     return 0;
  29. }
复制代码
回复

使用道具 举报

Nicolle 学生认证  发表于 2018-11-8 11:46:31 |显示全部楼层
2.9

  1. /*(This is an extension of an exercise from Chapter 1 .) The Babylonian algorithm to
  2. compute the square root of a positive number n is as follows:
  3. 1. Make a guess at the answer (you can pick n/2 as your initial guess).
  4. 2. Compute r = n / guess .
  5. 3. Set guess= (guess + r) / 2.
  6. 4. Go back to step 2 for as many iterations as necessary. The more steps 2 and 3 are
  7. repeated, the closer guess will become to the square root of n.
  8. Write a program that inputs a double for n, iterates through the Babylonian
  9. algorithm until the guess is within 1% of the previous guess, and outputs the
  10. answer as a double to two decimal places. Your answer should be accurate even for
  11. large values of n. */
  12. #include <iostream>
  13. #include <cmath>
  14. using namespace std;
  15. int main(){
  16.     double n = 45369;
  17.     double guess;
  18.     cout << "Enter your guess: ";
  19.     cin >> guess;
  20.     bool result = false;
  21.     double r;
  22.     double current_guess;
  23.     while(result == false){
  24.         r = n/guess;
  25.         current_guess = guess;
  26.         guess = (guess + r)/static_cast<double>(2);
  27.         if((((guess > current_guess) ? guess-current_guess : current_guess) - guess) <= 0.01*current_guess){
  28.             cout << endl << guess;
  29.             result = true;
  30.         }
  31.     }
  32.     return 0;
  33. }
复制代码
回复

使用道具 举报

igs816 在职认证  发表于 2018-11-8 11:57:03 |显示全部楼层
这不是书吧
已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 我要注册

GMT+8, 2018-11-20 00:10