Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 5.3
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- cout << "CRISTIAN CUEVAS \n";
- cout << "\nActivity 1 \n==========\n";
- // Change the following do-while loop to a while loop. //DONE
- int inputNum;
- while(inputNum != 0){
- cout << "Enter a number (or 0 to quit): ";
- cin >> inputNum;
- }
- cout << "\nActivity 2 \n==========\n";
- // Change the following while loop to a do-while loop. //DONE
- char doAgain = 'y';
- do {
- cout << "Do you want to loop again? (y/n) ";
- cin >> doAgain;
- }
- while(doAgain == 'Y' || doAgain == 'y');
- cout << "\nActivity 3 \n==========\n";
- // Change the following while loop to a for loop. //DONE
- int count = 0;
- for (count=0;count<5;count++){
- cout << "Count is " << count << endl;
- }
- cout << "\nActivity 4 \n==========\n";
- // Change the following for loop to a while loop.
- int x=5;
- while(x>0){
- cout << x << " seconds to go. \n";
- x--;
- }
- cout << "\nActivity 5 \n==========\n";
- // Make the following changes to the code below that uses nested loops:
- // 1. The code is supposed to print 3 lines with a $ and 5 stars on
- // each line, but it contains a logic error. Find and fix the error.
- // 2. Then revise the code to follow each $ with just 4 stars, like this:
- // $****
- // $****
- // $****
- // 3. Change the two loop control variable names to be more descriptive.
- for (int counterS = 1 ; counterS <= 3; counterS++)
- {
- cout << '$';
- for (int counterStar = 1; counterStar<= 4; counterStar++)
- cout << '*';
- cout<< endl;
- }
- return 0;
- }
- 5.4
- // Lab 5 - cookies.cpp
- // This program finds the average number of boxes of cookies
- // sold by the children in a particular scout troop.
- // It illustrates the use of a counter, an accumulator,
- // and an end sentinel.
- // CRISTIAN CUEVAS
- #include <iostream>
- using namespace std;
- int main()
- {
- int numBoxes, // Number of boxes of cookies sold by one child
- totalBoxes, // Accumulates total boxes sold by the entire troop
- numSeller; // Counts the number of children selling cookies
- double averageBoxes; // Average number of boxes sold per child
- // WRITE CODE TO INITIALIZE THE totalBoxes ACCUMLATOR TO 0 AND
- // THE numSeller COUNTER TO 1.
- totalBoxes = 0;
- numSeller = 1;
- cout << " **** Cookie Sales Information **** \n\n";
- // Get the first input
- cout << "Enter number of boxes of cookies sold by seller " << numSeller
- << " (or -1 to quit): ";
- cin >> numBoxes;
- // WRITE CODE TO START A while LOOP THAT LOOPS WHILE numBoxes
- // IS NOT EQUAL TO -1, THE SENTINEL VALUE.
- while(numBoxes!= -1)
- {
- // WRITE CODE TO ADD numBoxes TO THE totalBoxes ACCUMULATOR.
- totalBoxes += numBoxes;
- // WRITE CODE TO ADD 1 TO THE numSeller COUNTER.
- numSeller++;
- // WRITE CODE TO PROMPT FOR AND INPUT THE NUMBER OF BOXES
- // SOLD BY THE NEXT SELLER.
- cout << "Enter the boxes from the next seller"<< endl;
- cin>> numBoxes;
- while (numBoxes < -1){
- cout<< "Wrong number \n";
- cout << "Enter the boxes from the next seller"<< endl;
- cin>> numBoxes;}
- }
- // WHEN THE LOOP IS EXITED, THE VALUE STORED IN THE numSeller COUNTER
- // WILL BE ONE MORE THAN THE ACTUAL NUMBER OF SELLERS. SO WRITE CODE
- // TO ADJUST IT TO THE ACTUAL NUMBER OF SELLERS.
- numSeller = numSeller-1;
- if (numSeller == 0) // If true, -1 was the very first entry
- cout << "\nNo boxes were sold.\n";
- else
- { // WRITE CODE TO ASSIGN averageBoxes THE COMPUTED AVERAGE NUMBER
- // OF BOXES SOLD PER SELLER.
- // WRITE CODE TO PRINT OUT THE NUMBER OF SELLERS AND AVERAGE NUMBER
- // OF BOXES SOLD PER SELLER.
- averageBoxes = (double)totalBoxes/numSeller;
- cout << "The average number of boxes sold by the " << numSeller << " sellers was "<< averageBoxes <<endl;
- }
- return 0;
- }
- 5.5
- #include <iostream>
- #include <string>
- #include <cmath>
- using namespace std;
- int main()
- {
- // DEFINE THE NAMED CONSTANT PI HERE AND SET ITS VALUE TO 3.14159
- double pi = 3.14159;
- int choice;
- double radius;
- do{
- cout << "Program to calculate areas of objects\n" << "1 --square\n" << "2 --circle\n" << "3--right triangle\n" << "4--quit" << endl;
- choice=0;
- cout << "ENTER YOUR CHOICE " << endl;
- cin >> choice;
- switch (choice)
- {
- case 1:
- cout << "Radius of square: ";
- cin >> radius;
- cout << "Area = " << pow(radius, 2);
- cout<< "\n\n";
- break;
- case 2:
- cout << "Radius of circle: ";
- cin >> radius;
- cout << "Area = " << pi * pow(radius,2);
- cout<< "\n\n";
- break;
- case 3:
- cout << "Radius of right triangle: ";
- double a, b;
- cout << "Enter a and b separated by a space" << endl;
- cin >> a >> b;
- cout << "Area = " << (a*b) / 2 << endl;
- cout<< "\n\n";
- break;
- case 4:
- cout << " BYE ";
- return 0;
- break;
- default:
- cout << "WRONG CHOICE SELECTED, TRY AGAIN";
- break;
- }
- } while(choice!=4);
- 5.6
- #include <iostream>
- #include <cmath>
- #include <string>
- using namespace std;
- int main()
- {
- int value,
- denom,
- finalDenom;
- double sum = 0.0;
- cout << "CRISTIAN CUEVAS \n";
- cout << "This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n \n";
- cout << "\n\Enter your value for the multiple ";
- cin >> value;
- while (value < 2 || value > 10){
- cout << "Invalid input. "
- << " Enter a positive integer between 2 and 10: ";
- cin >> value;
- }
- cout << endl;
- sum = 0.0;
- // Compute the denominator of the final term
- finalDenom = int (pow(2.0, value));
- // Loop once for each term
- for (denom = 2; denom <= finalDenom; denom *=2)
- {
- // Print the term
- cout << "1/" << denom;
- if (denom < finalDenom)
- cout << " + ";
- else
- cout << " = ";
- // Add the value of the term to the accumulator
- sum = sum + 1.0 / denom;
- }
- // Print the answer
- cout << sum << endl << endl;
- cout << "GOOD BYE";
- return 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement