There are two main tasks for this lesson:
Programming EnvironmentThe following will describe basic procedure to start up a programming project in each environment:
Version ControlPurpose:
While you're working on your tutorial materials or assignments, you might primarily do your work on your own computer (home desktop or notebook). When you're working on your computer, you might not be in the situation that you do not have access to the network to interact with a version control server (repository). Despite the lack of network connection, you probably still want to use the versioning system. Hence, we encourage you to use git, which is a distributed version control system, rather than subversion. How to version control...Template#include <iostream> using namespace std; //main() function without args int main() { cout << "This is a C++ main without args" << endl; return 0; } #include <iostream> using namespace std; //main() function with args int main(int argc, char *argv[]) { cout << "This is a C++ main with args" << endl; return 0; }
Practice 0When you write a program, you need to use a text editor (your favorit editor or one provided by your programming environment such as Visual Studio). C++ is a programming language, which is readable and understandable by humans. This form of program is often called "source program". A text file containing this source program is called "source file". Once you created a source file and typed a source program, you save the source file with its name. A typical extension of C++ source files is .cpp. (such as helloworld.cpp )Once you created a source file, you need to compile it to produce an executable file (such as helloworld.exe ). Then you can execute this file.CommentA line starting with // is a comment line. This is for assisting people, who read the source program, to understand the code. Whatever written as a comment does not affect actual program code (executable code). You can also write a comments between /*....*/ .header and incudeA line starting with #include is substituted with the content of <iostream> . After this #include, you can use various variables and functions defined in <iostream> . There are many other useful variables and functions defined in<string> , <cstdlib> . These are often called "header".Practice0-1 (without header)Remove #include <iostream> from your helloworld.cpp and see what will happen. Display output and output streamIn order to output something to a display (console) and files, you use stream. A stream is connected to the target output and you just need to throw things into the stream to output. cout is connected to the standard output (console) and is called "standard output stream". To output letters to cout you insert letters to it using << (inserter). StringIn C++, a set of letters like "Hello World." is called "string literal" (or string). You sometime see \n at the end of string and it represents "carriage return". Practice 0-2Write a code which does:
The value of x is 34. The value of y is 44.
The sum of x and y is 78. The average of x and y is 39. Practice 0-3Write a code which reads a number being typed from the keyboard and displays it. Practice 0-4Write a code which reads a number being typed from the keyboard and displays its absolute value. Practice 1 (C++ and std)Try the following code. If it does not work as you intended, discuss what's wrong and how you could fix it. // compute an average of elements from two arrays #include <iostream> int main() { int narray1[]=(16,22,38,46,51,69,70,88,95,102); int narray2[]={13,29,33,45,57,62,75,84,91,105}; int size = sizeof narray1 / sizeof(int); for (int i = 0; i < size; i++) { cout << "The average of "; << "narray1[" << i << "] and narray2[" << i << "]: "; << (narray1[i] + narray2[i]) / 2 << endl; } return 0; }
Practice 2 (Scope Resolution Operator)Try the following code. If it does not work as you intended, discuss what's wrong and how you could fix it. /* find max and min value of three arrays */ #include <iostream> int main() { double narray1[] = { 16.21, 22.50, 38.33, 46.00, 51.71, 69.09, 70.89, 88.05, 95.77, 102.01 }; double narray2[] = { 13.44, 29.78, 33.45, 45.03, 57.66, 62.07, 75.66, 84.00, 91.82, 106.00 }; double narray3[] = { 10.05, 29.44, 34.35, 43.56, 55.25, 69.44, 72.00, 89.80, 97.45, 105.02 }; int size = sizeof narray1 / sizeof(double), i; std::cout << std::showpoint; // find the max for (i = 0; i < size; i++) { std::cout << "The max value of " << narray1[i] << ',' << narray2[i] << ',' << narray3[i] << " : " << ((narray1[i] > narray2[i] ? narray1[i]:narray2[i]) > (narray2[i] > narray3[i] ? narray2[i]:narray3[i]) ? (narray1[i] > narray2[i] ? narray1[i]:narray2[i]): (narray2[i] > narray3[i] ? narray2[i]:narray3[i])) << std::endl; } std::cout << std::noshowpoint; // find the min for (i = 0; i < size; i++) { std::cout << "The min value of " << narray1[i] << ',' << narray2[i] << ',' << narray3[i] << " : " << ((narray1[i] < narray2[i] ? narray1[i]:narray2[i]) < (narray2[i] < narray3[i] ? narray2[i]:narray3[i]) ? (narray1[i] < narray2[i] ? narray1[i]:narray2[i]): (narray2[i] < narray3[i] ? narray2[i]:narray3[i])) << std::endl; } return 0; } You can do one of followings:
Practice 3 (static keyword)Try the following code. If it does not work as you intended, discuss what's wrong and how you could fix it. /* reverse pointers */ #include <iostream> // declare a const visible only within this file static const int size = 10; int main() { int narray[] = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 }; int *pnarray[size], i, j; for (i = 0, j = size - 1; i < size; i++, j--) pnarray[j] = &narray[i]; for (j = 0; j < size; j++) std::cout << "pnarray[" << j << "] is : " << *pnarray[j] << std::endl; return 0; } If you need to define something(s), which is(are) visible only within the source file, you should use anonymous name space. namespace { //identifies only visibile with in the file ... } Practice 4 (printf())Try the following code. If it does not work as you intended, discuss what's wrong and how you could fix it. /* Simple division */ #include <iostream> using namespace std; // anonymous name space for "file scope" namespace { // a function to handle error. void errorexit(char *pchar) { // display an error to the standard err. fprintf(stderr, pchar); fprintf(stderr, "\n"); exit(1); } } int main() { int n1, n2; cout << " [Simple Division Program]\n" << "Type in the first integer: "; // get the first input. cin >> n1; cout << "Type in the second integer: "; // get the second input. cin >> n2; if (n2 == 0) { errorexit("Divided by zero."); } else { cout << n1 << " / " << n2 << " = " << n1 / n2 << endl; } return 0; } |