all codes

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of: 3456 as 3 4 5 6 8030 as 8 0 3 0 2345526 as 2 3 4 5 5 2 6 4000 as 4 0 0 0 -2345 as 2 3 4 5

#include <iostream> #include <cmath> using namespace std; int main() { int num; int digit; int sum = 0; int pwr = 0; cout << "Enter an integer: "; cin >> num; cout << endl; cout << "The digits of " << num << " are: "; if (num < 0) num = -num; //Find the highest power of 10 that divides the number while (num/static_cast<int> (pow(10.0, pwr)) >= 10) pwr++; while (num > 0) { digit = num / static_cast<int> (pow (10.0, pwr)); cout << digit << " "; sum = sum + digit ; num = num % static_cast<int>(pow(10.0, pwr)); pwr--; } if (pwr != -1) while (pwr != -1) //Either num is 0 or there are trailing zeros in num while(pwr != -1) { cout << 0 << " "; pwr--; } cout << endl; cout << "The sum of the digits = " << sum << endl; return 0; }

4-4 The statements in the file main.cpp are in incorrect order. Rearrange the statements so that they prompt the user to input: The shape type (rectangle, circle, or cylinder) The appropriate dimension of the shape. Note: For grading purposes place the cylinder height statement before the radius statement. The program then outputs the following information about the shape: For a rectangle, it outputs the area and perimeter For a circle, it outputs the area and circumference For a cylinder, it outputs the volume and surface area. Use 3.1416 as the constant value for any calculations that may need \piπ.

#include <iostream> #include <cmath> #include <string> #include <iomanip> using namespace std; const double PI = 3.1416; int main() { string shape; double height; double width; double radius; double length; cout << fixed << showpoint << setprecision(2); cout << "Enter the shape type: (rectangle, circle, cylinder) "; cin >> shape; cout << endl; if (shape == "rectangle") { cout << "Enter the length of the rectangle: "; cin >> length; cout << endl; cout << "Enter the width of the rectangle: "; cin >> width; cout << endl; cout << "Area of the rectangle = " << length * width << endl; cout << "Perimeter of the rectangle = " << 2 * (length + width) << endl; } else if (shape == "circle") { cout << "Enter the radius of the circle: "; cin >> radius; cout << endl; cout << "Area of the circle = " << PI * pow(radius, 2.0) << endl; cout << "Circumference of the circle: " << 2 * PI * radius << endl; } else if (shape == "cylinder") { cout << "Enter the height of the cylinder: "; cin >> height; cout << endl; cout << "Enter the radius of the base of the cylinder: "; cin >> radius; cout << endl; cout << "Volume of the cylinder = " << PI * pow(radius, 2.0)* height << endl; cout << "Surface area of the cylinder: " << 2 * PI * radius * height + 2 * PI * pow(radius, 2.0) << endl; } else cout << "The program does not handle " << shape << endl; return 0; }

3-1 Suppose that the file inData.txt, data set 1, contains the following data: Giselle Robinson Accounting 5600 5 30 450 9 75 1.5 The first line contains a person's first name, last name, and the department the person works in. In the second line, the first number represents the monthly gross salary, the bonus (as a percent), and the taxes (as a percent). The third line contains the distance traveled and the traveling time. The fourth line contains the number of coffee cups sold and the cost of each coffee cup. Write statements so that after the program executes, the contents of the file outData.txt are as shown below. If necessary, declare additional variables. Your statements should be general enough so that if the content of the input file changes and the program is run again (without editing and recompiling), it outputs the appropriate results.

#include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; int main() { // Write your main here L l ifstream inFile; ofstream outFile; // declare variables here string fName, lName, depart; double gSalary, bonus, taxRate; double distanceTraveled, travelTime; int numOfCOffeeCupsSold; double coffeeCupCost; inFile.open("inData.txt"); outFile.open("outData.txt"); outFile <<fixed<< showpoint << setprecision(2); inFile >> fName >> lName >> depart; inFile >> gSalary >> bonus >> taxRate; outFile <<"Name: " << fName << " " <<lName <<", " <<"Department: " << depart<< endl; outFile << "Monthly Gross Salary: $" << gSalary << ", " << "Monthly Bonus: " << bonus << "%, " << "Taxes: " << taxRate << "%" << endl; outFile << "Paycheck: $ " << gSalary * (1 + bonus /100) * ( 1- taxRate / 100) << endl; outFile << endl; inFile >> distanceTraveled >> travelTime; outFile << "Distance Traveled: " << distanceTraveled << " miles, " << "Traveling Time: " << travelTime << " hours " << endl; outFile << " Average Speed: " << distanceTraveled / travelTime << " miles per hour" << endl; outFile << endl; inFile >> numOfCOffeeCupsSold >> coffeeCupCost; outFile<< "Number Of Cofee Cups Sold: " << numOfCOffeeCupsSold << ", " << " Cost: $ " << coffeeCupCost << " per cup " << endl; outFile << "Sales Amount = $" << numOfCOffeeCupsSold * coffeeCupCost ; inFile.close(); outFile.close(); return 0; }

3-2 Rearrange the statements in the following order so that the program prompts the user to input: The height of the base of a cylinder The radius of the base of a cylinder The program then outputs (in order): The volume of the cylinder. The surface area of the cylinder

#include <iostream> #include <iomanip> #include <cmath> using namespace std; const double PI = 3.14159; int main() { double height; double radius; cout << fixed << showpoint << setprecision (2) ; cout << "Enter height of the cylinder: "; cin >> height; cout << endl; cout << "Enter the radius of the base of the cylinder: "; cin >> radius; cout <<endl; cout << " Volume of cylinder: " << PI * pow (radius, 2.0) * height << endl; cout << "Surface Area: " << 2 * PI * radius * height + 2 * PI * pow(radius, 2.0) <<endl; return 0; }

3-3 Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds (Note that 1 kilogram equals 2.2 pounds). Format your output with two decimal places.

#include <iostream> #include <iomanip> using namespace std; const double POUNDS_IN_KILOGRAMS = 2.2; int main() { double weightInPounds; double weightInKilograms; cout << fixed << showpoint << setprecision(2) ; cout << "Enter the weight in kilograms: " ; cin >> weightInKilograms; cout << endl; weightInPounds = weightInKilograms * POUNDS_IN_KILOGRAMS; cout << "The equivalent in pounds = " << weightInPounds << endl; return 0;

4-5 In a right triangle, the square of the length of one side is equal to the sum of the squares of the lengths of the other two sides. Write a program that prompts the user to enter the lengths of three sides of a triangle and then outputs a message indicating whether the triangle is a right triangle. If the triangle is a right triangle, output It is a right angled triangle If the triangle is not a right triangle, output It is not a right angled triangle

#include <iostream> #include <iomanip> using namespace std; int main() { // Write your main here double side1, side2, side3; int intside1, intside2, intside3; cout << "Enter the lenghts of the sides of a triangle: "; cin >> side1 >> side2 >> side3; cout << endl; intside1 = static_cast<int>(side1 * 100); intside2 = static_cast<int>(side2 * 100); intside3 = static_cast<int>(side3 * 100); if ( (intside1 * intside1 == (intside2 * intside2 + intside3 * intside3) ) || (intside2 * intside2 == (intside1 * intside1 + intside3 * intside3) ) || (intside3 * intside3 == (intside1 * intside1 + intside2 * intside2) ) ) cout << "It is a right angled triangle" << endl; else cout << "It is not a right angled triangle" << endl; return 0; }

3-4 The following program prompts the user to enter the size of the fertilizer bag, in pounds, the cost of the bag, and the area, in square feet, that can be covered by the bag. The program should output the desired result.

#include <iostream> #include <iomanip> using namespace std; int main() { double cost; double area; double bagSize; cout << fixed << showpoint << setprecision (2); cout << "Enter the amount of fertilizer, in pounds, in one bag: "; cin >> bagSize; cout << endl; cout << "Enter the cost of the " << bagSize << " pound fertilizer bag: "; cin >> cost; cout << endl; cout << "Enter the area, in square feet, that can be " << "fertilized by one bag: "; cin >> area; cout << endl; cout << "The cost of the fertilizer per pound is: $" << cost / bagSize << endl; cout << "The cost of fertilizing per square foot is: $" << cost / area << endl; return 0; }

5-2 The value of π can be approximated by using the following series: The program in main.cpp uses this series to find the approximate value of π. However, the statements are in the incorrect order, and there is also a bug in this program. Rearrange the statements and remove the bug so that this program can be used to approximate π.

#include <iostream> #include <iomanip> using namespace std; int main() { double pi = 0; long i; long n; cout << "Enter the value of n: "; cout << endl; cin >> n; pi = 0; for(i = 0; i < n; i++) { if(i % 2 == 0) pi = pi + (1.0 / (2 * i + 1)); else pi = pi - (1.0 / (2 * i + 1)); } pi = 4 * pi; cout << endl << "pi = " << pi << endl; return 0; }

3-6 Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density. Format your output to two decimal places.

#include <iostream> #include <iomanip> using namespace std; int main() { double mass,density, volume; cout << "Enter mass of the object" <<endl; cin >> mass; cout << endl; cout << " Enter density of object " << endl; cin >> density; cout << endl; cout << fixed << showpoint << setprecision(2); volume = mass / density; cout << " The Object volume is " << volume << endl; return 0; }

Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits. If the user enters more than seven letters, then process only the first seven letters. Also output the - (hyphen) after the third digit. Allow the user to use both uppercase and lowercase letters as well as spaces between words. Moreover, your program should process as many telephone numbers as the user wants. Use the dialpad below for reference:

#include <iostream> #include <string> using namespace std; int main() { char letter; int noOfLetters; char response; cout << " Enter Y/y to convert a telephone number" <<" form letters to digits.\n" <<" enter any other letter to terminate the program." <<endl; cin >> response; while (response=='Y'|| response == 'y') { cout << "Enter a telephone number using only letters: "; cin >> letter; cout << endl; noOfLetters = 0; cout << " The coresponding telephone number is: \n"; while(noOfLetters < 7) { noOfLetters++; switch (letter) { case 'a': case 'b': case 'c': case 'A': case 'B' : case 'C' : cout << '2'; break; case 'd': case 'e': case 'f': case 'D': case 'E': case 'F': cout << '3'; break; case 'g': case 'h': case 'i': case 'G': case 'H': case 'I': cout << '4'; break; case 'j': case 'k': case 'l': case 'J': case 'K': case 'L': cout << '5'; break; case 'm': case 'n': case 'o': case 'M': case 'N': case 'O': cout << '6'; break; case 'p': case 'q': case 'r': case 's': case 'P': case 'Q': case 'R': case 'S': cout << '7'; break; case 't': case 'u': case 'v': case 'T': case 'U': case 'V': cout << '8'; break; case 'w': case 'x': case 'y': case 'z': case 'W': case 'X': case 'Y': case 'Z': cout << '9'; } if ( noOfLetters == 3) cout << "-"; cin >> letter; } cin.ignore(100, '\n'); cout << " \n To process another telephone number, enter Y/y \n" << "Enter any other letter to terminate the program." << endl; cin >> response; cout << endl; } return 0; }

2-3 step1: Write a C++ statement that allows you to use cin, cout, and endl without the prefix std::. step2: Write C++ statement(s) that declare the following variables: num1, num2,num3, and average of type int. step3: Write C++ statements that store 125 into num1, 28 into num2, and -25 into num3. step4 :Write a C++ statement that stores the average of num1, num2, and num3 into average. step 5: Write C++ statement(s) that output the values of num1, num2, num3, and average.

#include <iostream> //include statement(s) using namespace std; //using namespace statement int main() { //variable declaration int num1=125; int num2=28; int num3=-25; //executable statements int average = (num1+num2+num3)/3; cout<<" num1 = " << num1 << endl; cout<<" num2 =" << num2<< endl; cout<< "num3 =" <<num3<< endl; cout<<"average"<< average<< endl; return 0; }

2-5 Consider the following C++ program in which the statements are in the incorrect order. Rearrange the statements so that it prompts the user to input the radius of a circle and outputs the area and circumference of the circle.

#include <iostream> using namespace std; const double PI = 3.14; int main() { double radius; double area; double circumference; cout << "Enter the radius: "; cin >> radius; cout << endl; area = PI * radius * radius; circumference = 2 * PI * radius; cout << "Area = " << area << endl; area = PI * radius * radius; circumference = 2 * PI * radius; cout << "Circumference = " << circumference << endl; return 0;}

4-1 Write a program that prompts the user to input a number. The program should then output the number and a message saying whether the number is positive, negative, or zero.

#include <iostream> using namespace std; int main() { // Write your main here double num; cout << " Enter a number "; cin >> num; cout << endl; cout << " The number you entered is " << num << " , and this is a "; if (num == 0) cout << "zero." << endl; else if ( num > 0 ) cout << "positive number." << endl; else cout << "negative number." << endl; return 0; }

5-3 The program Telephone Digits outputs only telephone digits that correspond to uppercase letters. Rewrite the program so that it processes both uppercase and lowercase letters and outputs the corresponding telephone digit. If the input is something other than an uppercase or lowercase letter, the program must output an appropriate error message (The error message should contain the phrase Invalid input).

#include <iostream> using namespace std; int main() { char letter; cout << "Program to convert letters to their corresponding telephone digits" << endl; cout << "To stop the program press #" << endl; cout << "Enter a letter: " ; cin >> letter; while (letter != '#' && letter >= 'A' && letter <= 'z') { cout << "The letter you entered is: " << letter << endl; cout << "The corresponding telephone digit is: "; if (letter > 'Z') { letter = (int)letter-32; // Convert lowercase to uppercase if required. } switch (letter) { case 'A': case 'B': case 'C': cout << "2" << endl; break; case 'D': case 'E': case 'F': cout << "3" << endl; break; case 'G': case 'H': case 'I': cout << "4" << endl; break; case 'J': case 'K': case 'L': cout << "5" << endl; break; case 'M': case 'N': case 'O': cout << "6" << endl; break; case 'P': case 'Q': case 'R': case 'S': cout << "7" << endl; break; case 'T': case 'U': case 'V': cout << "8" << endl; break; case 'W': case 'X': case 'Y': case 'Z': cout << "9" << endl; break; default: break; } cout << "Enter another letter to find out the number: "; cin >> letter; } return 0; }

2-2 Write a program that produces the following output: CCCCCCCCC ++ ++ CC ++ ++ CC ++++++++++++++ +++++++++++++++ CC ++++++++++++++ +++++++++++++++ CC ++ ++ CCCCCCCCC ++ ++

#include <iostream> using namespace std; int main() { cout <<"CCCCCCCCC ++ ++" << endl; cout <<"CC ++ ++" << endl; cout <<"CC ++++++++++++++ +++++++++++++++" << endl; cout <<"CC ++++++++++++++ +++++++++++++++" << endl; cout <<"CC ++ ++" << endl; cout <<"CCCCCCCCC ++ ++" << endl; return 0; }

2-1 ********************************** * Programming Assignment 1 * * Computer Programming I * * Author: ??? * * Due Date: Thursday, Jan. 24 * ********************************** In your program, substitute ??? with your own name. If necessary, adjust the positions and the number of the stars to produce a rectangle.

#include <iostream> using namespace std; int main() { cout<<"**********************************" << endl; cout <<"* Programming Assignment 1 *" << endl; cout <<"* Computer Programming I *" << endl; cout <<"* Author: Miguel Soto *" << endl; cout <<"* Due Date: Thursday, Jan. 24 *" << endl; cout<<"**********************************" << endl; return 0;}

4-2 Write a program that prompts the user to input three numbers. The program should then output the numbers in ascending order, separated by a single space.

#include <iostream> using namespace std; int main() { // Write your main here double num1, num2, num3; double temp; cout << " Enter three numbers "; cin >> num1 >> num2 >> num3; cout << endl; if (num1 > num2) { temp= num1; num1 = num2; num2 = temp; } cout << " The numbers are in assending order are: "; if ( num3 <= num1) cout << num3 << " " << num1 << " " << num2 << endl; else if ( num1 <= num3 && num3 <= num2 ) cout << num1 << " " << num3 << " " << num2 << endl; else cout << num1 << " " << num2 << " " << num3 << endl; return 0; }

4-3 Write a program that prompts the user to input an integer between 0 and 35. The prompt should say Enter an integer between 0 and 35:. If the number is less than or equal to 9, the program should output the number; otherwise, it should output: A for 10 B for 11 C for 12 . . . and Z for 35. (Hint: For numbers >= 10, calculate the ACSII value for the corresponding letter and convert it to a char using the cast operator, static_cast<char>().)

#include <iostream> using namespace std; int main() { // Write your main here int num; cout << " Enter an integer between 0 and 35: " ; cin >> num; cout << endl; cout << " The number you entered is: " << num << endl; cout << " Output: "; if(num <=9 ) cout << num << endl; else cout << static_cast<char>(num + 55) << endl; return 0; }

2-4 Write C++ statement(s) that declare the following variables: num1, num2,num3, and average of type double. Write C++ statements that store 75.35 into num1, -35.56 into num2, and 15.76 into num3. Write a C++ statement that stores the average of num1, num2, and num3 into average. Write C++ statement(s) that output the values of num1, num2, num3, and average.

#include<iostream> //include statement(s) //using namespace statement using namespace std; int main() { //variable declaration double num1=75.35; double num2=-35.56; double num3=15.76; double average = (num1+num2+num3) /3; //executable statements cout<<"num1= "<<num1<<endl; cout<<"num2= "<<num2<<endl; cout<<"num3= "<<num3<<endl; cout<<"average= "<<average<<endl; //return statement return 0; }

4-6 A box of cookies can hold 24 cookies, and a container can hold 75 boxes of cookies. Write a program that prompts the user to enter: The total number of cookies The program then outputs: The number of boxes and the number of containers to ship the cookies. Note that each box must contain the specified number of cookies, and each container must contain the specified number of boxes. If the last box of cookies contains less than the number of specified cookies, you can discard it and output the number of leftover cookies. Similarly, if the last container contains less than the number of specified boxes, you can discard it and output the number of leftover boxes.

#include<iostream> using namespace std; int main() { int Numofcookies , NumofcookiesinBox , Numofcookieboxesincontainer , a , b ,extracookies , extracookiesboxes , totalcookiesbox , totalcookiesboxesincontainer; cout << "enter the Num of cookies:"; cin >> Numofcookies; NumofcookiesinBox = 24; totalcookiesbox = Numofcookies/NumofcookiesinBox; cout << "total cookies boxes = " << totalcookiesbox<<endl; a = totalcookiesbox*24; extracookies = Numofcookies-a; cout << "extra cookies leftover =" << extracookies << endl; Numofcookieboxesincontainer = 75; totalcookiesboxesincontainer = totalcookiesbox/Numofcookieboxesincontainer; cout<<"total cookies boxes in container =" << totalcookiesboxesincontainer<<endl; b = totalcookiesboxesincontainer*75; extracookiesboxes = totalcookiesbox-b; cout<<"extra cookies boxes leftover="<<extracookiesboxes; return 0; }

2-6 Write C++ statements that declare the following variables: name of type string and studyHours of type double. Write C++ statements that prompt and input a string into name and a double value into studyHours. Write a C++ statement that outputs the values of name and studyHours with the appropriate text. For example, if the value of name is "Donald" and the value of studyHours is 4.5, the output is:

//include statement(s) #include <iostream> #include <string> //using namespace statement using namespace std; int main() { //variable declaration string name; double studyHours; //cin >> name= miguel ; //executable statements cout << " Enter your name: " << endl; cin >> name; cout << "study hours ?" << endl; cin >> studyHours; cout << "Hello, " <<name << "! On Saturday, you need to study" <<studyHours<< " hours for the exam." <<endl; //return statement return 0; }

2-7 Write a program that prompts the user to input the decimal value of a double and outputs the number rounded to the nearest integer

include <iostream> using namespace std; int main() { // Write your main here double number; cout <<" enter decimal number: "; cin >> number; cout << endl; cout <<" The integer nearest to " << number <<" = " << static_cast<int>(number + 0.5)<<endl; return 0; }


Kaugnay na mga set ng pag-aaral

Macroeconomics Unit Five: Stabilization Policies and Economic Growth

View Set

Chapter 17/4: Florida Life Regulations

View Set

Chemistry Topic 4 - Exam Style Questions

View Set

Intro to Politics Ch4, 5, 11, 12 (Test 2)

View Set

BMGT quiz 3 smartbook assignments

View Set

Chapter 13 Food Safety and Technology

View Set