TSR Wiki > Life > Computing and Technology


Techwikishort.png

Contents

General Advice

It is important to develop your ideas while learning programming languages and to constantly use the new knowledge you acquire. There is little to gain from reading the literature and completing the exercises if you don't actually put into practice what you've learned. It is also worthwhile to experiment with the operators and operands of the language without any real direction other than to see whether what you're trying is permissible. This way you can fully understand the language, to a greater depth than the authors and even inventors themselves. This is why Development Environments such as IDLE for python are used; they allow the programmer to see the direct consequence of input which saves a lot of time reading literature.

An EXTREMELY important idea to keep in your mind is that Coding is not the same as Programming. When learning programming, do not merely focus on the syntax and the particular code that is used in a program, but try to learn and truly understand the basic concepts behind programming. These include but are not limited to the understanding of the following concepts:

1. Different programming paradigms

2. Data Structures

3. Conditionals and Loops

4. Object-oriented programming concepts (Classes, Objects, Inheritance etc.)

Once you really understand these concepts, the particular language you learn will become irrelevant as you'll be able to apply your understanding to all languages with some changes in syntax and logic of course.

Program Structure

It is a good idea to keep your programs tidy with regular comments that explain the program to a human reader, this way after a long time has passed you will still be able to see what the program does and so will any others whom you share your program with. Below is a general layout.


  1. Program description.
  1. Programmer name(s).
  1. Date when it was written.
  1. Content, with descriptive comments.

Depending on which language you use whitespace is treated differently. When you are free to use whitespace however you see fit, it is good practice to indent the text within a function as it is much easier to read.

Languages

C

Language description

C is general purpose and procedural. It was invented in order to implement the Unix operating system and utilities with as much hardware independence as possible.

Example programs

Hello World

#include <stdio.h>

int main(void) {
	printf("Hello, World!");
	return 0;
}

External Tutorials and Books

Compilers and/or IDEs

C++

Language description

Object-oriented, strongly typed and supports multiple paradigms, built on top of the C programming language. Contains data structures, classes, inheritance and functions.

While-loop syntax

A While loop contains a condition in its parameters that when false terminates the loop. Here is the layout:

statement
while (condition){
statement(s) & expressions
}

The first statement must satisfy the condition if the scope is to ever be executed. Furthermore, the statement(s) and expressions must eventually cause the condition to be false if the while loop is to ever terminate, this is commonly done by incrementing a variable using "++" next to it (hence the name c++). This kind of incrementing is so common that a new function, the for loop, was created.

For-loop syntax

for (statement; condition; statement2){
statement(s) & expressions
}

This is essentially the same as the while loop, except that statement2 would have previously been contained inside the while-loop's scope.

Functions

Functions can be with or without parameters. Below is a format for a function:

type1 function_name(type2 parameter1, type3 parameter2,...typeN parameter N-1)
{
statements_involving_all_parameters
}

The types are obviously your int, float, double std::string, bool, vectors etcetera. Every time you call function_name in the same scope or 'subscopes' with valid arguments it will execute statements_involving_all_parameters supplying your stated arguments.

Example programs

Some of these are derivatives of the Exercises in Accelerated C++, so they may be spoilers for some of you if you intend to complete the exercises in them independently.

Hello World

#include <iostream>

int main() {
	std::cout << "Hello World!";
	return 0;
}

Counter

/* this program counts from 10 to -5 */
#include <iostream>
int main()
{
int a;
for (a=10;a!=-6;a--){std::cout << a << std::endl;}
return 0;
}

Average of three scores

#include <iostream>
int main(){
unsigned int FIRST,SECOND,THIRD;
std::cout << "Enter the first score: " << std::endl;
std::cin >> FIRST;
std::cout << "Enter the second score: " << std::endl;
std::cin >> SECOND;
std::cout << "Enter the third score: " << std::endl;
std::cin >> THIRD;
int AVERAGE = (FIRST + SECOND + THIRD)/3;
std::cout << "The average score to the nearest integer is " << AVERAGE << ". " << std::endl;
return 0;
}

Multiplier

/* this program calculates the product of the numbers [1,10) */
#include <iostream>
int main()
{
unsigned int a = 1; // counter
unsigned int b = 1; // where the product is stored
for (a=1;a!=10;++a){
b*=a;
}
std::cout << b << std::endl;
return 0;
}

Character counter

/* this program searches within a string of text for a character and counts how many times it appears */
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;
using std::endl;
int main()
{
// request string to search within
cout << "Enter the string of text to search within: " << endl; 
string y; 
cin >> y;

// request character to search for
cout << "Enter the character to search for: " << endl;
char x; 
cin >> x; 

// count the number of x in y
unsigned int a;
unsigned int b=0;
for (a=0;a!=y.size();a++){
	if (y[a]==x){b++;}}

// tell user occurances of character
cout << b << endl;
return 0;
}

A simple function

#include <iostream>

void welcome(){std::cout << "Greetings, from a simple function" << std::endl;}

int main(){
welcome();
return 0;
}

External Tutorials and books

  1. http://www.cplusplus.com/ - Uses descriptions and examples to teach C++
  1. Accelerated C++ by Andrew Koenig and Barbara E. Moo - Uses examples, descriptions and exercises to show you how to solve problems in C++. Starts off quickly. Does not require knowledge of C.
  1. The C++ Programming Language by Bjarne Stroustrup - Teaches the entire language
  1. Beginning C++ Through Game Programming, 3rd Edition by Micheal Dawson

Compilers and/or IDEs

For gcc the basic command to compile file.c into file is:

g++ file.c -o file

GCC usually gives very useful error messages unless you've made a huge mistake.

For other GCC-like compilers, such as the popular Clang compiler, g++ can be substituted for clang as the program arguments emulate that of the GCC tools.

Bash

Language description

Tutorial

Hello World

echo Hello World!

External Tutorials and Books

Compilers and/or IDEs

Python

Language description

Tutorial

Hello World

Python 3.x:

print("Hello World")

Earlier versions:

print 'Hello World'

External Tutorials and Books

  1. http://docs.python.org/tutorial/
  2. Learn Python The Hard Way by Zed A. Shaw
  3. http://www.khanacademy.org/#computer-science
  4. Learn to code | The code academy

Compilers and/or IDEs

IDLE is a cross-platform IDE that provides and interactive environment so that you can experiment with new functions, it also has a useful help facility.

Matlab

Language description

"MATLAB is a programming environment for algorithm development, data analysis, visualization, and numerical computation. Using MATLAB, you can solve technical computing problems faster than with traditional programming languages, such as C, C++, and Fortran." - http://www.mathworks.co.uk/products/matlab/

Tutorial

Hello World

External Tutorials and Books

Compilers and/or IDEs

Try Learn together, TSR's study area

35,633
revision notes

39,210
mindmaps

39,573
crosswords

15,182
quizzes

create
a study planner

thousands
of discussions


Today on TSR
Poll
Would you rather have...?
Study resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE