You are on page 1of 18

C Programming

Week 1: An Introduction to Programming


Class 1: Computer Architecture
Lecture Goals

 To Introduce some basic computer terms and theory.

 Lecture contents
Introduction to computers and computer programming

“Hello World” program

The Software development method

2
Computer Basics

Input Process Output

Storage

 Hardware - the physical machine


 Software - what makes the hardware work
 programs and data
 Types of programs
 systems software
 “packaged” software
 customised software
3
Computer Software

 Problems - work, communication, transport, finance - all


needing a computer solution
 Computer only recognises zeros and ones (binary
system)
 Software allows people to communicate the problem to
the computer and the computer to communicate the
solution back

A program is a set
of instructions
designed to solve
problems
4
Some computer terms

Program: List of instructions for a computer to follow in


order to solve a given task
currency exchange
calculating grades for students
finding the average of a list of numbers

Programming: The process or activity of composing a list


of instructions for a computer to follow to solve a given
problem

Note: usually the instructions can be followed more than


once

5
Some more computer terms

File: A collection of information stored on a computer and


referred to by a filename (e.g. hello.c login.com)
Editor: A program available on the computer that allows
you to create and modify files
Program: A list of coded instructions given to a computer
enabling it to process data. A program is contained in a a
file (e.g. hello.c)
Account: Personal space on a computer to store your files,
and a password to protect and allow you to access your
files.
6
Computer Programs

 Computer processes in native machine language


 uses binary to represent the most basic computer operations
 Humans use programming languages
 written in an English-type language
 use familiar terminology and notation
 variety of languages to suit specific needs
 …e.g. BASIC, COBOL, FORTRAN, C, Java
 Need a translator program to convert from programming
language to machine code
 can be a compiler
 ….. or an interpreter

7
Computer Programs

Compiler: A program that converts (translates) a program


in a high level language into an object file (e.g. hello.obj)
Linker: A program that links object files to produce a
binary file (e.g. hello.exe)
Language: A set of commands used to write computer
programs (e.g. Pascal, C, Fortran, BASIC, assembler)

8
1. /* This is a program in C. It prints out the message Hello World on the
computer terminal
2. */

3. #include <stdio.h>

4. void main()

5. {

6. /* Display the message on the screen */

7. printf("Hello World\n");

8. }

9
Key in the program

Source file

Compile program

Object file
Error?
Warning?
Link/load the program

Executable file

Doesn’t work?

Execute the program 10


The Software Development Method

Systems
engineering Define the problem

Analysis
Plan the solution
Design
Implement the solution
Code

Testing

Maintenance
11
1. Specify the problem requirements
find out what user wants

2. Analyse the Problem


think about the problem & understand it

3. Design the algorithm to solve the problem


write down the steps needed to solve the problem

4. Implement the algorithm


write the algorithm as a program in C

5. Test and verify the completed program


make sure the program does what it is meant to do

6. Maintain and update the program


Fix any bugs previously undetected, make any changes
required, keep it up-to-date
12
Calculate the cost of a bag of apples
 Specify the problem requirements
 Compute and display the total cost of apples, given the number of kg.
of apples purchased and the cost per kg. of apples.

 Analyse the Problem


 Inputs - number of kg. purchased (kgs), cost per kg (£ per kg)
 Outputs - result: total cost of apples (£s)

Computation: total cost = cost per kg * kgs of apples
 Design the algorithm to solve the problem
 Get the data (kg. of apples, cost per kg.)
 Perform computation
 Display the result (total cost)

13
Calculate the cost of a bag of apples

 Implement the algorithm


 Test and verify the completed program
 Maintain and update the program

14
1. /* This program calculate the cost of apples, given the quantity purchased and the cost per *
pound.
2. */
3. #include <stdio.h>

4. void main()
5. {
6. int quantity; /* apples bought in kg.*/
7. double unit_cost, total_cost; /* unit cost in £ per kg. total cost in £s */

8. printf("How many pounds of apples were bought?\n"); /* Ask for quantity purchased */
9. scanf("%d",&quantity); /* Get quantity from user */

10. printf("What is the cost per pound of apples?\n"); /* Ask for unit cost */
11. scanf("%lf",&unit_cost); /* Get unit cost from user */
12.
13. total_cost = quantity * unit_cost; /* Calculate the cost */
14. printf("The total cost of your apples is : %f\n",total_cost); /* display total cost */

15. }

15
Summary

Program: List of instructions for a computer to follow in order to


solve a given task
Compiler: A program that converts (translates) a program in a high
level language into an object file (e.g. hello.obj)

Software development method


1. Systems engineering
2. Analysis
3. Design
4. Code
5. Testing
6. Maintenance

16
Review Questions

 A program that converts (translates) a program in a high


level language into an object file is called a _______.
compiler

 A collection of information stored on a computer is


file
known as a _______.

 What are the 6 steps in the software development


method?
1. Systems engineering
2. Analysis
3. Design
4. Code
5. Testing
6. Maintenance 17
Next Class

 The C programming language

 A brief history of C
 Variables
 Integer arithmetic
 /* Comments in C */

18

You might also like