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

The purpose of this assignment is to have you start learning how to use the Visu

ID: 3661783 • Letter: T

Question

The purpose of this assignment is to have you start learning how to use the Visual C++ and either Xcode or g++ environments and understand a variety of programming errors.

Here's what you are to do:

(optional) Obtain a copy of Visual C++ and install it. You don't need to do this if you prefer to do your Visual C++ work on the SEASnet computers (in the lab or remotely). If you will be using another development environment (such as Xcode on a Mac), use this project to get comfortable using it as well as Visual C++.

Enter this C++ program into your development environment. Do not change the program.

Build the executable from the program. (Fix any typos you may have made when entering the program.)

Execute the program with a variety of reasonable input integers to see if it runs as one would expect from reading the source code. (If the Visual C++ console window disappears when your program finishes executing, before you have a chance to see the output, you probably forgot to do step 4 from the Visual C++ 2013 writeup, or you started execution by selecting the Start Debugging item from the Debug menu or by double-clicking on the .exe file. What you want to do is select the Start Without Debugging item from the Debug menu; if there is no such menu item, fix it as directed in step 7.)

Using the program as given, run it with input integers that cause it to produce incorrect, unusual, or nonsensical results. (Notice we're saying to try input integers, not input like 124765.23 or strawberry.)

Starting from the program as given, introduce into the source code at least one error that someone might make that, while not preventing a successful build, causes the program when it runs to produce incorrect results from reasonable input.

Again starting from the program as given, introduce at least two distinct types of mistakes that someone might make, each of which would cause the program to fail to compile correctly.

You should create a separate project for each of steps 2, 6, and 7, since you're not allowed to have multiple files in the same project if more than one has a main routine.

In addition to running the programs under Visual C++, run them using clang++ or g++ (perhaps using Xcode on a Mac or g++ with Linux).

What you will turn in for this assignment is a compressed file in zip format containing exactly four files:

A file named original.cpp that contains the program as given.

A file named logic_error.cpp with the program you produced in step 6.

A file named compile_error.cpp with the program you produced in step 7.

A file named report.doc or report.docx (in Microsoft Word format) or report.txt (an ordinary text file) that describes the input you provided in step 5 and each of the errors you introduced into the logic_error.cpp and compile_error.cpp programs. Briefly discuss any error messages the compiler reported, and incorrect, unusual, or nonsensical results. This report may well end up being much less than a page long.

Here is the C++ program:

Explanation / Answer

Here are all four files

1. Original.cpp

#include <iostream>
using namespace std; // pp. 37-38 in Savitch book explain this line

int main()
{
int daysReviewed;
int daysWithRain;
int daysBelow50Degrees;

cout << "How many days were reviewed? ";
cin >> daysReviewed;
cout << "How many of these days were rainy? ";
cin >> daysWithRain;
cout << "How many of these days were colder than 50 degrees? ";
cin >> daysBelow50Degrees;

double pctRainy = 100.0 * daysWithRain / daysReviewed;
double pctCold = 100.0 * daysBelow50Degrees / daysReviewed;

cout.setf(ios::fixed); // see pp. 30-31 in Savitch book
cout.precision(1);

cout << endl;
cout << pctRainy << "% were rainy." << endl;
cout << pctCold << "% were cold." << endl;

if (pctRainy > pctCold)
cout << "It was rainy more often than it was cold." << endl;
else
cout << "It was cold more often than it rained." << endl;
}

2. logical_error.cpp

#include <iostream>
using namespace std; // pp. 37-38 in Savitch book explain this line

int main()
{
int daysReviewed;
int daysWithRain;
int daysBelow50Degrees;

cout << "How many days were reviewed? ";
cin >> daysReviewed;
cout << "How many of these days were rainy? ";
cin >> daysWithRain;
cout << "How many of these days were colder than 50 degrees? ";
cin >> daysBelow50Degrees;

// Here I introduced a logical error (Multiplication instead of division) which will give me incorrect results
double pctRainy = 100.0 * daysWithRain * daysReviewed;

// Here In this line daysReviewed is replaced with daysWithRainwhich is a logical error producing incorrect results
double pctCold = 100.0 * daysBelow50Degrees / daysWithRain;

cout.setf(ios::fixed); // see pp. 30-31 in Savitch book
cout.precision(1);

cout << endl;
cout << pctRainy << "% were rainy." << endl;
cout << pctCold << "% were cold." << endl;

if (pctRainy > pctCold)
cout << "It was rainy more often than it was cold." << endl;
else
cout << "It was cold more often than it rained." << endl;
}

3. compile_error.cpp


using namespace std; // pp. 37-38 in Savitch book explain this line

int main()
{
int daysReviewed;
int daysWithRain;
int daysBelow50Degrees;

cout << "How many days were reviewed? ";
cin >> daysReviewed;
cout << "How many of these days were rainy? ";
cin >> daysWithRain;
cout << "How many of these days were colder than 50 degrees? ";
cin >> daysBelow50Degrees;

double pctRainy = 100.0 * daysWithRain / daysReviewed;
double pctCold = 100.0 * daysBelow50Degrees / daysReviewed

cout.setf(ios::fixed); // see pp. 30-31 in Savitch book
cout.precision(1);

cout << endl;
cout << pctRainy << "% were rainy." << endl;
cout << pctCold << "% were cold." << endl;

if (pctRainy > pctCold)
cout << "It was rainy more often than it was cold." << endl;
else
cout << "It was cold more often than it rained." << endl;
}

4. Report.txt

Report

1. Inputs in orginal.cpp which provided non sensical results

daysReviewed=0;

It produced undefined of infinite value because the value in daysReviewed divides daysWithRain and daysBelow50Degrees at line 17 and 18 respectively.

2. In logic_error.cpp

Two errors are introduced one at line 18 where division (/) operator has been replaced by multiplication operator (*) which produced incorrect results for pctRainy.

Another error is at line 21 where daysReviewed is replaced with daysWithRain producing incorrect results.

Both the errors are logical errors and wont produce compilation errors. The program will run without any error but will produce incorrect results.

3. compile_error.cpp

Two errors are introduced in compile_error.cpp

At line 0 #include <iostream> has been deleted which means no header file has been included to handle input or output operation. Thus any call to cin or cout will result in compile time error because cin and cout are not defined.

Another error is at line 18 where the end of statement (semicolon ;) is missing . The compiler will throw an error as (expected , or ; at line 18)