Problem You require an algorithm that will receive
Problem You require an algorithm that will receive the length of the hypotenuse and one of the sides of a right triangle, and calculate the length of the remaining side and the two acute angles in the triangle.
Problem You require an algorithm that will receive the length of the hypotenuse and one of the sides of a right triangle, and calculate the length of the remaining side and the two acute angles in the triangle.
Problem angle 1 side 1 hyp angle 2 side 2
Defining Diagram Inputs hyp side 1 Processing Prompt for and get hyp and side 1 Calculate side 2 (Pythagorean theorem) Calculate angle 1 and angle 2 Display results Output side 2 angle 1 angle 2
Calculate_Side_and_Angles Prompt user for hyp and side 1 Get hyp and side 1 side 2 = hyp*hyp – side 1*side 1 side 2 = SQRT(side 2) angle 1 = ARCSIN (side 2/hyp) angle 2 = 90 – angle 1 Display side 2, angle 1 and angle 2 END
Basic Elements of C++
# include <iostream> using namespace std; int main() { cout<<“Hello world!”; return 0; } Preprocessor directive Function main Curly braces Semicolons
# include <iostream> using namespace std; int main() { cout<<“Hello world!”; return 0; }
# include <iostream> using namespace std; void main() { cout<<“Hello world!”; return; }
/* This is our first C++ program Oh, how exciting!!!!! */ # include <iostream> using namespace std; //this is preprocessor directive int main() //this is our first function { cout<<“Hello world!”; return 0; }
Variables l Identifiers can consist of l Letters l Digits (but cannot start with digits) l Underscore character Identifiers are case sensitive. Identifiers should be self-explanatory.
l l l l l price+tax My Number costofthepurchase _total_sum one*two Valid Identifiers? pay$ xcsrtysb Average_Age 3 rd_side
C++ Data Types Simple l Structured l Pointers l
Basic Data Types Integer - int l Floating point value – float, double l Character - char l Boolean - bool l
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 3. 3 -4 39. 001 0 0. 0 ‘d’ ‘Bob’ 9. 0 +123 ‘’
Variable Declaration data-type variable-name , var-name… optional int num; float price 1, price 2; double average; char letter_grade;
Variable Initialization l l l int a=3; //initialization int a; a=3; //assignment int a; cin>>a; //input data
# include <iostream> using namespace std; int main() { int age; cout<<“Enter your age”; cin >>age; cout<<endl<<“You are ”<<age; return 0; }
Calculate_Midpoint Prompt user for x 1, y 1, x 2, y 2 Get x 1, y 1, x 2, y 2 xm=(x 1+x 2)/2 ym=(y 1+y 2)/2 Display xm and ym END
# include <iostream> using namespace std; int main() { int x 1, y 1, x 2, y 2, xm, ym; cout<<endl<<“Enter the coordinates of the 1 st point”; cin >>x 1 >>y 1; cout<<endl<<“Enter the coordinates of the 2 nd point”; cin>>x 2 >>y 2; xm=(x 1+x 2)/2; ym=(y 1+y 2)/2; cout<<endl<<“The midpoint is ”<<xm<<“, ”<<ym; return 0; }
Review l l Declare an integer variable and initialize it to 8 Give an example of preprocessor directive What is the name of the function that every C++ program must have? If A is initialized to 5 and B is initialized to 7 what are the values of A and B after the following assignment operation: B=A; A= ____, B = _______.
Review - identify the operation l int num; declaration l num = 5; assignment l int num=5; assignment during declaration initialization
The grade distribution in a certain class is as follows: l l l HW – 30% - 100 points max Tests – 45% - 100 points max Labs - 25% - 100 points max Write an algorithm that will receive the scores in each category of one student and display his final grade (out of 100%).
- Slides: 23