SI Object-Oriented Programming - Assignment 1 | CS 1410, Assignments of Computer Science

Material Type: Assignment; Class: SI Object-Oriented Programming; Subject: Computer Science; University: Weber State University; Term: Unknown 2008;

Typology: Assignments

Pre 2010

Uploaded on 07/23/2009

koofers-user-sma
koofers-user-sma 🇺🇸

5

(1)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page 1 of 2
CS 1410 Programming Assignment #1
C++ Operators and Statements
Program #1
Quadratic equations have the form: ax2 + bx + c = 0 where a (cannot be 0), b, and c are
the coefficients and x is the unknown value. x is computed using the quadratic formula:
xbb ac
a
=
−±
2
4
2
There are some complex issues (a poor pun BTW) that must be dealt with but these are best left
until the next chapter. The ± operation leads to two similar calculations: replace ‘±’ with ‘+’ in
the first calculation and with ‘-’ in the second. The two calculations produce two roots or
solutions to the original equation.
The math library includes a function named sqrt to calculate square roots. Any program that
uses this function must include the math header file (the library is linked automatically).
#include <cmath>
. . .
y = sqrt(x);
Note that x in the above function may be a constant, a variable, or an expression.
Write a program named roots.cpp that:
1. prompts for and reads the coefficients into double variables in the order a, b, and c
2. calculates and prints two roots
Program #2
Write a program named cone.cpp that:
1. prompts for and reads (in this order) a radius and a height
2. calculates and prints the volume of a right cone:
vrh
=
1
3
2
π
3. calculates and prints the total surface area (base + cone):
Sr rrh
=+ +
ππ
222
The math header file <cmath> defines a symbolic constant for π: M_PI (which you should use).
The Microsoft compiler requires a non-standard definition before math constants may be used:
#define _USE_MATH_DEFINES // Microsoft only
#include <iostream>
#include <cmath>
using namespace std;
You may use the pow function (included in the math library and demonstrated in class) to square
the values in the above formulas; however, for squares and cubes, it’s just about as easy to
multiply the numbers by themselves: radius * radius.
pf2

Partial preview of the text

Download SI Object-Oriented Programming - Assignment 1 | CS 1410 and more Assignments Computer Science in PDF only on Docsity!

Page 1 of 2

CS 1410 Programming Assignment

C++ Operators and Statements

Program

Quadratic equations have the form: ax 2 + bx + c = 0 where a (cannot be 0), b, and c are the coefficients and x is the unknown value. x is computed using the quadratic formula:

x

b b ac

a

There are some complex issues (a poor pun BTW) that must be dealt with but these are best left until the next chapter. The ± operation leads to two similar calculations: replace ‘±’ with ‘+’ in the first calculation and with ‘-’ in the second. The two calculations produce two roots or solutions to the original equation.

The math library includes a function named sqrt to calculate square roots. Any program that uses this function must include the math header file (the library is linked automatically). #include

... y = sqrt(x); Note that x in the above function may be a constant, a variable, or an expression.

Write a program named roots.cpp that:

  1. prompts for and reads the coefficients into double variables in the order a, b, and c
  2. calculates and prints two roots

Program

Write a program named cone.cpp that:

  1. prompts for and reads (in this order) a radius and a height

2. calculates and prints the volume of a right cone: v = r h

1 3

2 π

3. calculates and prints the total surface area (base + cone): S^ =^ π r^^ +^ π r^ r^ + h

2 2 2

The math header file defines a symbolic constant for π: M_PI (which you should use). The Microsoft compiler requires a non-standard definition before math constants may be used: #define _USE_MATH_DEFINES // Microsoft only #include #include using namespace std;

You may use the pow function (included in the math library and demonstrated in class) to square the values in the above formulas; however, for squares and cubes, it’s just about as easy to multiply the numbers by themselves: radius * radius.

Page 2 of 2

Program Submission and Grading

Both programs should exhibit good programming style. Specifically, use consistent and appropriate indentation and blank lines (put blank lines where they improve readability but eliminate irrelevant blank lines).

The file grade1.bat is the test bed used to grade this assignment. You may also use it to verify that you programs are correct prior to submission.

  1. Copy grade1.bat to the a directory containing both source code files
  2. Open a “Visual Studio 2008 Command Prompt”
  3. cd to the directory containing the source code files and grade1.bat
  4. Type the command grade1 and press the enter key

Upload roots.cpp and cone.cpp to WSU Online for grading.