0% found this document useful (0 votes)
2 views

Selected-Programming-Exercises-Ch4

The document contains programming exercises for a course on computer skills at the University of Jordan. It includes solutions for three exercises: determining if a triangle is right-angled, creating a simple calculator, and calculating the number of lines and characters that can be printed on paper based on various parameters. Each exercise is accompanied by sample code and sample runs demonstrating the expected output.

Uploaded by

shessored8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Selected-Programming-Exercises-Ch4

The document contains programming exercises for a course on computer skills at the University of Jordan. It includes solutions for three exercises: determining if a triangle is right-angled, creating a simple calculator, and calculating the number of lines and characters that can be printed on paper based on various parameters. Each exercise is accompanied by sample code and sample runs demonstrating the expected output.

Uploaded by

shessored8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

The University of Jordan

King Abdullah II School of Information Technology


Department of Computer Science

Computer Skills for Scientific Faculties-(1931102)

Chapter 4: Programming Exercises -sample

ExProg1: 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.

Solution:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double side1, side2, side3;

cout << "Enter the lengths of the sides a triangle: ";


cin >> side1 >> side2 >> side3;
cout << endl;

if ((side1 * side1 == (side2 * side2 + side3 * side3)) ||


(side2 * side2 == (side1 * side1 + side3 * side3)) ||
(side3 * side3 == (side1 * side1 + side2 * side2)))

cout << "It is a right angled triangle" << endl;


else
cout << "It is not a right angled triangle" << endl;

return 0;
}

Sample Run:

Enter the lengths of the sides a triangle: 5 6 7

It is not a right angled triangle


*********************************************

ExProg1: Write a program that mimics a calculator. The program should


take as input two integers and the operation to be performed. It should
then output the numbers, the operator, and the result. (For division, if
the denominator is zero, output an appropriate message.) Some sample
outputs follow:
3+4=7
13 * 5 = 65
Solution:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
int num1, num2;
char opr;

cout << "Enter two integers: ";


cin >> num1 >> num2;
cout << endl;

cout << "Enter operator: + (addition), - (subtraction),"


<< " * (multiplication), / (division): ";
cin >> opr;
cout << endl;

cout << num1 << " " << opr << " " << num2 << " = ";

switch (opr)
{
case '+':
cout << num1 + num2 << endl;
break;
case '-':
cout << num1 - num2 << endl;
break;
case '*':
cout << num1 * num2 << endl;
break;
case '/':
if (num2 != 0)
cout << num1 / num2 << endl;
else
cout << "ERROR \nCannot divide by zero" << endl;
break;
default:
cout << "Illegal operation" << endl;
}
return 0;
}
Sample Run:

Enter two integers: 2 3

Enter operator: + (addition), - (subtraction), * (multiplication), /


(division):
+

2 + 3 = 5
----

Enter two integers: 6 9

Enter operator: + (addition), - (subtraction), * (multiplication), /


(division):
-

6 - 9 = -3

*********************************************

ExProg3: The number of lines that can be printed on a paper depends on


the paper size, the point size of each character in a line, whether lines
are doublespaced or singlespaced, the top and bottom margin, and the
left and right margins of the paper. Assume that all characters are of the
same point size, and all lines are either singlespaced or doublespaced.
Note that 1 inch = 72 points. Moreover, assume that the lines are printed
along the width of the paper. For example, if the length of the paper is 11
inches and width is 8.5 inches, then the maximum length of a line is 8.5
inches. Write a program that calculates the number of characters in a line
and the number of lines that can be printed on a paper based on the
following input from the user:

a) The length and width, in inches, of the paper


b) The top, bottom, left, and right margins
c) The point size of a line
d) If the lines are doublespaced, then double the point size of each
character

Solution:

#include <iostream>
#include <iomanip>
using namespace std;

const int POINTS_IN_INCH = 72;

int main()
{
double length;
double width;

double leftMargin;
double rightMargin;

double topMargin;
double bottomMargin;

int charPointSize;
char lineSpacing;

double lengthOfALine;
int numberOfCharactersInALine = 0;
int numberOfLines = 0;

cout << fixed << showpoint << setprecision(2);

cout << "Enter the length of the paper: ";


cin >> length;
cout << endl;

cout << "Enter the width of the paper: ";


cin >> width;
cout << endl;

cout << "Enter the left margin of the paper: ";


cin >> leftMargin;
cout << endl;

cout << "Enter the right margin of the paper: ";


cin >> rightMargin;
cout << endl;

cout << "Enter the top margin of the paper: ";


cin >> topMargin;
cout << endl;

cout << "Enter the bottom margin of the paper: ";


cin >> bottomMargin;
cout << endl;

cout << "Enter the point size of a character: ";


cin >> charPointSize;
cout << endl;

cout << "Enter line spacing, s or S (single spaced), "


<< "d or D (double spaced): ";
cin >> lineSpacing;
cout << endl;

lengthOfALine = width - leftMargin - rightMargin;


numberOfCharactersInALine = static_cast<int>(lengthOfALine *
POINTS_IN_INCH)
/ charPointSize;

if (lineSpacing == 's' || lineSpacing == 'S')


numberOfLines = static_cast<int>(length - topMargin - bottomMargin)
* POINTS_IN_INCH / charPointSize;
else if (lineSpacing == 'd' || lineSpacing == 'D')
numberOfLines = static_cast<int>(length - topMargin - bottomMargin)
* POINTS_IN_INCH / charPointSize /
2;
else
cout << "Invalid line spacing" << endl;

cout << "The number of lines that can be printed: " << numberOfLines <<
endl;
cout << "The number of characters that can be printed in a line: "
<< numberOfCharactersInALine << endl;

cin.ignore();
cin.get();
return 0;
}

Sample Run:

Enter the length of the paper: 12

Enter the width of the paper: 9

Enter the left margin of the paper: 2

Enter the right margin of the paper: 3

Enter the top margin of the paper: 2

Enter the bottom margin of the paper: 2

Enter the point size of a character: 12

Enter line spacing, s or S (single spaced), d or D (double spaced): s

The number of lines that can be printed: 48


The number of characters that can be printed in a line: 24

You might also like