[C ++ primer plus] [learning C ++ from Chapter 2] -- 4. programming exercises

Source: Internet
Author: User

Programming exercises

1. Write a C ++ program that displays your name and address.

// file-name: 2-7-1.cpp// Description: Display your name and address#include <iostream>int main(void){  using namespace std;    cout << "Name: xiao" << endl;  cout << "Address: ysu" << endl;    return 0;}

2. Write a C ++ program that asks for a distance in furlongs and converts it to yards. (One

Furlong is 220 yards .)

// file-name: 2-7-2.cpp// Description: Ask for a distance in furlongs and convert it to yard//              One forlong is 220 yards#include <iostream>int main(void){  using namespace std;  const int YardsPerForlong = 220;  int furlongs;  cout << "Distance in furlongs:";  cin >> furlongs;  cout << "Distance in yards is " << furlongs* YardsPerForlong << ".\n";  test();    return 0;}


3. Write a C ++ program that uses three user-defined functions (counting main () as one)
And produces the following output:
Three Blind Mice
Three Blind Mice
See how they run
See how they run
One function, called two times, shocould produce the first two lines, and the remaining

Function, also called twice, showould produce the remaining output.

// FileName: 2-7-3.cpp// Description: Output four sentences,two functions,  two sentences every function#include<iostream>void printBlind();void printFun();int main ( void ){  using namespace std;  printBlind();  printBlind();  printFun();  printFun();    return 0;}void printBlind(){  using namespace std;    cout << "Three blind mice" << endl;}void printFun(){  using namespace std;  cout << "See how the fun\n";}

4. Write a program that has main () call a user-defined function that takes a Celsius Temperature
Value as an argument and then returns the equivalent Fahrenheit value. The program
Shocould request the Celsius value as input from the user and display the result,
Shown in the following code:
Please enter a Celsius value: 20
20 degrees Celsius is 68 degrees Fahrenheit.
For reference, here is the formula for making the conversion:

Fahrenheit = 1.8 × degrees Celsius + 32.0

// FileName: 2-7-4.cpp// Description: Convert celsium to farenheit ( farenheit = 1.8 * celsium + 32.0 )#include <iostream>double convertCelToFar ( double celsium );int main ( void ){  using namespace std;    double celsium;    cout << "Please enter a Celsius value : ";  cin >> celsium;    cout.setf(ios_base::fixed, ios_base::floatfield);  cout.precision(0);  cout << celsium << " degree Celsius is " << convertCelToFar(celsium) << " degree Fahrenheit\n";    return 0;}double convertCelToFar ( double celsium ){  return 1.8 * celsium + 32.0;}

5. Write a program that has main () call a user-defined function that takes a distance in
Light years as an argument and then returns the distance in ast1_mical units. The program
Shocould request the light year value as input from the user and display the result,
Shown in the following code:
Enter the number of lights years: 4.2
4.2 light years = 265608 ast1_mical units.
An ast1_mical unit is the average distance from the Earth to the Sun (about
150,000,000 or 93,000,000 miles), and a light year is the distance light travels in
Year (about 10 trillion kilometers or 6 trillion miles). (The nearest star after the sun is
About 4.2 light years away.) Use Type Double (as in listing 2.4) and this conversion
Factor:

1 light year = 63,240 ast1_mical units

// FileName:2-7-5.cpp// Description:Convert light years to Astronomical Unit#include <iostream>double lightYearToAstroUnit ( double lightYear );int main ( void ){  using namespace std;  double lightYear;  cout << "Enter lightYear: ";  cin >> lightYear;  cout << lightYear << " light year = " << lightYearToAstroUnit ( lightYear ) << " Astronomical Unit\n";  return 0;}double lightYearToAstroUnit ( double lightYear ){  const int AstroUnitPerLightYear = 63240;  return lightYear * AstroUnitPerLightYear;}

6. Write a program that asks the user to enter an hour value and a minute value.
Main () function shocould then pass these two values to a type void function that displays
The two values in the format shown in the following sample run:
Enter the number of hours: 9
Enter the number of minutes: 28

Time: 9: 28

// FileName: 2-7-6.cpp// Decription: Write a function to Display time in format:xx:yy#include <iostream>void displayTime (int hour, int minute);int main (void){  using namespace std;  int hour, minute;  cout << "Enter the number of hours: ";  cin >> hour;  cout << "Enter the number of minute: ";  cin >> minute;  displayTime (hour, minute);  return 0;}void displayTime (int hour, int minute){  using namespace std;    cout << "Time: " <<  hour <<":" << minute << endl;  }


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.