LAB : C++ Programming Basics - Texas Advanced Computing Center
LAB : C++ Programming Basics - Texas Advanced Computing Center
LAB : C++ Programming Basics - Texas Advanced Computing Center
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>LAB</strong> : <strong>C++</strong> <strong>Programming</strong> <strong>Basics</strong><br />
Ritu Arora<br />
<strong>Texas</strong> <strong>Advanced</strong> <strong>Computing</strong> <strong>Center</strong><br />
The University of <strong>Texas</strong> at Austin<br />
Email: rauta@tacc.utexas.edu<br />
November 8 th , 2011
Introduction<br />
• You will learn<br />
– How to write <strong>C++</strong> code<br />
– How to compile and execute <strong>C++</strong> code<br />
• What will you do<br />
– Modify the code for the exercises to embed logic in it<br />
– Compile and execute the code for the programs discussed<br />
in the lecture and exercises
Accessing Lab Files<br />
• Log on to Ranger using your_login_name<br />
• Uncompress the tar file, trainingCPP.tar , that is located in the<br />
~train00 directory into your HOME directory.<br />
• Switch to the subdirectory Exercise within the directory<br />
trainingCPP<br />
This is your existing<br />
login, or the portal login<br />
(and password) you<br />
recently created.<br />
ssh @ranger.tacc.utexas.edu<br />
tar -xvf ~train00/trainingCPP.tar<br />
cd trainingCPP/Exercise
Exercise 1: fct1.cc<br />
• Objective: Understand the basics of writing functions<br />
• Make sure you are in the directory Exercise<br />
• Open file fct1.cc<br />
– vi fct1.cc<br />
• Follow the instructions in the comments<br />
– add an appropriate return type for the function named addition<br />
– add an appropriate return statement in function addition;<br />
– print the value of variable c to the console<br />
• Once you have completed the code<br />
– Compile the code with the intel compiler<br />
icpc -o fct1 fct1.cc<br />
– Fix errors, if any<br />
– Run the executable<br />
./fct1
Exercise 2: minVal.cc<br />
• Objective: Learn how to declare variables, read values from the<br />
keyboard, write values to the console, and invoke functions<br />
• Function minFct is provided to you. This function takes two<br />
integers as input and returns the minimum of the two<br />
• Modify the code in the file minVal.cc to<br />
– declare three integers with the names: number1, number2, minValue<br />
– prompt the user to enter two integer values<br />
– read the integer values entered through the keyboard and store them in<br />
number1 and number2<br />
– invoke the function minFct and pass the values read from the keyboard<br />
Remember to store the value returned by minFct function in the variable<br />
minValue<br />
– print the minimum value to the console<br />
• Save the file, compile and run:<br />
icpc –o minVal minVal.cc<br />
./minVal
Exercise 3: fct2.cc<br />
• Objective: Learn about access-specifiers, class memberfunction<br />
declaration and definition<br />
• Modify fct2.cc – follow the comments in the code<br />
– add the access-specifier : public in the class GradeBook for all<br />
the member functions<br />
– write the definition of the function getBookName which is a<br />
member of the class GradeBook. The signature of the function is<br />
provided in the class-definition.<br />
– this function should return the string value stored in the class<br />
variable named courseBook<br />
– read the course name entered through keyboard. Make sure that<br />
spaces in the course name are allowed
Exercise 3: fct2.cc<br />
• Compile and run the code<br />
– icpc -o fct2 fct2.cc<br />
– ./fct2