Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Material(s) we need
A text editor (gedit, or kate) Terminal or konsole
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
executable file
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
#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: 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
// 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
// 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
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
all:
g++ helloworld.cc o helloworld
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
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
$@ = target name
Equivalent to
$(PROG) = DotProduct
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)
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)
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.