Follow the exercises in order, refer to your lecture notes on a topic if you get stuck, and come see your instructor if you're still stuck.
#include <cstdio> int main() { // do some stuff return 0; }The program execution always starts in the main routine, and follows instructions in sequence until the 'return 0' is reached (at which point the program exits).
Our first instruction will be the 'printf' command, which is supplied for us via the cstdio library.
The syntax is printf("some text to display");
The text from multiple printf statements is tacked together
unless we ask for an end-of-line, or newline, to be displayed:
by adding a special newline character, represented with '\n',
at the end of the text:
printf("some text to display\n");
Exercises: write, compile, test, and debug seperate programs to:
The most basic data types available are int (for integer data), float (for real numbers), char (for single characters), and bool (for true/false values). There is also a string data type, provided for us in the string library (therefore we have to #include the string library to use strings).
The syntax is datatype variablename; e.g.
char myCharVar; string myStrVar; float myFloat; int myInteger; bool myBoolean;To assign values to a variable we use the syntax var = value; e.g.
myCharVar = 'c'; // characters in single quotes myStrVar = "blah"; // strings in double quotes myFloat = 3.14; // real numbers go in float variables myInteger = 17; // integer values go in int variables myBoolean = true; // true/false values in bool variablesREMEMBER: a variable must be declared in the main routine somewhere above the line where it is first actually used.
We can also give variables an initial value when we declare them,
using the syntax
datatype variablename = value;
Note the different syntax for assigning the different data types:
char myCharVariable = 'c'; string myStrVar = "blah"; float myFloat = 3.14; int myInteger = 17; bool myBoolean = true;We can also copy values from one variable to another, as long as they are of the same type, e.g.
int var1 = 17; int var2; var2 = 1; // var2 stores value 1 var2 = 34; // now var2 stores value 34 var2 = var1; // now var2 stores value 17, copied from var1Exercises: write, compile, test, and debug seperate programs to:
The syntax to read a value from the keyboard and store it in a
variable is:
scanf("%X", &variableName);
Here the X is replaced with different characters depending
on the data type of the variable: %c for char, %d for int,
%ld for long, %f for float, and %lf for double. (Reading in
strings is a bit trickier, we'll address that "soon".)
scanf looks at the data type of the variable and takes that into account when processing the user input. For instance, suppose for each of the input requests below the user typed 3 then hit enter:
char var1; int var2; float var3; scanf("%c", &var1); // if the user types 3, this stores the character '3' scanf("%d", &var2); // if the user types 3, this stores the integer 3 scanf("%f", &var3); // if the user types 3, this stores the float 3.0Remember, the variable must be declared in main before/above the point where you try and read a value into it.
You can also chain a sequence of %X placeholders together, e.g.
scanf("%c %d %f", &var1, &var2, &var3);
This would read one thing into var1, then one into var2, then
one into var3, etc.
Exercises: write, compile, test, and debug seperate programs to:
For ints, we can pad out the width of a displayed variable,
adding extra spaces on the left as necessary. This is done
by specifying the desired width after the %, e.g.
printf("Pad this int to be 10 spaces wide %10d\n", myVar);
(If the variable is already wider than the amount specified then
it just prints the whole variable value anyway - it doesn't truncate
it to fit.)
Similarly, for floats we can specify the width and then also how many decimal places after the . For example, %8.2f would pad the whole thing out to be 8 spaces wide, of which one would be the decimal point and two would be digits after the decimal point.
Exercises: write, compile, test, and debug seperate programs to:
We can also tell it to read and discard a particular kind of value using a * after the % sign, e.g. scanf("%*c"); means read and discard a single character. (Note that since the character is being discarded we don't need to give scanf a variable to store the value in.)
Note that attempting to divide a number by 0 will result in a crash!
With respect to division, x / y uses integer division if both x and y are integer values, floating point division otherwise (similar rules for %).
Modulo is often used for determining even or odd, e.g. x % 2 is 0 if x is even, 1 if x is odd.
Exercises: write, compile, test, and debug seperate programs to:
Each function has a unique name, and typically expects to be given one or more values (of specific data types) to process and may or may not return a value.
For example, the square root function is named sqrt, expects to be given a value to compute the square root of, and returns the identified square root.
The syntax is to provided the data to be processed in brackets,
and store the value the function returns, e.g.
result = sqrt(3.2); // compute root of 3.2, store in result
The cmath library includes routines such as sqrt, sin, cos, tan, etc.
The cctype library includes routines such as toupper, isalpha, isdigit, etc
The cstdlib library includes routines such as atoi, atof, etc
Many of the common library and function descriptions can be found here:
CPlusPlus.com
Exercises: write, compile, test, and debug seperate programs to:
The general idea is as follows:
if thing 1 is true then do this .... otherwise do this...The syntax is of the form
if (thing1 == true) { // ... stuff to do if true } else { // ... stuff to do otherwise }If whatever is in the brackets evaluates to true then the first block of code is run, otherwise the second is. Because of this, an equivalent, shorter, syntax is:
if (thing1) { // ... stuff to do if true } else { // ... stuff to do otherwise }We can create a sequence of options of the form
if (thing1) { // do this if thing1 is true } else if (thing2) { // if we didn't do thing1 AND thing2 is true, do this } else if (thing3) { // if we didn't do things 1 or 2 AND thing3 is true, do this } else { // do this if we didn't do any of the 3 above }For example, consider this grading example:
if (score > 85) { printf("A!\n"); } else if (score > 75) { printf("B\n"); } else if (score > 60) { printf("C\n"); } else { printf("uh oh\n"); }We can use the logical operators ! (not), && (and), and || (or) to combine logical expressions together. For example, to see if x is between y and z we could ask if x is greater than y AND less than z:
if (x < z) { if (y < x) { printf("%f < %f < %f\n", y, x, z); } else if (y > z) { printf("%f < %f < %f\n", x, z, y); } else { printf("%f < %f < %f\n", x, y, z); } } else { printf("%f is not less than %f\n", x, z); }
Exercises: write, compile, test, and debug seperate programs to: