Home / Notes / Class 10 / Computer Science / Programming basics
Problem solving, algorithms and flowcharts, program structure in C, data types, variables, operators, input and output, decisions with if and switch, loops, and common errors, with short programs.
A program is a set of instructions written in a programming language to solve a problem. A programmer follows these steps: understand the problem, plan the solution as an algorithm, write the code, test and debug it, and document it. An algorithm is a step by step procedure that solves a problem in a finite number of steps.
A flowchart shows an algorithm with symbols:
Pseudocode is an algorithm written in simple English-like statements. For example: start, read two numbers, add them, print the sum, end. It is not tied to any language.
Types of programming error:
Structure of a simple C program:
Here is a complete example: #include <stdio.h> then int main(void) { int a = 7, b = 3; printf("Sum = %d\n", a + b); return 0; } This prints Sum = 10. The code %d is a format specifier for an integer and \n moves to a new line.
Data types and variables. A variable is a named memory location that stores a value, and it must be declared with a type before use:
A constant is a value that does not change. It can be defined with the const keyword, for example const float PI = 3.14;. A statement such as x = 5; is an assignment: the value on the right is stored in the variable on the left.
Operators:
Input is read with scanf and output is written with printf. To read an integer: scanf("%d", &num); The & sign gives the address of the variable and must be used with scanf for numbers and characters. Without the right format specifier, the value will be read or shown wrongly.
Decision making lets a program choose between actions:
Loops repeat a block of statements:
Study tip: Practise writing short programs by hand: sum of numbers, even or odd, largest of three numbers, table of a number, and factorial. Always include the header line, main, declarations, input prompt and return statement. Trace loops in a table of values, because dry runs are common in papers.
Say the meaning first, then open the card to check.
A finite, step by step method of solving a problem.
A diagram that shows an algorithm using symbols and arrows.
A named memory location that holds a value.
A mistake that breaks the grammar rules of the language.
A mistake that makes the program give wrong results.
A symbol that performs an operation on values.
A structure that repeats statements.
Finding and correcting errors in a program.
A program that translates source code to machine code.
These are the kind of questions asked for 2, 3 and 5 marks. Try each one before opening the answer.
An algorithm is a finite sequence of clear steps used to solve a problem. It must be unambiguous, with each step clearly stated, and it must end after a finite number of steps and produce the expected output.
A syntax error breaks the rules of the language, such as a missing semicolon, and the compiler refuses to build the program. A logical error is not detected by the compiler. The program runs but gives wrong output because the instructions are wrong, for example using + instead of *.
#include <stdio.h> int main(void) { int a, b; scanf("%d %d", &a, &b); printf("Product = %d\n", a * b); return 0; } The program declares two integers, reads them with scanf, multiplies them and displays the result with printf.
The oval marks the start and end of the flowchart. The diamond shows a decision with two outcomes, yes and no. The parallelogram shows input or output.
A while loop tests its condition before running the body, so the body may never run. A do while loop runs the body first and tests the condition afterwards, so the body runs at least once.
int stores whole numbers such as 45. float stores decimal numbers such as 2.5. char stores a single character such as 'B'. double stores decimal numbers with more precision such as 3.141592.
Answer: sum = 10
Answer: 17 / 5 = 3 and 17 % 5 = 2
Answer: Even