C/C++ Help Page
If you need any help with this - go to the forums with your question.
Section 1: Standard Input and Output
#include <cstdio> // C STandarD Input Output (I/O). Gives us printf and scanf // #... statements are called pre-processor statements int main() { printf( "Hello, World!\n" ); scanf( "*" ); return 0; }
Section 2: Control Structures
A control structure is not a statement, but it controls other statements i.e. under what conditions do the controlled statements run? Is it run in a loop while the condition is true?
if(condition) { printf("the condition is true\n"); } else { printf("the condition is false\n"); }
Section 3: Arrays and Strings
If you're writing a program for, say, a database, you want to have a way of quickly making many, many variables at once.
For example, if you were to write a program which asked for 50 people's ages, you wouldn't want something like this:
int age1; int age2; int age3; ... int age50;
Or even this shorthand version:
int age1, age2, age3, age4, age5, ..., age50;
In this case, you'll need to declare not one number, but a whole array of numbers.
The way we do this is using square brackets:
int age[50]; // Means "make 50 'age' variables"
To access (use) the variables, we use the same square bracket notation:
age[12] = 7; scanf("%d", &age[45]); // We can access the variable number based on another value: int n = 6; age[n] = 4; if(age[6] == 4) printf("age[6] is 4!\n"); // NOTE: With arrays, the numbering starts from 0, not 1. So // int array[10]; // will make 10 variables called "array", numbered array[0], array[1], array[2] ... up to array[9]. // This means that when you type "int array[10];" There will not be an array[10] variable, // but there will be 10 array[x]'s from 0 to 9 // You should now know enough about the syntax of C to understand what the following code does, // and how and why it works on a fundamental level. for(int i = 0; i < 50; i++) printf("age[%d] is %d\n", i, age[x]);
Section 4: Random etc.
#include <cstdio> // C STndarD Input Output (I/O): printf and scanf #include <cstdlib> // C STandarD LIBrary: Used for making random numbers #include <ctime> // C Time: for time(NULL) - so numbers are different each time the code runs int main() { srand(time(NULL)); // Change the random "seed" so that a different random number is produced. num1 = rand(); // Will be any random positive integer. num2 = rand() % 7; /* % is modulo function ( a % b is the remainder of a / b ) so a % b will always be a number between 0 and b-1 so rand() % x will always be a number between 0 and x-1 (Year 11 Maths C students might have heard of this) */ num3 = rand() % 10 + 1; // Will be between 1 and 10 (inclusive) }
For a list of tasks to test your knowledge, see the C++ page, or the Programming Challenges.
… If anything is missing or unclear, bring it up on the forums.