Escolar Documentos
Profissional Documentos
Cultura Documentos
2.25 (A) What will the following programs print on the screen?
0
100
Because even though we initialized the variables as freeze = 32, boil = 212, we changed
the values before printing, so the output will be the last value stored in the variables (freeze
= 0, boil = 100)
2.27 There are a number of syntax errors in the following program. Locate as many as you
can.
1- In the first line they use the following format to comment the line:
*/ comment/*
And it has to be in this way:
/* your comment here */
(Starting the comment with backward slash followed by an asterisk and finishing the
comment with an asterisk followed by a backward slash)
Also if you want to comment just one line, its better using // instead of /*
comment */ .
2-
In the second line the preprocessor directive it is written incorrectly, iostream has
to be inside brackets, like this:
#include <iostream>
3- In the fourth line we have another error, because it is written in this way:
int main();
With a semicolon at the end, but the correct way is without the semicolon, like this:
int main()
4- The braces are written in the other way around. The first brace (opening brace) has
to be { and the last brace (closing brace) has to be }
5- They defined the variables a, b and c without using a semicolon at the end of the
statement. Also they forgot to put the semicolon when they initialized the values of
the variables a, b and c. The correct way is the following:
int a,b,c; // Three integers
a=3;
b=4;
c=a+b;
I can see some errors at the moment to print out the value of c. The first one is that
Cout it is written with capital C, and it has to be written in lower case. The second
error in this statement is that they are using just one bracket, and it has to have two
brackets (<<). And the last error is that they are printing the value of the variable
C, which does not exist because it is written with capital C (our variable c is
written in lower case). The correct way to print out the value of c is:
cout<<The value of c is %d<<c;
/*
Homework 1, Programming Languages 29/03/2015
Challengue Problems of the book "Starting Out with C++" 7th edition
2.9 , 2.16 (Chapter 2, Problems 9 and 16)
3.3 , 3.8, 3.17 (Chapter 3, Problems 3,8 and 17
Solved by Jos Giles, Mechatronics student
*/
/* Programming Challenge 2.9 Circuit Board Price
Program to calculate the selling price of a product that cost $12.67 to produce,
with a 40 percent of profit.
You can modify the percent profit and the production cost to adapt this program
to your specific requirements. */
#include <iostream> //Because we are going to print the output to the screen (we are
going to use the input-output stream library)
using namespace std; // Using the standard namespace
int main()
{
float percent_profit = 40.0; //write here the percent profit you will get for every
product sold, for this specific problem we will use 40 %
float production_cost = 12.67; // write here the production cost for each product. For
this problem we will use 12.67, but you can change it depending on your product.
float selling_price = ((percent_profit / 100) + 1)*production_cost; //first we have to
divide the percent profit by 100 because we are working with the percentage
//after that we add 1 because its the unit cost of production. Multiplying this result
by the cost of production we will get the selling price for this product.
cout << "Name: Jose Giles\nID number: 21530021" << endl<<endl;
cout << "The selling price of your product is: " << selling_price << endl; //Print out
the selling price
return 0; // Return 0 if everything is ok
}
Results:
int main()
{
int surveyed_customers = 12467; // The total of surveyed customers. It has to be
integer for obvious reasons
int energy_drinkers__percent = 14; // Percentage of the surveyed customer that
purchase energy drinks, lets supose we will work just with integer values of percentage
int citrus_drinkers_percent=64; //Percentage of the energy drinkers that prefer citrus
flavored drinks
int energy_drinkers=surveyed_customers*energy_drinkers__percent/100; //If we
multiply the surveyed customers by the energy drinks percent we obtain the approximate
number of energy drinkers
//we have to divide by 100 because we are working with percentage values
int
citrus_drinkers=surveyed_customers*citrus_drinkers_percent*energy_drinkers__percent/100
00; //The approximate number of customers that prefer citrus flavored energy drinks.
//(divide by 100 two times (1/100)(1/100)=(1/10000)
cout << "Name: Jose Giles\nID number: 21530021" <<endl<< endl;
cout << "The total number of customers surveyed is: " << surveyed_customers <<
endl;
cout << "The approximate number of customers in the survey who purchase one or
more energy drinks per week: ";
cout<< energy_drinkers << endl;
cout << "The approximate number of customers in the survey who prefer citrus
flavored energy drinks is: ";
cout << citrus_drinkers << endl;
}
return 0;
Results:
const int price_A = 15, price_B = 12, price_C = 9; //We use const data type because
the value of the prices will not change
int num_A, num_B, num_C;
//The number of sold tickets has to be
integer
float total;
//To use the fixed-point notation required by the
problem (data type has to be float or double)
cout << "Name: Jose Giles\nID number: 21530021" << endl << endl;
cout << "Please enter the number of sold tickets for A class " << endl;
cin >> num_A; //We assign the value written by the user to the variable "num_A"
cout << "Please enter the number of sold tickets for B class " << endl;
cin >> num_B; //We assign the value written by the user to the variable "num_B"
cout << "Please enter the number of sold tickets for C class " << endl;
cin >> num_C; //We assign the value written by the user to the variable "num_C"
total = num_A*price_A + num_B*price_B + num_C*price_C;
cout << fixed << showpoint << setprecision(2); // To give the right format(fixedpoint notation with 2 decimal
//points and assuring that the decimal point will be always displayed.
cout <<"Total: "<< total << endl;
}
return 0;
Results:
//points and assuring that the decimal point will be always displayed.
cout << left << setw(35) << "Gross Box Office Profit:" << left << "$" << right <<
setw(10)<<total << endl;
cout << left << setw(33) << "Amount Paid to Distributor:" << left << "- $" <<
right << setw(10)<<money_distributor << endl;
cout << left << setw(35) << "Net Box Office Profit::" << left << "$" << right <<
setw(10)<<money_theater << endl;
return 0;
}
Results
#include <iostream> //Because we are going to print the output to the screen (we are
going to use the input-output stream library)
#include <math.h> //To use pow function
#include<string>
using namespace std;
int main()
{
float principal, interest_rate, total_amount, interest; //Floating type because these
values could have decimal point values
//also because we need to apply the right format to these values (fixed-point
notation with 2 decimal)
int T;
cout << "Name: Jose Giles\nID number: 21530021" << endl << endl;
cout << "Please write the balance of the acccount (Principal)" << endl;
cin >> principal;
cout << "Please insert the anual interest rate " << endl;
cin >> interest_rate;
cout << "Please write the number of times the interest is compounded during a year
" << endl;
cout << "(e.g., write 4 if the interest is compounded quarterly)" << endl;
cin >> T;
float x = 1 + (interest_rate / (100 * T));
total_amount = principal*(pow(x, T)); // pow(base,exponent) Returns base value
raised to the power exponent
interest = total_amount - principal; // This is your profit
total_amount = floor(100 * total_amount) / 100; // I found this weird solution to
display 2 decimal points without rounding
//e.g. 3.777 will be displayed like 3.77 instead of 3.78
interest = floor(100 * interest) / 100;
cout << fixed << showpoint << setprecision(2); //To give format
cout << left << setw(30) << "Interest Rate:" << right << setw(14) <<
interest_rate << "%" << endl;
cout << left << setw(30) << "Times Compounded:" << right << setw(14) << T <<
endl;
cout << left << setw(30) << "Principal:" << left << "$ " << right << setw(12) <<
principal << endl;
cout << left << setw(30) << "Interest:" << left << "$ " << right << setw(12) <<
interest << endl;
cout << left << setw(30) << "Final Balance:" << left << "$ " << right << setw(12)
<< total_amount << endl;
return 0;
}
Results