Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 1: Design, code, and test a C++ program that accepts two corner points

ID: 3902843 • Letter: E

Question

Exercise 1: Design, code, and test a C++ program that accepts two corner points (x, y coordinate pairs) of a rectangle from the user. The first point will be the lower-left corner and the second will be the upper-right corner. Use structured decision logic to determine whether the rectangle is a square, or is in "portrait" or "landscape" orientation. In this coordinate system you can assume that the rectangle's coordinates are all positive. Turn in input showing that your program correctly identified all three cases of rectangles.

Exercise 2: Design, code, and test a C++ program that allows a user to enter positive integers continuously until the value 0 is entered. Once the program has finished getting numbers from the user, output the number of numbers that were entered, the sum of those numbers, the average of those numbers, the largest value entered, and the smallest number entered. Note that 0 is the sentinel value and it is NOT part of the data. Submit output showing at least five input values. Note that you cannot use an integer for your average (averages must have a fractional component).

Notes:

Be sure to use only named constants for any constants used.

Be sure to comment your code.

Be sure to PROMPT the user when requesting input from the user.

Be sure to make it clear to the user the meaning of the things being output (for example: "largest number entered was 175" etc).

Make sure your loop is structured

Explanation / Answer

Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you


Code for Question 1:
===========

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

int main()
{
int x1, x2, y1, y2;
int length, breadth;

cout << "Enter lower left corner coordinates (x1 y1): ";
cin >> x1 >> y1;

cout << "Enter upper right corner coordinates (x2 y2): ";
cin >> x2 >> y2;

length = y2 - y1;
if(length < 0) //convert to positive in case of -ve
length = -length;

breadth = x2 - x1;
if(breadth < 0)
breadth = -breadth;

if(length == breadth)
cout << "It's square" << endl;
else if(length > breadth)
cout << "It's a portrait" << endl;
else
cout << "It's a landscape" << endl;
}


output
-----
Enter lower left corner coordinates (x1 y1): 100 100
Enter upper right corner coordinates (x2 y2): 250 150
It's a landscape

Code for Question 2:
-=========
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num;
bool done = false;
int min = 0, max = 0, sum = 0;
double avg;
int n = 0;

while(!done)
{
cout << "Enter a number (type 0 to quit): ";
cin >> num;

if(num == 0)
done = true;
else{
n++;
sum += num;
if(num > max)
max = num;
if(min == 0 || num < min)
min = num;
}

}

if(n != 0)
avg = ((double)sum) / n;

cout << "No. of numbers entered: " << n << endl;
cout << "Largest: " << max << endl;
cout << "Smallest: " << min << endl;
cout << "Sum: " << sum << endl;
cout << "Average: " << avg << endl;


}

output
====
Enter a number (type 0 to quit): 6
Enter a number (type 0 to quit): 3
Enter a number (type 0 to quit): 8
Enter a number (type 0 to quit): 4
Enter a number (type 0 to quit): 9
Enter a number (type 0 to quit): 1
Enter a number (type 0 to quit): 8
Enter a number (type 0 to quit): 0
No. of numbers entered: 7
Largest: 9
Smallest: 1
Sum: 39
Average: 5.57143