COMP 1100 – Computer Programming 1 Lecture 1: Problem Solving using Computers • Architecture of a Computer • Illustration of a Problem Science starts only with problems - Karl Popper • How a Computer Solves a Problem • Algorithms • Problem Statement • Input/Output Description • Algorithm Development • Testing Summer 2011 Prepared by: Roger Gajraj Architecture of Computer Control Processing Unit (CPU) Control Unit Input Memory Arithmethic & Logic Unit (ALU) System Bus Output Architecture of Computer • Input unit – Takes inputs from the external world via a variety of input devices keyboard, mouse, temperature sensors, odometers, wireless devices etc. • Output Unit – Sends information (after retrieving, processing) to output devices –monitors/displays, projectors, audio devices, switches, relays, gearbox etc. • Memory • System Bus – Place where information is stored.Control Unit – Used by the other units to communicate with each other. • • ALU – Arithmetic and Logic Unit – Processes data; add, subtract, decision – after Arithmethic comparing with data, & for example InputUnit Control Memory Logic Unit (ALU) Output – Controls the interaction among other units. – Knows each unit by its name, responds to requests fairly, reacts quickly on certain critical events. Gives up control periodically in the interest of the system. – Together with the ALU is called the CPU. System Bus Architecture of Computer • THE CPU – Can fetch an instruction from memory – Execute the instruction Control Unit – Store the result in memory Arithmethic & Memory Output • AInput program – a set of instructions Logic Unit (ALU) • An instruction has the following structure: System Bus – Operation operands, destination Architecture of Computer • Input can be taken from real world and processed to produce useful output. • A computer program can be created by a programmer to instruct the computer how to process data. Problem Solving • The student must read the problem statement a number of times to ensure that he/she understands what is asked before attempting to solve the problem. • The following steps need to be followed: 1. read the problem carefully 2. understand what the problem entails 3. and only then, write down the steps to solve the problem. • An everyday example will be: – I need to go to school. Write down the steps to take from waking up in the morning until I am at school. Problem Solving • • • • • • • • 1. Wake up. 2. Get out of bed. 3. Wash. 4. Put on clothes. 5. Prepare something to eat. 6. Eat. 7. Take my bag. 8. Go to school. Algorithms • your steps must always be in a logical order! • These steps are called an algorithm that can be defined as a set of sequential instructions to solve a problem. • The most important aspect of solving a problem by using a computer is to write an algorithm to solve it. An algorithm is a set of steps that must be written in such a way that is it unambiguous and precise. The computer cannot think for itself – you as the programmer must tell the computer exactly what to do. You may never assume that the computer will do something if you have not explicitly included the specific step. Algorithms • Think of an algorithm as a recipe. • We can re-use this recipe to – take specific ingredients(input) – cook them together(processing) – produce some food(output) Algorithms • An algorithm to solve a computer based problem consists of 3 phases i.e. 1. 2. 3. What you have available for the algorithm to solve the problem How you are going to solve the problem i.e. what steps you are going to take What is the required result Algorithms • An algorithm must receive data (input) that must be processed to render meaningful results (output or information). • In other words: • Input is processed to render meaningful output. • Data is processed to render meaningful information. Expressing algorithms • Sequence of worded steps • Pseudocode • Flowcharts Example • Calculate the sum of 2 numbers and display the result on the screen: – Input: Not available in the problem statement. The user has to supply the – numbers. – Processing: Add the 2 numbers to determine the sum. – Output: The sum as calculated in the processing phase is displayed on the screen of the computer. • Note the following: – The value of the numbers must be available before the sum can be – calculated. – It is impossible to display the sum before it has been calculated.