C++ Programming Exercise 3: Data Types, Expressions, Constants, and Functions, Exercises of Computer Science

The instructions and tasks for the third c++ programming exercise. It covers data types, expressions, symbolic constants, mathematical functions, and their usage in c++. Students are expected to write and test code snippets to understand the concepts. The document also includes examples and error-finding activities.

Typology: Exercises

2013/2014

Uploaded on 02/01/2014

savitri_122
savitri_122 🇮🇳

4.6

(14)

184 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Third Programming Exercise
Objective
This exercise provides an opportunity to examine the effects of different data types and the ways
in which we have to write C++ statements that use various expressions. It also introduces the
use of symbolic constants and mathematical functions.
Background for exercise
From the second exercise, you should be comfortable using structure of a simple C++ program
shown below. You should also be familiar with the use of the cout and cin statements for
output and input, respectively.
#include <iostream>
using namespace std;
int main()
{
// program statements go here
return EXIT_SUCCESS;
}
You will continue to use the basic structure for the C++ programs to accomplish the tasks in this
programming exercise.
Overview of this exercise
This exercise has four tasks. The first is a copy and paste to view results of data types; the
second and third require the construction of programs to get input, do simple calculations, and
write output. The fourth task is a slight variation of the third. Use the same procedure you
followed for exercise two to create a single file, called the submission file, to which you will copy
all your listings and the screen displays from the program execution. See the notes for the
second exercise for details about copying the screen to another file and preparing a single file to
submit all your program listings and output.
Programming tasks
Task one: Data types and type conversions – The goal of this exercise is for you to learn about
operations with data types. You can start by reviewing the code below and write down what you
think each output line will produce. Then compare your answers to the program output.
Alternatively you can review the results to make sure that you understand them.
Copy, compile and execute the code; add comments to indicate that this is the first task of
exercise three and to show your name and date. Make sure that you understand why the results
have the values they do because the first quiz will ask you to predict the output of similar
statements.
docsity.com
pf3
pf4

Partial preview of the text

Download C++ Programming Exercise 3: Data Types, Expressions, Constants, and Functions and more Exercises Computer Science in PDF only on Docsity!

Third Programming Exercise

Objective

This exercise provides an opportunity to examine the effects of different data types and the ways

in which we have to write C++ statements that use various expressions. It also introduces the use of symbolic constants and mathematical functions.

Background for exercise

From the second exercise, you should be comfortable using structure of a simple C++ program

shown below. You should also be familiar with the use of the cout and cin statements for output and input, respectively.

#include using namespace std;

int main() { // program statements go here

return EXIT_SUCCESS; }

You will continue to use the basic structure for the C++ programs to accomplish the tasks in this programming exercise.

Overview of this exercise

This exercise has four tasks. The first is a copy and paste to view results of data types; the

second and third require the construction of programs to get input, do simple calculations, and write output. The fourth task is a slight variation of the third. Use the same procedure you

followed for exercise two to create a single file, called the submission file, to which you will copy

all your listings and the screen displays from the program execution. See the notes for the second exercise for details about copying the screen to another file and preparing a single file to submit all your program listings and output.

Programming tasks

Task one: Data types and type conversions – The goal of this exercise is for you to learn about operations with data types. You can start by reviewing the code below and write down what you

think each output line will produce. Then compare your answers to the program output. Alternatively you can review the results to make sure that you understand them.

Copy, compile and execute the code; add comments to indicate that this is the first task of exercise three and to show your name and date. Make sure that you understand why the results

have the values they do because the first quiz will ask you to predict the output of similar statements.

Note that one of the statements has an error that is not a syntax error. Can you find this error

before running the program? If you do, do not correct it until you run the program. If you cannot find the error in the listing, you should be able to see the error when you view the results of the

code.

Programming exercise three -- Task one Name: Date: */ #include using namespace std;

int main() { double a; int x = 27, y = 4, z; z = x / y; cout << "Example of output from different data types." << "In these results x = 27, y = 4, and z are type int;" << "a is type double.\n\n"; cout << "For z = x / y, z = " << z << endl; a = x / y; cout << "For a = x / y, a = " << a << endl; z = double( x ) / y; cout << "For z = double( x )/ y, z = " << z << endl; a = double( x ) / y; cout << "For a = double( x )/ y, a = " << a << endl; cout << "cout << double( x )/ y gives " << double( x ) / y << endl; cout << "cout << double( x )/ double( y ) gives " << double( x ) / double( x ) << endl; z = double( x ) / double( y ); cout << "For z = double( x )/ double( y ), z = " << z << endl; cout << "x % y equals " << x % y << endl; cout << "y % x equals " << y % x << endl << endl; return EXIT_SUCCESS; }

Correct the error in the code and rerun it to get correct results. Copy the corrected code listing

(with added comments to give your name, date, exercise number and task number) to the submission file. Copy the correct screen output to the submission file.

Make sure that you understand why the individual results were either an integer 6 or a real number 6.75.

Task two: Writing expressions – Write a C++ program to evaluate the variables a, b, and c from the equations below and print out the results. (Use one-letter C++ variables, a, b, c, w, x,

y, and z, to represent the mathematical quantities, a, b, c, w, x, y, and z in the equations.) Declare all variables to be of type double. Write input statements that allow you to enter any

values for w, x, y, and z. You should write only one program that computes all three values (a, b, and c) for any set of input values (w, x, y, and z).

a = y z

w x

 b = y

w x

x

z

2

 c =

wy x

z

y

x w

 Copy of screen input and output (with all three answers for each data set)

 Code from task three

 Copy of screen input and output from task three

 Code from task four

 Copy of screen input and output from task four