Sunday, 9 December 2018

Starting out with C++; Early Objects 8th Edition | PDF | Original | Tony Gaddis | Judy Walters | Godfrey Muganda | Uploaded by Asad Ali


Starting out with C++; Early Objects 8th Edition .pdf

File Name : Starting Out With C Early Objects 8th Edition .pdf

Size: 16.8 MB

Edition:8th

Author: Tony Gaddis, Judy Walters, Godfrey Muganda

Publisher:Pearson

Release Date:March 2013



Download and Enjoy For Free!

Contents

Preface xv
CHAPTER 1 Introduction to Computers and Programming 1
CHAPTER 2 Introduction to C++ 27
CHAPTER 3 Expressions and Interactivity 77
CHAPTER 4 Making Decisions 155
CHAPTER 5 Looping 243
CHAPTER 6 Functions 323
CHAPTER 7 Introduction to Classes and Objects 407
CHAPTER 8 Arrays 503
CHAPTER 9 Searching, Sorting, and Algorithm Analysis 595
CHAPTER 10 Pointers 637
CHAPTER 11 More About Classes and Object-Oriented Programming 695
CHAPTER 12 More on C-Strings and the string Class 789
CHAPTER 13 Advanced File and I/O Operations 837
CHAPTER 14 Recursion 899
CHAPTER 15 Polymorphism and Virtual Functions 933
CHAPTER 16 Exceptions, Templates, and the Standard Template Library (STL) 971
CHAPTER 17 Linked Lists 1021
CHAPTER 18 Stacks and Queues 1069
CHAPTER 19 Binary Trees 1109
Appendix A: The ASCII Character Set 1139
Appendix B: Operator Precedence and Associativity 1143
Appendix C: Answers to Checkpoints 1145
Appendix D: Answers to
Share:

Monday, 3 December 2018

Unlimited Positive Number Inputs | C++ | Asad Ali


Unlimited Positive Inputs


Write a program that will ask the user to input n positive numbers. The program will terminate if one of those number is not positive.

#include <iostream>
using namespace std;
int main()
{
long int num = 0;

while( num >= 0 )
{
cout << "Enter a postive number: ";
cin >> num;
}

return 0;
}

Please help me improve my work by letting me know about mistakes (if any) I mad in the project given above, by commenting on the particular post.
Share:

Friday, 30 November 2018

Problem Set 3. Mobile Service Provider - Part 3 | Asad Ali


Problem Set 3. Mobile Service Provider - Part 3


/// Problem Set 3. Mobile Service Provider - Part 2

#include <iostream>
#include <string>
using namespace std;
int main()
{
string month;
char choice;
int mins, extramins;
double extraCharges, dueAmount, saved_B, saved_C;
cout << "\t\t\t\tMobile Service Provider" << endl;
cout << "--------------------------------------------------------------------------------------------------" << endl;
cout << "Package A: For $39.99 per month 450 minutes are provided. Additional minutes are $0.45 per minute." << endl;
cout << "Package B: For $59.99 per month 900 minutes are provided. Additional minutes are $0.40 per minute." << endl;
cout << "Package C: For $69.99 per month unlimited minutes provided." << endl;
cout << "--------------------------------------------------------------------------------------------------" << endl;
cout << "Choose a package out of A, B and C : ";
cin >> choice;
switch ( choice )
{
case 'A':
case 'a':
cout << "How many minuts you used : ";
cin >> mins;
cout << "Wright the name of the month : ";
cin >> month;
if ( month == "January" || month == "March" || month == "May" || month == "July" || month == "August" || month == "October" || month == "December" )
{
if ( mins <= 744 * 60 )
{
if ( mins <= 450 && mins > 0 )
{
cout << "Total amount due : $39.99" << endl;
}
else if ( mins > 450 && mins <= 900 )
{
extramins = mins - 450;
extraCharges = extramins * 0.45;
dueAmount = 39.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
saved_B = dueAmount - 59.99;
if ( saved_B > 0 )
{
cout << "You would saved about $" << saved_B << " if you had choosed Package B." << endl;
}
}
else if ( mins > 900 )
{
extramins = mins - 450;
extraCharges = extramins * 0.45;
dueAmount = 39.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
saved_C = dueAmount - 69.99;
if ( saved_C > 0 )
{
cout << "You would saved about $" << saved_C << " if you had choosed Package C." << endl;
}
}
else
{
cout << "ERROR!" << endl;
}
}
else
{
cout << "You have entered impossible number of minuts. January, March, May, July, August, October, December have only " << 744 * 60 << " minuts at most." << endl;
}
}
if ( month == "April" || month == "June" || month == "September" || month == "November" )
{
if ( mins <= 720 * 60 )
{
if ( mins <= 450 && mins > 0 )
{
cout << "Total amount due : $39.99" << endl;
}
else if ( mins > 450 && mins <= 900 )
{
extramins = mins - 450;
extraCharges = extramins * 0.45;
dueAmount = 39.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
saved_B = dueAmount - 59.99;
if ( saved_B > 0 )
{
cout << "You would saved about $" << saved_B << " if you had choosed Package B." << endl;
}
}
else if ( mins > 900 )
{
extramins = mins - 450;
extraCharges = extramins * 0.45;
dueAmount = 39.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
saved_C = dueAmount - 69.99;
if ( saved_C > 0 )
{
cout << "You would saved about $" << saved_C << " if you had choosed Package C." << endl;
}
}
else
{
cout << "ERROR!" << endl;
}
}
else
{
cout << "You have entered impossible number of minuts. April, June, September, November December have only " << 744 * 60 << " minuts at most." << endl;
}
}
if ( month == "February" )
{
if ( mins <= 672 * 60 )
{
if ( mins <= 450 && mins > 0 )
{
cout << "Total amount due : $39.99" << endl;
}
else if ( mins > 450 && mins <= 900 )
{
extramins = mins - 450;
extraCharges = extramins * 0.45;
dueAmount = 39.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
saved_B = dueAmount - 59.99;
if ( saved_B > 0 )
{
cout << "You would saved about $" << saved_B << " if you had choosed Package B." << endl;
}
}
else if ( mins > 900 )
{
extramins = mins - 450;
extraCharges = extramins * 0.45;
dueAmount = 39.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
saved_C = dueAmount - 69.99;
if ( saved_C > 0 )
{
cout << "You would saved about $" << saved_C << " if you had choosed Package C." << endl;
}
}
else
{
cout << "ERROR!" << endl;
}
}
else
{
cout << "You have entered impossible number of minuts. February has only " << 744 * 60 << " minuts at most." << endl;
}
}
break;
case 'B':
case 'b':
cout << "How many minuts you used : ";
cin >> mins;
cout << "Wright the name of the month : ";
cin >> month;
if ( month == "January" || month == "March" || month == "May" || month == "July" || month == "August" || month == "October" || month == "December" )
{
if ( mins <= 744 * 60 )
{
if ( mins > 0 )
{
if ( mins <= 900 && mins > 450 )
{
cout << "Total amount due : $59.99" << endl;
}
else if ( mins > 900 )
{
extramins = mins - 900;
extraCharges = extramins * 0.40;
dueAmount = 59.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
saved_C = dueAmount - 69.99;
if ( saved_C > 0 )
{
cout << "You would saved about $" << saved_C << " if you had choosed Package C." << endl;
}
else
{
// display nothing
}
}
else if ( mins <= 450 && mins > 0 )
{
dueAmount = 59.99 + extraCharges;
cout << "You would saved about $" << dueAmount - 39.99 << " if you had choosed Package A." << endl;
}
}
else
{
cout << "ERROR!" << endl;
}
}
else
{
cout << "You have entered impossible number of minuts. January, March, May, July, August, October, December have only " << 744 * 60 << " minuts at most." << endl;
}
}
if ( month == "April" || month == "June" || month == "September" || month == "November" )
{
if ( mins <= 720 * 60 )
{
if ( mins > 0 )
{
if ( mins <= 900 && mins > 450 )
{
cout << "Total amount due : $59.99" << endl;
}
else if ( mins > 900 )
{
extramins = mins - 900;
extraCharges = extramins * 0.40;
dueAmount = 59.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
saved_C = dueAmount - 69.99;
if ( saved_C > 0 )
{
cout << "You would saved about $" << saved_C << " if you had choosed Package C." << endl;
}
else
{
// display nothing
}
}
else if ( mins <= 450 && mins > 0 )
{
dueAmount = 59.99 + extraCharges;
cout << "You would saved about $" << dueAmount - 39.99 << " if you had choosed Package A." << endl;
}
}
else
{
cout << "ERROR!" << endl;
}
}
else
{
cout << "You have entered impossible number of minuts. April, June, September, November December have only " << 744 * 60 << " minuts at most." << endl;
}
}
if ( month == "February" )
{
if ( mins <= 672 * 60 )
{
if ( mins > 0 )
{
if ( mins <= 900 && mins > 450 )
{
cout << "Total amount due : $59.99" << endl;
}
else if ( mins > 900 )
{
extramins = mins - 900;
extraCharges = extramins * 0.40;
dueAmount = 59.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
saved_C = dueAmount - 69.99;
if ( saved_C > 0 )
{
cout << "You would saved about $" << saved_C << " if you had choosed Package C." << endl;
}
else
{
// display nothing
}
}
else if ( mins <= 450 && mins > 0 )
{
dueAmount = 59.99 + extraCharges;
cout << "You would saved about $" << dueAmount - 39.99 << " if you had choosed Package A." << endl;
}
}
else
{
cout << "ERROR!" << endl;
}
}
else
{
cout << "You have entered impossible number of minuts. February has only " << 744 * 60 << " minuts at most." << endl;
}
}
break;
case 'C':
case 'c':
cout << "How many minuts you used : ";
cin >> mins;
cout << "Wright the name of the month : ";
cin >> month;
if ( month == "January" || month == "March" || month == "May" || month == "July" || month == "August" || month == "October" || month == "December" )
{
if ( mins <= 744 * 60 )
{
if ( mins > 0 )
{
if ( mins > 450 && mins <= 900 )
{
cout << "You should choose Package B. In this way you would save about $10." << endl;

}
else if ( mins <= 450 )
{
cout << "You should choose Package A. In this way you would save about $30." << endl;
}
else
{
cout << "Total amount due : $69.99" << endl;
}
}
else
{
cout << "ERROR!" << endl;
}
}
else
{
cout << "You have entered impossible number of minuts. January, March, May, July, August, October, December have only " << 744 * 60 << " minuts at most." << endl;
}
}
if ( month == "April" || month == "June" || month == "September" || month == "November" )
{
if ( mins <= 720 * 60 )
{
if ( mins > 0 )
{
if ( mins > 450 && mins <= 900 )
{
cout << "You should choose Package B. In this way you would save about $10." << endl;

}
else if ( mins <= 450 )
{
cout << "You should choose Package A. In this way you would save about $30." << endl;
}
else
{
cout << "Total amount due : $69.99" << endl;
}
}
else
{
cout << "ERROR!" << endl;
}
}
else
{
cout << "You have entered impossible number of minuts. April, June, September, November December have only " << 744 * 60 << " minuts at most." << endl;
}
}
if ( month == "February" )
{
if ( mins <= 672 * 60 )
{
if ( mins > 0 )
{
if ( mins > 450 && mins <= 900 )
{
cout << "You should choose Package B. In this way you would save about $10." << endl;

}
else if ( mins <= 450 )
{
cout << "You should choose Package A. In this way you would save about $30." << endl;
}
else
{
cout << "Total amount due : $69.99" << endl;
}
}
else
{
cout << "ERROR!" << endl;
}
}
else
{
cout << "You have entered impossible number of minuts. February has only " << 744 * 60 << " minuts at most." << endl;
}
}
break;
default:
cout << "ERROR!" << endl;
break;
}
return 0;
}

/// Code by Asad Ali and officially posted on padacode.blogspot.com

Please help me improve my work by letting me know about mistakes (if any) I mad in the project given above, by commenting on the particular post.
Share:

Monday, 26 November 2018

Problem Set 2. Mobile Service Provider - Part 2 | Asad Ali


Problem Set 2. Mobile Service Provider - Part 2


/// Problem Set 2. Mobile Service Provider - Part 2

#include <iostream>
using namespace std;
int main()
{
char choice;
int mins, extramins;
double extraCharges, dueAmount, saved_B, saved_C;
cout << "\t\t\t\tMobile Service Provider" << endl;
cout << "--------------------------------------------------------------------------------------------------" << endl;
cout << "Package A: For $39.99 per month 450 minutes are provided. Additional minutes are $0.45 per minute." << endl;
cout << "Package B: For $59.99 per month 900 minutes are provided. Additional minutes are $0.40 per minute." << endl;
cout << "Package C: For $69.99 per month unlimited minutes provided." << endl;
cout << "--------------------------------------------------------------------------------------------------" << endl;
cout << "Choose a package out of A, B and C : ";
cin >> choice;
switch ( choice )
{
case 'A':
case 'a':
cout << "How many minuts you used : ";
cin >> mins;
if ( mins <= 450 && mins > 0 )
{
cout << "Total amount due : $39.99" << endl;
}
else if ( mins > 450 && mins <= 900 )
{
extramins = mins - 450;
extraCharges = extramins * 0.45;
dueAmount = 39.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
saved_B = dueAmount - 59.99;
if ( saved_B > 0 )
{
cout << "You would saved about $" << saved_B << " if you had choosed Package B." << endl;
}
}
else if ( mins > 900 )
{
extramins = mins - 450;
extraCharges = extramins * 0.45;
dueAmount = 39.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
saved_C = dueAmount - 69.99;
if ( saved_C > 0 )
{
cout << "You would saved about $" << saved_C << " if you had choosed Package C." << endl;
}
}
else
{
cout << "ERROR!" << endl;
}
break;
case 'B':
case 'b':
cout << "How many minuts you used : ";
cin >> mins;
if ( mins > 0 )
{
if ( mins <= 900 && mins > 450 )
{
cout << "Total amount due : $59.99" << endl;
}
else if ( mins > 900 )
{
extramins = mins - 900;
extraCharges = extramins * 0.40;
dueAmount = 59.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
saved_C = dueAmount - 69.99;
if ( saved_C > 0 )
{
cout << "You would saved about $" << saved_C << " if you had choosed Package C." << endl;
}
else
{
// display nothing
}
}
else if ( mins <= 450 && mins > 0 )
{
dueAmount = 59.99 + extraCharges;
cout << "You would saved about $" << dueAmount - 39.99 << " if you had choosed Package A." << endl;
}
}
else
{
cout << "ERROR!" << endl;
}
break;
case 'C':
case 'c':
cout << "How many minuts you used : ";
cin >> mins;
if ( mins > 0 )
{
if ( mins > 450 && mins <= 900 )
{
cout << "You should choose Package B. In this way you would save about $10." << endl;

}
else if ( mins <= 450 )
{
cout << "You should choose Package A. In this way you would save about $30." << endl;
}
else
{
cout << "Total amount due : $69.99" << endl;
}
}
else
{
cout << "ERROR!" << endl;
}
break;
default:
cout << "ERROR!" << endl;
break;
}
return 0;
}
/// Code by Asad Ali and officially posted on PADA Code

Please help me improve my work by letting me know about mistakes (if any) I mad in the project given above, by commenting on the particular post.
Share:

Problem Set 1. Mobile Service Provider - Part 1 | Asad Ali



Problem Set 1. Mobile Service Provider - Part 1

A mobile phone service provider has three different subscription packages for its customers:
• Package A: For $39.99 per month 450 minutes are provided. Additional minutes are $0.45 per minute.
• Package B: For $59.99 per month 900 minutes are provided. Additional minutes are $0.40 per minute.
• Package C: For $69.99 per month unlimited minutes provided.
Write a program that calculates a customer’s monthly bill. It should ask which package the customer has purchased and how many minutes were used. It should then display the total amount due.
Input Validation: Be sure the user only selects package A, B, or C.
/// Problem Set 1. Mobile Service Provider - Part 1

#include <iostream>
using namespace std;
int main()
{
char choice;
int mins, extramins;
double extraCharges, dueAmount;
cout << "\t\t\t\tMobile Service Provider" << endl;
cout << "--------------------------------------------------------------------------------------------------" << endl;
cout << "Package A: For $39.99 per month 450 minutes are provided. Additional minutes are $0.45 per minute." << endl;
cout << "Package B: For $59.99 per month 900 minutes are provided. Additional minutes are $0.40 per minute." << endl;
cout << "Package C: For $69.99 per month unlimited minutes provided." << endl;
cout << "--------------------------------------------------------------------------------------------------" << endl;
cout << "Choose a package out of A, B and C : ";
cin >> choice;
switch ( choice )
{
case 'A':
case 'a':
cout << "How many minuts you used : ";
cin >> mins;
if ( mins == 450 || mins < 450 )
{
cout << "Total amount due : $39.99" << endl;
}
else if ( mins > 450 )
{
extramins = mins - 450;
extraCharges = extramins * 0.45;
dueAmount = 39.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
}
else
{
cout << "ERROR!" << endl;
}
break;
case 'B':
case 'b':
cout << "How many minuts you used : ";
cin >> mins;
if ( mins == 900 || mins < 900 )
{
cout << "Total amount due : $59.99" << endl;
}
else if ( mins > 900 )
{
extramins = mins - 900;
extraCharges = extramins * 0.40;
dueAmount = 59.99 + extraCharges;
cout << "Total amount due : $" << dueAmount << endl;
}
else
{
cout << "ERROR!" << endl;
}
break;
case 'C':
case 'c':
cout << "How many minuts you used : ";
cin >> mins;
cout << "Total amount due : $69.99" << endl;
break;
default:
cout << "ERROR!" << endl;
break;
}
return 0;
}

/// Code by Asad Ali and officially posted on PADA Code

These lines of code first ask the user for choosing the package provided by Mobile Service Providers then it will ask for the used up mins. The according to the given charges it will output the total amount due. 
Share:

Thursday, 15 November 2018

Getting Started with C++ | Tony Gaddis | Chapter 4 | Problem Solutions | Asad Ali


All the solutions to the problems of the Chapter 2 out of book "Getting Started With C++ by Tony Gaddis"

Problem 1
/*
Minimum/Maximum
Write a program that asks the user to enter two numbers. The program should use the
conditional operator to determine which number is the smaller and which is the larger.
*/
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter number one : ";
cin >> num1;
cout << "Enter number two : ";
cin >> num2;
if(num1 > num2)
{
cout << "Number one " << num1 << " is maximum and Number two " << num2 << " is minimum." << endl;
}
else if(num2 > num1)
{
cout << "Number one " << num2 << " is maximum and Number two " << num1 << " is minimum." << endl;
}
else
{
cout << "Number one " << num1 << " is equal to Number two " << num2 << endl;
}
return 0;
}
Problem 2
/*
Roman Numeral Converter
Write a program that asks the user to enter a number within the range of 1 through
10. Use a switch statement to display the Roman numeral version of that number.
Input Validation: Do not accept a number less than 1 or greater than 10.
*/
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a number : ";
cin >> num;
if(num >= 1 && num <= 10)
{
switch(num)
{
case 1: cout << "1 in roman is I" << endl;
break;
case 2: cout << "2 in roman is II" << endl;
break;
case 3: cout << "3 in roman is III" << endl;
break;
case 4: cout << "4 in roman is IV" << endl;
break;
case 5: cout << "5 in roman is V" << endl;
break;
case 6: cout << "6 in roman is VI" << endl;
break;
case 7: cout << "7 in roman is VII" << endl;
break;
case 8: cout << "8 in roman is VIII" << endl;
break;
case 9: cout << "9 in roman is IX" << endl;
break;
case 10: cout << "10 in roman is X" << endl;
break;
}
}
else
{
cout << "ERROR: Invalid Entry!" << endl;
}
return 0;
}
Problem 3
/*
Magic Dates
The date June 10, 1960 is special because when we write it in the following format,
the month times the day equals the year.
6/10/60
Write a program that asks the user to enter a month (in numeric form), a day, and a
two-digit year. The program should then determine whether the month times the day
is equal to the year. If so, it should display a message saying the date is magic. Otherwise
it should display a message saying the date is not magic.
*/
#include <iostream>
using namespace std;
int main()
{
int day = 0, month = 0, year = 0, check = 0;

cout << "Please Enter the day number : " << endl;
cin >> day;
cout << "Please Enter the month number : " << endl;
cin >> month;
cout << "Please Enter the year number last two digits : " << endl;
cin >> year;

check = day * month;

if(check == year)
{
cout << "The date is magic" << endl;
}
else if (check != year)
{
cout << "The date is not a magic" << endl;
}
else
{
cout << "Ooo! Imposible." << endl;
}

return 0;
}
Problem 4
/*
Areas of Rectangles
The area of a rectangle is the rectangle’s length times its width. Write a program that
asks for the length and width of two rectangles. The program should tell the user
which rectangle has the greater area, or if the areas are the same.
*/
#include <iostream>
using namespace std;
int main()
{
int L1, L2, W1, W2, A1, A2;
cout << "Enter the length of Rectangle One : ";
cin >> L1;
cout << "Enter the Width of Rectangle One : ";
cin >> W1;
cout << "Enter the length of Rectangle two : ";
cin >> L2;
cout << "Enter the Width of Rectangle two : ";
cin >> W2;
A1 = L1 * W1;
A2 = L2 * W2;
if(A1 > A2)
{
cout << "Rectangle One with area " << A1 << " is greater than the Rectangle Two with area " << A2 << endl;
}
else if(A2 > A1)
{
cout << "Rectangle Two with area " << A2 << " is greater than the Rectangle One with area " << A1 << endl;
}
else
{
cout << "ERROR!" << endl;
}
return 0;
}
Problem 5
/*
Body Mass Index
Write a program that calculates and displays a person’s body mass index (BMI). The
BMI is often used to determine whether a person with a sedentary lifestyle is overweight
or underweight for his or her height. A person’s BMI is calculated with the following
formula:
BMI = weight × 703 / height2
where weight is measured in pounds and height is measured in inches. The program
should display a message indicating whether the person has optimal weight, is underweight,
or is overweight. A sedentary person’s weight is considered to be optimal if his
or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is considered
to be underweight. If the BMI value is greater than 25, the person is considered
to be overweight.
*/
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int weight, height, BMI;
cout << "Enter your height : ";
cin >> height;
cout << "Enter your weight : ";
cin >> weight;
BMI = weight * 703 / pow(height, 2);
if(BMI < 18.5)
{
cout << "The person is underweight." << endl;
}
else if(BMI > 25)
{
cout << "The person is overweight." << endl;
}
else
{
cout << "The person is optimal weight." << endl;
}
return 0;
}
Problem 6
/*
Mass and Weight
Scientists measure an object’s mass in kilograms and its weight in newtons. If you
know the amount of mass that an object has, you can calculate its weight, in newtons,
with the following formula:
Weight = mass × 9.8
Write a program that asks the user to enter an object’s mass, and then calculates and
displays its weight. If the object weighs more than 1,000 newtons, display a message
indicating that it is too heavy. If the object weighs less than 10 newtons, display a
message indicating that the object is too light.
*/
#include <iostream>
using namespace std;
int main(){
double weight, mass;
cout << "Enter mass of the object in kilogram : ";
cin >> mass;
weight = mass * 9.8;
if(weight > 1000)
{
cout << "The object is too heavy." << endl;
}
else if(weight < 10)
{
cout << "The object is too light." << endl;
}
else
{
cout << "The object weight is optimal." << endl;
}
return 0;
}
Problem 7
/*
Time Calculator
Write a program that asks the user to enter a number of seconds.
• There are 60 seconds in a minute. If the number of seconds entered by the user is
greater than or equal to 60, the program should display the number of minutes in
that many seconds.
• There are 3,600 seconds in an hour. If the number of seconds entered by the user
is greater than or equal to 3,600, the program should display the number of hours
in that many seconds.
• There are 86,400 seconds in a day. If the number of seconds entered by the user is
greater than or equal to 86,400, the program should display the number of days
in that many seconds.
*/
#include <iostream>
using namespace std;
int main(){
float seconds, minuts, hours;
cout << "Enter the number of the seconds : ";
cin >> seconds;

if(seconds >= 60 && seconds < 3600)
{
minuts = seconds / 60;
cout << "The number of minuts is " << minuts << endl;
}
else if(seconds >= 3600)
{
hours = seconds / 3600;
cout << "The nubmer of hours is " << hours << endl;
}
else
{
cout << "Less than a minut." << endl;
}
return 0;
}
Problem 8
coming soon..
Problem 9
coming soon..

Problem 9
coming soon..

Problem 9
coming soon..

Share:

Getting Started with C++ | Tony Gaddis | Chapter 2 | Problem Solutions | Asad Ali


All the solutions to the problems of the Chapter 2 out of book "Getting Started With C++ by Tony Gaddis"

Problem 1
/*
Sum of Two Numbers
Write a program that stores the integers 62 and 99 in variables, and stores the sum of
these two in a variable named total.
*/
#include <iostream>
using namespace std;
int main(){
int num1 = 62,
num2 = 99,
total;

total = num1 + num2;

cout << "Sum of 62 and 99 is " << total << endl;
return 0;
}
Problem 2
/*
Sales Prediction
The East Coast sales division of a company generates 62 percent of total sales. Based
on that percentage, write a program that will predict how much the East Coast division
will generate if the company has $4.6 million in sales this year.
*/
#include <iostream>
using namespace std;
int main(){
double per_TotalSales = 0.62,   // 62 %
totalSales = 4600000;      // 4.6 million dollar
int profit;

profit = totalSales * per_TotalSales;

cout << "East Cost Sales Divsion of company generates : " << profit << endl;
return 0;
}
Problem 3
/*
Sales Tax
Write a program that will compute the total sales tax on a $52 purchase. Assume the
state sales tax is 4 percent and the county sales tax is 2 percent.
*/
#include <iostream>
using namespace std;
int main(){
double state_tax = 0.04,
contry_tax = 0.02,
purchase = 52,
total_salesTax;

total_salesTax = ( state_tax + contry_tax ) * purchase;

cout << "The total sales tax on 52$ purchase is : " << total_salesTax << endl;
return 0;
}
Problem 4
/*
Restaurant Bill
Write a program that computes the tax and tip on a restaurant bill for a patron with a
$44.50 meal charge. The tax should be 6.75 percent of the meal cost. The tip should
be 15 percent of the total after adding the tax. Display the meal cost, tax amount, tip
amount, and total bill on the screen.
*/
#include <iostream>
using namespace std;
int main(){
double meal_charge = 44.50,
tax_per = 0.0675,
tip_per = 0.15,
tax_amount,
meal_cost_tax,
tip_amount,
total_meal_cost;

tax_amount = meal_charge * tax_per;
meal_cost_tax = meal_charge + tax_amount;
tip_amount = meal_cost_tax * tip_per;
total_meal_cost = meal_cost_tax + tip_amount;

cout << "Meal Cost : " << total_meal_cost << endl;
cout << "Tax Amount : " << tax_amount << endl;
cout << "Tip Amount : " << tip_amount << endl;
return 0;
}
Problem 5
/*
Average of Values
To get the average of a series of values, you add the values up and then divide the sum by
the number of values. Write a program that stores the following values in five different
variables: 28, 32, 37, 24, and 33. The program should first calculate the sum of these five
variables and store the result in a separate variable named sum. Then, the program
should divide the sum variable by 5 to get the average. Display the average on the screen.
*/
#include <iostream>
using namespace std;
int main(){
double num1 = 28, num2 = 32, num3 = 37, num4 = 24, num5 = 33, sum, average;

sum = num1 + num2 + num3 + num4 + num5;
average = sum / 5;

cout << "Average : " << average << endl;
return 0;
}
Problem 6
/*
Annual Pay
Suppose an employee gets paid every two weeks and earns $1700.00 each pay period. In a
year the employee gets paid 26 times. Write a program that defines the following variables:
payAmount This variable will hold the amount of pay the employee earns each pay
period. Initialize the variable with 1700.0.
payPeriods This variable will hold the number of pay periods in a year. Initialize the
variable with 26.
annualPay This variable will hold the employee’s total annual pay, which will be calculated.
The program should calculate the employee’s total annual pay by multiplying the
employee’s pay amount by the number of pay periods in a year, and store the result in
the annualPay variable. Display the total annual pay on the screen.
*/
#include <iostream>
using namespace std;
int main(){
int payAmount = 1700,
payPeriods = 26,
annualPay;

annualPay = payAmount * payPeriods;

cout << "Annual Pay of the Employee is : " << annualPay << endl;
return 0;
}
Problem 7
/*
Ocean Levels
Assuming the ocean’s level is currently rising at about 1.5 millimeters per year, write a
program that displays:
• The number of millimeters higher than the current level that the ocean’s level will
be in 5 years
• The number of millimeters higher than the current level that the ocean’s level will
be in 7 years
• The number of millimeters higher than the current level that the ocean’s level will
be in 10 years
*/
#include <iostream>
using namespace std;
int main(){
double rise_each_year = 1.5, five_year_later, seven_year_later, ten_year_later;
five_year_later = rise_each_year * 5;
seven_year_later = rise_each_year * 7;
ten_year_later = rise_each_year * 10;

cout << "The number of millimeters higher than the current level that the ocean’s level after 5 years is : " << five_year_later << endl;
cout << "The number of millimeters higher than the current level that the ocean’s level after 5 years is : " << seven_year_later << endl;
cout << "The number of millimeters higher than the current level that the ocean’s level after 5 years is : " << ten_year_later << endl;
return 0;
}
Problem 8
/*
Total Purchase
A customer in a store is purchasing five items. The prices of the five items are:
Price of item 1 = $12.95
Price of item 2 = $24.95
Price of item 3 = $6.95
Price of item 4 = $14.95
Price of item 5 = $3.95
Write a program that holds the prices of the five items in five variables. Display each
item’s price, the subtotal of the sale, the amount of sales tax, and the total. Assume
the sales tax is 6%.
*/
#include <iostream>
using namespace std;
int main(){
float item1 = 12.95,
item2 = 24.95,
item3 = 6.95,
item4 = 14.95,
item5 = 3.95,
sales_tax_per = 0.06,
subTotal = item1 + item2 + item3 + item4 + item5,
sales_tax_amount,
total;

sales_tax_amount = sales_tax_per * subTotal;
total = sales_tax_amount + subTotal;

cout << "Price of item 1 : " << item1 << endl;
cout << "Price of item 2 : " << item2 << endl;
cout << "Price of item 3 : " << item3 << endl;
cout << "Price of item 4 : " << item4 << endl;
cout << "Price of item 5 : " << item5 << endl;
cout << "Sub Total : " << subTotal << endl;
cout << "Total Sales Tax : " << sales_tax_amount << endl;
cout << "Total amount : " << total << endl;
return 0;
}
Problem 9
/*
Cyborg Data Type Sizes
You have been given a job as a programmer on a Cyborg supercomputer. In order to
accomplish some calculations, you need to know how many bytes the following data
types use: char, int, float, and double. You do not have any manuals, so you can’t
look this information up. Write a C++ program that will determine the amount of
memory used by these types and display the information on the screen.
*/
#include <iostream>
using namespace std;
int main(){
cout << "Size of data type char is (bytes) : " << sizeof(char) << endl;
cout << "Size of data type int is (bytes) : " << sizeof(int) << endl;
cout << "Size of data type float is (bytes) : " << sizeof(float) << endl;
cout << "Size of data type double is (bytes) : " << sizeof(double) << endl;
return 0;
}
Problem 10
/*
Miles per Gallon
A car holds 12 gallons of gasoline and can travel 350 miles before refueling. Write a
program that calculates the number of miles per gallon the car gets. Display the result
on the screen.
Hint: Use the following formula to calculate miles per gallon (MPG):
MPG = Miles Driven / Gallons of Gas Used
*/
#include <iostream>
using namespace std;
int main(){
int gallons_stored = 12,
miles_traveled = 350,
miles_per_gallon;

miles_per_gallon = miles_traveled / gallons_stored;

cout << "Miles Per Gallon of The Car : " << miles_per_gallon << endl;
return 0;
}
Problem 11
/*
Distance per Tank of Gas
A car with a 20-gallon gas tank averages 21.5 miles per gallon when driven in town
and 26.8 miles per gallon when driven on the highway. Write a program that calculates
and displays the distance the car can travel on one tank of gas when driven in
town and when driven on the highway.
Hint: The following formula can be used to calculate the distance:
Distance = Number of Gallons × Average Miles per Gallon
*/
#include <iostream>
using namespace std;
int main(){
double gallons = 20,
miles_per_gallon = 21.5,
distance_traveled;

distance_traveled = gallons * miles_per_gallon;

cout << "Distance Traveled by Car: " << distance_traveled << endl;
return 0;
}
Problem 12
/*
Land Calculation
One acre of land is equivalent to 43,560 square feet. Write a program that calculates
the number of acres in a tract of land with 389,767 square feet.
*/
#include <iostream>
using namespace std;
int main(){
long int acre = 43560, // int can only store 32,767 so we used "long int"
land = 389767;
double numberOfAcres;
numberOfAcres = land / acre;
cout << "Number of Acres : " << numberOfAcres << endl;
return 0;
}
Problem 13
/*
Circuit Board Price
An electronics company sells circuit boards at a 40 percent profit. Write a program
that will calculate the selling price of a circuit board that costs $12.67. Display the
result on the screen.
*/
#include <iostream>
using namespace std;
int main(){
double profit_per = 0.4,
profit_price,
circuit_cost = 12.67,
total_price;

profit_price =  profit_per * circuit_cost;
total_price = circuit_cost + profit_price;

cout << "Total circuit price :  " << total_price << endl;
return 0;
}
Problem 14
/*
Personal Information
Write a program that displays the following pieces of information, each on a separate line:
Your name
Your address, with city, state, and ZIP code
Your telephone number
Your college major
Use only a single cout statement to display all of this information.
*/
#include <iostream>
using namespace std;
int main(){
cout << "Asad Ali\nJohar Town, Lahore, Punjab, 5400\n+923043388800\nUniversity of Lahore -DRC" << endl;
return 0;
}
Problem 15
/*
Triangle Pattern
Write a program that displays the following pattern on the screen:
   *
  ***
 *****
*******
*/
#include <iostream>
using namespace std;
int main(){
cout << "Triangle Pattern" << endl;
cout << endl;
cout << "   *   " << endl;
cout << "  ***  " << endl;
cout << " ***** " << endl;
cout << "*******" << endl;
return 0;
}
Problem 16
/*
Diamond Pattern
Write a program that displays the following pattern:
   *
  ***
 *****
*******
 *****
  ***
   *
*/
#include <iostream>
using namespace std;
int main(){
cout << "Diamond Pattern" << endl;
cout << endl;
cout << "   *   " << endl;
cout << "  ***  " << endl;
cout << " ***** " << endl;
cout << "*******" << endl;
cout << " ***** " << endl;
cout << "  ***  " << endl;
cout << "   *   " << endl;
return 0;
}
Problem 17
/*
Stock Commission
Kathryn bought 600 shares of stock at a price of $21.77 per share. She must pay her
stock broker a 2 percent commission for the transaction. Write a program that calculates
and displays the following:
• The amount paid for the stock alone (without the commission)
• The amount of the commission
• The total amount paid (for the stock plus the commission)
*/
#include <iostream>
using namespace std;
int main(){
double number_shares = 600,
price_per_share = 21.77,
commission_per = 0.02,
subtotal_price = number_shares * price_per_share,
commission_price,
total_price;

commission_price = commission_per * subtotal_price;
total_price = commission_price + subtotal_price;

cout << "The amount paid for stock alone : " << subtotal_price << endl;
cout << "The amount of commission : " << commission_price << endl;
cout << "The total amount paid : " << total_price << endl;
return 0;
}
Problem 18
/*
Energy Drink Consumption
A soft drink company recently surveyed 12,467 of its customers and found that
approximately 14 percent of those surveyed purchase one or more energy drinks per
week. Of those customers who purchase energy drinks, approximately 64 percent of
them prefer citrus flavored energy drinks. Write a program that displays the following:
• The approximate number of customers in the survey who purchase one or more
energy drinks per week
• The approximate number of customers in the survey who prefer citrus flavored
energy drinks
*/
#include <iostream>
using namespace std;
int main(){
double total_dirinks = 12467,
energy_per = 0.14,
citrus_per = 0.64,
nEnergy = energy_per * total_dirinks,
nCitrus = citrus_per * total_dirinks;
cout << "The approximate number of customers in the survey who purchase one or more energy drinks per week is : " << nEnergy << endl;
cout << "The approximate number of customers in the survey who prefer citrus flavored energy drinks : " << nCitrus << endl;
return 0;
}

Share:

Lectures | Programming Fundamentals-1 | Adeem Ali Sahib | pada.edu | Asad Ali



Guys

Welcome Again

Those students at University of Lahore who are not member of  https://groups.google.com/forum/#!forum/pf1-uol

Can download the lectures from the link below

Just click the link below and download the Eleven Lectures of Programming Fundamentals-1

👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇👇

Programming Fundamentals Lectures


Please those students who are new to my blog please follow my blog so that you can get notifications of each post made by me.

Thanks for Visiting my blog Showing your love for me. 
Share:

padacode.blogspot.com

Powered by Blogger.