130 likes | 197 Views
PHYS707: Special Topics. C++ Lectures. Lecture 1. Schedule of Lectures. Getting started -- #includes , namespace, main/return, etc ; compile/link/execute; basic I/O; simple formatting; variables ( int , double); assignment; property of ints ; simple math
E N D
PHYS707: Special Topics C++ Lectures Lecture 1
Schedule of Lectures Getting started -- #includes , namespace, main/return, etc; compile/link/execute; basic I/O; simple formatting; variables (int, double); assignment; property of ints; simple math More data types; complexclass; flow control (if-else, while, do-while, for); using predefined libraries (ROOT) File I/O; defining your own functions (call-by-value, overloading, call-by-reference) and classes Arrays, strings, vectors; dynamic arrays More about classes; templates and standard template library
Getting Started: Basic Structure of a program: // Description of what the program does // When the program was written and by whom // Date of last edit – and what was added // Include any libraries you may need #include <iostream> #include <math.h> using namespace std; // “declare” any “home-made” functions that will be used: double delta_nlj(double x[5],double n); double Enl(double n, double delta); // Begin the main program int main() { // define variables: double Es1_2,Ep1_2,Ep3_2,Ed3_2,Ed5_2,Ef5_2,Ef7_2; int n, nmin=10, nmax=50; // Start the actual programming: … …. // End the program: return 0; }
Sample Program // Classic “Hello” program // Written 2000 b.c. by Alley Oop // Last edited 06/28/2012 by B. DePaola // who transcribed it from cave art to present form #include <iostream> using namespace std; int main() { intcounter,maxnum=10; for(counter = 0; counter < maxnum; counter++) { cout << “Hello” << endl; } return 0; }
What’s Next?!compile/link/execute Suppose the program is saved in the file “hello.cpp”. Then: depaola@Depaolanote ~/cstuff/CPP_Class $ g++ hello.cpp –o hello $ ./hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello depaola@Depaolanote ~/cstuff/CPP_Class $
Variable Types Most commonly used variable types: Integer: inta,b,age=25; Double: double x,y,z=3.7; * * * Most commonly used types
C++: Object Oriented Language(Not just for Comp Sci!) // program to demonstrate use of classes // written 07/20/2012 by B. DePaola // NOTE: Must have file “matrix.h” #include "matrix.h" // this file contains the matrix // "class" using namespace std; intmain() { inti,j; matrix A(6,4), B(4,6), C(6,6); //Assign some numbers to the elements of A: for(i=1; i<=6;++i) for(j=1; j<=4; ++j) A(i,j)=i*j; // Assign some numbers to the elements of B: for(i=1; i<=4;++i) for(j=1; j<=6; ++j) B(i,j)=i*j; // Multiply A and B: C=A*B; // Wow! This is powerful! // output the result: cout << C; // This is powerful too! return (0); } Output: 60 90 120 150 180 120 180 240 300 360 180 270 360 450 540 240 360 480 600 720 150 300 450 600 750 900 180 360 540 720 900 1080
Examples of Computer Arithmetic inti=3,j=4,k; double x=3.0,y=4.0,z; k=i/j; z=i/j; cout << “z=“ << z << “ k=“ << k << endl; z=x/y; cout<< “z=“ << z << endl; z=x/j; cout << “z=“ << z << endl; z=1.0*i/j; cout << “z=“ << z << endl; z=1.0*(i/j); cout << “z=“ << z << endl; z=double(i/j); cout << “z=“ << z << endl; z=double(i)/double(j); cout << “z=“ << z << endl; Gives the following output: k=0 z=0 z=0.75 z=0.75 z=0.75 z=0 z=0 z=0.75
First Homework Write a program that solves the quadric equation: The solution is, of course, • Your program should ask you for a, b, and c. • It should test to see if the argument of the square root is negative • If the solution is real, the program should output the two roots • If the solution is not real, the program should just state this. (No need to solve; we’ll learn how to deal is complex number later) Due before next class (email me: depaola@phys.ksu.edu)
Formatting // program to demonstrate simple formatting // written 06/29/2012 by B. DePaola #include <iostream> using namespace std; int main() { constintimax=10; // something new here! inti; double TCelcius,TFehrenheit, TFmin=85.0; for(i=0; i<imax; i++) { TFehrenheit=TFmin+double(i); // what if I had written "TFehrenheit=TFmin+i;" ? TCelcius=(TFehrenheit-32.0)*5.0/9.0; cout << TFehrenheit << " degrees F = \t" << TCelcius << “ degrees C\n"; // note use of \t and \n } cout << "\n\tThat's All Folks!\n"; return 0; }
Output UGLY! BEAUTIFUL! 85 degrees F = 29.4444 degrees C 86 degrees F = 30 degrees C 87 degrees F = 30.5556 degrees C 88 degrees F = 31.1111 degrees C 89 degrees F = 31.6667 degrees C 90 degrees F = 32.2222 degrees C 91 degrees F = 32.7778 degrees C 92 degrees F = 33.3333 degrees C 93 degrees F = 33.8889 degrees C 94 degrees F = 34.4444 degrees C That's All Folks! 85.0 degrees F = 29.4 degrees C 86.0 degrees F = 30.0 degrees C 87.0 degrees F = 30.6 degrees C 88.0 degrees F = 31.1 degrees C 89.0 degrees F = 31.7 degrees C 90.0 degrees F = 32.2 degrees C 91.0 degrees F = 32.8 degrees C 92.0 degrees F = 33.3 degrees C 93.0 degrees F = 33.9 degrees C 94.0 degrees F = 34.4 degrees C That's All Folks! //set format of output: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(1); Just by adding: Before the couts For now, just treat this as black magic…
Other Stuff Funny expressions: 1) Variable Op= Expression Examples: sum += 4*y; => sum = sum+4*y; prod *= i+j; => prod = prod*(i+j); z /= y; => z = z/y; 2) Increment/decrement: Examples: i++; => i=i+1; r - -; => r=r-1; ++s; => s=s+1; - -p ; => p=p-1; But ++ or - -can be tricky: i = 3; j = 2*(i++); cout << “i= “<<i<<“ j=“<<j<<endl; Output: i=4 j=6 BUT: i = 3; j = 2*(++i); cout << “i= “<<i<<“ j=“<<j<<endl; Output: i=4 j=8 Input:cin Example: cout << “Input a number “; cin >> x; cout << endl;
Homework 2 Due before next class (email me: depaola@phys.ksu.edu) Write a program that computes how far an object falls in t seconds. Have your program ask for time (in seconds) and then use that time to compute the distance using the formula d = ½gt2. Hint: Either use the construction t*t for t2 or use the built-in power function: t2 can be computed as pow(t,2).