Sei sulla pagina 1di 36

(C++) PROGRAMMING IN PHYSICS

Material(s) we need
A text editor (gedit, or kate) Terminal or konsole

Say Hi to simple Hello World Program


/* Program: helloworld.cc Author: Giovanni J Paylaga Date created: 6/27/2013 12:30PM Date modified: 6/27/2013 12:30PM NOTE: program simply prints out hello world */ #include <iostream> int main() { std::cout << Hello World! << std::endl; } # end of main

Say Hi to simple Hello World Program /* Header that defines the Program: helloworld.cc input/output Author: Giovanni J standard Paylaga Date created: 6/27/2013 stream 12:30PM objects Date modified: 6/27/2013 12:30PM (example: cin or cout) NOTE: program simply prints out hello world
*/ #include <iostream> int main() { std::cout << Hello World! << std::endl; } # end of main

Build and Run the Hello World Program


Build Command: g++ helloworld.cc o helloworld Run Command: ./helloworld g++ = C++ compiler helloworld.cc = source file helloworld = executable program

Build and Run the Hello World Program


Build Command: g++ helloworld.cc o helloworld Run Command: -o option creates ./helloworld

executable file

g++ = C++ compiler helloworld.cc = source file helloworld = executable program

What is the difference between:


int main() {} int main(int argc, char** argv) {} If a program is going to ignore command line arguments, choose the first line. Choose the second line if the program needs to process line arguments.

What is the difference between:


int main(int argc, char** argv) {} argc = argument count argv = argument vector Similar to
int main(int num_args, char** arg_strings) {}

What is the difference between:


#include <iostream> int main(int argc, char** argv) { std::cout << Have << argc << arguments: << td::endl; for(int i = 0; i < argc << i++) { std::cout << argv[i] << std::endl; } # end of for } # end of main

Compiling and running What is the difference between:


#include <iostream> int main(int argc, char** argv) { ./test a1 b2 c3 std::cout << Have << argc << arguments: << td::endl;

it with

will output:

for(int i = 0; i < argc << i++) { Have 4 std::cout << argv[i] << arguments: std::endl; } # end of for ./test } # end of main

a1 b2 c3

/* Program: masstoenergy.cc Author: Giovanni J Paylaga Date created: 6/27/2013 12:30PM Date modified: 6/27/2013 12:30PM NOTE: converts mass to energy, E = m*c^2 */ #include <iostream> int main(int argc, char** argv) { float m, E; // variable m = mass, E = energy const float c = 3e8; // c = speed of light in m/s m = atof(argv[1]); // atof converts string into float E = m*c*c; std::cout << Object with rest mass: << m << kg is equivalent to energy of: << E << joules << std::endl; return 0; } # end of main

/* Program: masstoenergy.cc Compile it with: Author: Giovanni J Paylaga

Datemasstoenergy.cc created: 6/27/2013 12:30PM g++ o masstoenergy


*/

and then run it by:


./masstoenergy 0.005

Date modified: 6/27/2013 12:30PM NOTE: converts mass to energy, E = m*c^2

#include <iostream>

int main(int argc, char** argv) { float m, E; // variable m = mass, E = energy const float c = 3e8; // c = speed of light in m/s m = atof(argv[1]); // atof converts string into float E = m*c*c; std::cout << Object with rest mass: << m << kg is equivalent to energy of: << E << joules << std::endl; return 0; } # end of main

Header, Source, & main file


Suppose we want a program that calculates the dot product of two vectors A and B. Needed Files: MAIN: VectorDotProduct_main.cc HEADER: CalculateDotProduct.hh SOURCE: CalculateDotProduct.cc
Article: Header and Includes: Why and How, Link: www.cplusplus.com/forum/articles/10627/

Header, Source, & main file


Suppose we want a program that calculates the dot product of two vectors A and B. Needed Files: MAIN: VectorDotProduct_main.cc HEADER: CalculateDotProduct.hh SOURCE: CalculateDotProduct.cc
Article: Header and Includes: Why and How, Link: www.cplusplus.com/forum/articles/10627/

Interface & Implementation

/*

Program: VectorDotProduct_main.cc Author: Giovanni J Paylaga Date created: 6/27/2013 12:30PM Date modified: 6/27/2013 12:30PM NOTE: calculate the dotproduct of two vectors */ #include <iostream>

VectorDotProduct_main.cc

int main(int argc, char** argv) { // define two vectors A and B const int ndim = 3; // in 3-dimenasions double vectorA[ndim] = {1.0, 1.5, 2.0}; double vectorB[ndim] = {3.0, 4.0, 5.0}; double dotproduct = 0.0; // calcuate the dotproduct for(int i = 0; i < ndim; i++) { dotproduct += vectorA[i] * vectorB[i]; } // print out results std::cout << A dot B = << dotproduct << std::endl; return 0; } # end of main

/*

Program: VectorDotProduct_main.cc Author: Giovanni J Paylaga Date created: 6/27/2013 12:30PM Date modified: 6/27/2013 12:30PM NOTE: calculate the dotproduct of two vectors */ #include <iostream>

VectorDotProduct_main.cc

int main(int argc, char** argv) { // define two vectors A and B const int ndim = 3; // in 3-dimenasions double vectorA[ndim] = {1.0, 1.5, 2.0}; double vectorB[ndim] = {3.0, 4.0, 5.0}; double dotproduct = 0.0;

BAD PRACTICE

// calcuate the dotproduct for(int i = 0; i < ndim; i++) { dotproduct += vectorA[i] * vectorB[i]; } // print out results std::cout << A dot B = << dotproduct << std::endl; return 0; } # end of main

/*

Program: VectorDotProduct_main.cc Author: Giovanni J Paylaga Date created: 6/27/2013 12:30PM Date modified: 6/27/2013 12:30PM NOTE: calculate the dotproduct of two vectors */ #include <iostream> #include CalculateDotProduct.hh int main(int argc, char** argv) { // define two vectors A and B const int ndim = 3; // in 3-dimenasions double vectorA[ndim] = {1.0, 1.5, 2.0}; double vectorB[ndim] = {3.0, 4.0, 5.0}; double dotproduct = 0.0;

VectorDotProduct_main.cc

GOOD PRACTICE

GOOD PRACTICE

// calcuate the dotproduct dotproduct = CalculateDotProduct(vectorA, vectorB);

// print out results std::cout << A dot B = << dotproduct << std::endl; return 0; } # end of main

/*

Program: VectorDotProduct_main.cc Author: Giovanni J Paylaga Date created: 6/27/2013 12:30PM Date modified: 6/27/2013 12:30PM NOTE: calculate the dotproduct of two vectors */ #include <iostream> #include CalculateDotProduct.hh int main(int argc, char** argv) { // define two vectors A and B const int ndim = 3; // in 3-dimenasions double vectorA[ndim] = {1.0, 1.5, 2.0}; double vectorB[ndim] = {3.0, 4.0, 5.0}; double dotproduct = 0.0;

VectorDotProduct_main.cc

// calcuate the dotproduct dotproduct = CalculateDotProduct(vectorA, vectorB);

// print out results std::cout << A dot B = << dotproduct << std::endl; return 0; } # end of main

/*

CalculateDotProduct.hh
Program: CalculateDotProduct.hh Author: Giovanni J Paylaga Date created: 6/27/2013 12:30PM Date modified: 6/27/2013 12:30PM NOTE: header file that calculate the dotproduct of two vectors

*/ #ifndef _CALCULATE_DOT_PRODUCT_HH #define CALCULATE_DOT_PRODUCT_HH double CalculateDotProduct(double *vector1, double *vector2); #endif

/*

CalculateDotProduct.hh
Program: CalculateDotProduct.hh Author: Giovanni J Paylaga Date created: 6/27/2013 12:30PM Date modified: 6/27/2013 12:30PM NOTE: header file that calculate the dotproduct of two vectors

*/ #ifndef _CALCULATE_DOT_PRODUCT_HH #define CALCULATE_DOT_PRODUCT_HH double CalculateDotProduct(double *vector1, double *vector2); #endif

Interface
Forward declaration of variables, functions and classes

/*

CalculateDotProduct.cc
Program: CalculateDotProduct.cc Author: Giovanni J Paylaga Date created: 6/27/2013 12:30PM Date modified: 6/27/2013 12:30PM NOTE: source file that calculate the dotproduct of two vectors

*/ #include CalculateDotProduct.hh double CalculateDotProduct(double *vector1, double *vector2) { // calcuate the dotproduct double result = 0.0; result = CalculateDotProduct(vectorA, vectorB); return result; } # end of CalculateDotProduct

/*

CalculateDotProduct.cc
Program: CalculateDotProduct.cc Author: Giovanni J Paylaga Date created: 6/27/2013 12:30PM Date modified: 6/27/2013 12:30PM NOTE: source file that calculate the dotproduct of two vectors

*/ #include CalculateDotProduct.hh

Implementation

double CalculateDotProduct(double *vector1, double *vector2) { // calcuate the dotproduct double result = 0.0; result = CalculateDotProduct(vectorA, vectorB); return result; } # end of CalculateDotProduct

Running our Program


Now we have the Needed Files: MAIN: VectorDotProduct_main.cc HEADER: CalculateDotProduct.hh SOURCE: CalculateDotProduct.cc
Enter these commands:
g++ -c VectorDotProduct_main.cc g++ -c I ./ CalculateDotProduct.cc g++ VectorDotProduct_main.o CalculateDotProduct.o o DotProduct

First line will create Running our Program an object file Now we have the Needed Files: VectorDotProduct_main.o MAIN: VectorDotProduct_main.cc (a.k.a. library file) HEADER: CalculateDotProduct.hh NOTE: there is no SOURCE: CalculateDotProduct.cc executable file yet.
Enter these commands:
g++ -c VectorDotProduct_main.cc g++ -c I ./ CalculateDotProduct.cc g++ VectorDotProduct_main.o CalculateDotProduct.o o DotProduct

second line will compile Running oursource Program file into an object Now we havefile the CalculateDotProduct.o Needed Files:

MAIN: VectorDotProduct_main.cc NOTE: compiler will search HEADER: CalculateDotProduct.hh for the header file SOURCE: CalculateDotProduct.cc
g++ -c VectorDotProduct_main.cc g++ -c I ./ CalculateDotProduct.cc g++ VectorDotProduct_main.o CalculateDotProduct.o o DotProduct

HOW? By adding -I ./ Where ./ = directory where your Enter these commands: header is.

Last line will link the Running our Program executable program Now we have DotProduct the Needed to Files: the object files (*.o files) MAIN: VectorDotProduct_main.cc

HEADER: CalculateDotProduct.hh Note: -o lets you generate the executable file SOURCE: CalculateDotProduct.cc
You can then run it: Enter these commands:
g++ -c VectorDotProduct_main.cc g++ -c I ./ CalculateDotProduct.cc g++ VectorDotProduct_main.o CalculateDotProduct.o o DotProduct

./DotProduct

Make you life easier by using Makefiles


Makefiles special format files utilized by make to automatically create and manage applications Command: make -f yourMakeFile
Example: place this inside yourMakeFile: all: g++ helloworld.cc o helloworld Then type/execute the command

Make you life easier by using Makefiles


Makefiles Format:
[target] = dependencies [tab] system command
Example:

all:
g++ helloworld.cc o helloworld

Make you life easier by using Makefiles


Makefiles Format:
[target] = dependencies [tab] system command
Example: UTILIZiNG dependencies

all: helloworld helloworld: main.o helloworld.o g++ main.o hello.o o helloworld main.o: main.cc g++ -c main.cc helloworld.o: helloworld.ccc g++ -c helloworld.cc clean: rm rf *o helloworld

Make you life easier by using Makefiles


Using variables $(VAR) and comments # or /**/
# I am a comment, let variable CC = compiler to use CC = g++ /* CFLAGS will be the options thatll be passed to the compilers */ CFLAGS = -c Wall Wno-deprecated all: helloworld helloworld: main.o helloworld.o $(CC) main.o hello.o o helloworld main.o: main.cc $(CC) $(CFLAGS) main.cc helloworld.o: helloworld.ccc $(CC) $(CFLAGS) helloworld.cc clean: rm rf *o helloworld

Recall our VectorDotProduct program # Makefile


# Author: Giovanni J Paylaga CC = g++ CFLAGS = -c Wall Wno-deprecated SRCS OBJS LIBS INCS = = = = VectorDotProduct_main.cc CalculateDotProduct.cc VectorDotProduct_main.o CalculateDotProduct.o -L ./ -I ./

PROG = DotProduct all:$(PROG) $(PROG):$(OBJS) $(CC) $(CFLAGS) $^ $(LIBS) o $@ .cc.o: $(CC) $(CFLAGS) $(INCS) -c $< -o $@ clean: rm rf *.o $(PROG)

Recall our VectorDotProduct program # Makefile


# Author: Giovanni J Paylaga CC = g++ CFLAGS = -c Wall Wno-deprecated SRCS OBJS LIBS INCS = = = = VectorDotProduct_main.cc CalculateDotProduct.cc VectorDotProduct_main.o CalculateDotProduct.o -L ./ -I ./

PROG = DotProduct all:$(PROG) $(PROG):$(OBJS) $(CC) $(CFLAGS) $^ $(LIBS) o $@ .cc.o:

$(CC) $(CFLAGS) $(INCS) -c $< -o $@ clean: rm rf *.o $(PROG)

$^ = all dependencies
Equivalent to

VectorDotProduct_main.o CalculateDotProduct.o

Recall our VectorDotProduct program # Makefile


# Author: Giovanni J Paylaga CC = g++ CFLAGS = -c Wall Wno-deprecated SRCS OBJS LIBS INCS = = = = VectorDotProduct_main.cc CalculateDotProduct.cc VectorDotProduct_main.o CalculateDotProduct.o -L ./ -I ./

PROG = DotProduct all:$(PROG) $(PROG):$(OBJS) $(CC) $(CFLAGS) $^ $(LIBS) o $@ .cc.o:

$(CC) $(CFLAGS) $(INCS) -c $< -o $@ clean: rm rf *.o $(PROG)

$@ = target name
Equivalent to
$(PROG) = DotProduct

Recall our VectorDotProduct program # Makefile


# Author: Giovanni J Paylaga

$< = first dependency

CC = g++ .cc.o = CFLAGS = -c Wall Wno-deprecated

This is a suffix replacement rule for building SRCS = VectorDotProduct_main.cc CalculateDotProduct.cc .os from .ccs. OBJS = VectorDotProduct_main.o CalculateDotProduct.o It uses automatic variables $<: LIBS = -L ./ INCS = -I ./ name of the dependency of the rule (.cc file) and $@: the name PROG = DotProduct of the target of the rule (.o all:$(PROG) file)
$(PROG):$(OBJS) $(CC) $(CFLAGS) $^ $(LIBS) o $@ .cc.o: $(CC) $(CFLAGS) $(INCS) -c $< -o $@ clean: rm rf *.o $(PROG)

Write a program that will calculate the vertical and horizontal

Exercises:

positions (in meters) and components of the velocity vector (in m/s) of a projectile at any given time (value of time is asked at run time). Use the following conditions: y0 = 5m v0 = 20m/s
Write a program to sum the series:

x0 = 0 m theta= 30o.

=
=1

2 sin() 1 (1)

let n = 100, x=0.5

Write a program that calculates the dot product of n-dimensional

vectors. It asks the user at run time for two vectors and determines if they have the same dimensions. It informs the user about the dimension of each vector and prints out their dot product.

Assignment
Write a program that will calculate the mean and standard

deviation of a set of data. It asks the user at run time the number of data points.

Potrebbero piacerti anche