C ++ Primer Chapter 5 after-school programming questions, primer Chapter 5

Source: Internet
Author: User

C ++ Primer Chapter 5 after-school programming questions, primer Chapter 5


1. Code

# Include <iostream> int main () {using namespace std; int num1; int num2; int total = 0; cout <"Enter the start number \ n "; cin> num1; cout <"Enter the end number \ n"; cin> num2; for (num1; num1 <= num2; num1 ++) total = num1 + total; the integer between cout <num1 <"and" <num2 <"and is <total <endl; return 0 ;}
Running result

2. Code
# Include <iostream> int main () {using namespace std; double total = 0.0; double in; cout <"enter a number:"; cin> in; while (in! = 0) {total + = in; cout <"sum of all input numbers:" <total <"\ n"; cout <"enter the next number: "; cin> in;} cout <" Running ended "; return 0 ;}
Running result

3. Code
# Include <iostream> int main () {using namespace std; double daphne = 100; double cleo = 100; int year = 1; while (daphne> = cleo) {daphne + = 100*0.1; cleo + = cleo * 0.05; cout <"year <, the investment value of daphne is "<daphne <" cleo investment value is "<cleo <endl; year ++ ;} cout <"no." <year <", when cleo exceeds the investment value of daphne" <endl; return 0 ;}
Running result

4. Code
# Include <iostream> const char * months [12] = ", "August", "August"}; int main () {using namespace std; int sales [12]; int total; for (int I = 1; I <= 12; I ++) {cout <"enter" <months [I-1] <"sales quantity: "; cin> sales [I-1];} for (int j = 0; j <12; j ++) {total = total + sales [j];} cout <"total sales:" <total <endl; return 0 ;}
Running result

5. Code
# Include <iostream> const char * months [12] = ", "July", "August", "September", "October", "November", "December"}; int main () {using namespace std; int sales [3] [12]; int total [3] = {0}; // Initialization is required. Otherwise, the initial value is not 0 int sum; for (int a = 1; a <= 3; a ++) {for (int I = 1; I <= 12; I ++) {cout <"Enter the" <a <"year" <months [I-1] <"sales quantity :"; cin> sales [A-1] [I-1] ;}for (int B = 0; B <3; B ++) {for (int j = 0; j <12; j ++) {total [B] = total [B] + sales [B] [j];} sum = sum + total [B]; cout <"no." <B + 1 <"total annual sales:" <total [B] <endl ;}cout <"total sales: "<sum <endl; return 0 ;}
Running result

6. Code
<Pre name = "code" class = "cpp"> // you must add while (cin. get ()! = '\ N'); # include <iostream> using namespace std; const int LEN = 60; struct Car {char brand [LEN]; int year ;}; int main () {int num; cout <"How many cars do you wish to catalog? "; Cin> num; while (cin. get ()! = '\ N'); Car * ps = new Car [num]; for (int I = 0; I <num; I ++) {cout <"Car #" <(I + 1) <": \ n"; cout <"Please enter the make:"; cin. getline (ps [I]. brand, LEN); cout <"Please enter the year made:"; cin> ps [I]. year; while (cin. get ()! = '\ N');} cout <"Here is your collection: \ n"; for (int I = 0; I <num; I ++) {cout <ps [I]. year <"" <ps [I]. brand <endl;} delete [] ps; return 0 ;}
Running result 

7. Code
#include<iostream>#include<cstring>const int STR_LEN=60;int main(){  using namespace std;  char words[STR_LEN];  int count=0;  cout << "Enter words(to stop,type the word done):\n";  while (cin >> words && strcmp("done", words))    ++count;  cout << "You entered a total of " << count << " words .\n" <<endl;  return 0;}
Running result

8. Code
#include<iostream>#include<string>int main(){  using namespace std;  string words;  int count=0;  cout << "Enter words {to stop,type the word done}:\n";  while (cin >> words && words != "done")    ++count;  cout << "You entered a total of " << count << " words .\n";  return 0;}
Running result

9. Code
#include<iostream>int main(){  using namespace std;  int row;  cout << "Enter number of row:";  cin >> row;  for (int i=0; i<row; i++)  {    for (int j=row-i; j>1; j--)    {      cout << ".";    }    for (int k=0; k<=i; k++)    {      cout << "*";    }    cout << "\n";  }  return 0;}
Running result



Answers to programming questions after class in the Chinese version of C Primer Plus (fifth edition!

C ++ primer plus (Chinese version) which of the following answers to programming after the fifth edition is shared by Jack? // thanks, my QQ number is 115425796.
 
Hi, hero. Can you send me the answers to C Primer Plus (fifth edition) programming exercises after class? You

I have my own space

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.