Presentation is loading. Please wait.

Presentation is loading. Please wait.

What's a Computer? Monitor Disk Main mouse Memory Keyboard Network

Similar presentations


Presentation on theme: "What's a Computer? Monitor Disk Main mouse Memory Keyboard Network"— Presentation transcript:

1 What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Central Processing Unit Keyboard mouse Main Memory Network CPU: (stupid) brain of the computer can do very simple tasks VERY FAST add, write in memory... Goal: Perform elaborate tasks by putting together many simple tasks HOW ? 142 B-1

2 Memory and Data All data used by a program (numbers, names…) is
stored in the memory of the computer. Think of memory as a (huge) pile of boxes (called words) 1 2 3 4 5 Each box has a number gives the location Each box always contains a value a succession of 0’s and 1’s (a number in base 2) BUT can be interpreted in many ways by a program 142 B-2

3 Memory and Data Location 4 contains 0.981 4 0.981
1 2 3 4 0.981 109 ‘c’ 3.1415 Location 4 contains 0.981 Location 2 contains the letter ‘c’ In a program do not use the location number (=address) explicitly: don’t want to say “take the content of box 4” would drive us crazy! 142 B-3

4 Instead: Use a name to refer to the content of a given box
a variable Rule of programming: A variable MUST be declared to indicate the type of the variable (is it a character (letter), an integer, a floating point number…?) 142 B-4

5 Declaring Variables Many different types: some examples A C language
declaration int number_of_children; for integer must end with semi-colon (;) no space in the variable name use underscores (_) have meaningful names (NOT int nc;) An integer can be 1, -46, (NOT 1.5, -2.3) 142 B-5

6 for a floating point, that is 96.2, -14.9, -7.02e-11...
double temperature; for a floating point, that is 96.2, -14.9, -7.02e-11... char middle_initial; for a character, that is an individual keyboard character, e.g. ‘b’, ‘A’, ‘0’, ‘+’ (NOT ‘blue’) 142 B-6

7 Examples double 4 0.981 int 109 3 2 char ‘c’ int 1 3.1415 double
1 2 3 4 0.981 109 ‘c’ 3.1415 double int char int double 142 B-7

8 Rules for naming variables
Rules for C identifiers _ use letters, numbers, underscore: employee_SSN _ can’t start with a number or underscore: _bad, 1_not_good _can’t be a reserved word of the C language: if, while, double, int _ case sensitive: temperature  Temperature _ length: practically as long as you want (but…) _ meaningful names: account_balance (NOT x22yz) 142 B-8

9 Storing values into variables
Declare a variable gives a name to the content of a memory location We get a box. But how to put something in it? Or better said: How to store the value of a variable in the memory? One way: Use an assignment statement a declaration int number_of_children; number_of_children = 3; an assignment statement (don’t forget ;) A statement tells the CPU to do something. A declaration is NOT a statement. There are many kinds of statements. 142 B-9

10 The structure of a C program
preprocessor directive #include <stdio.h> int main(void) { variables declarations program statements return 0; } beginning of program end of program DON’T WORRY about what is in the boxes Explanations will come BUT the contents of the boxes have to be in every program. 142 B-10

11 An example of program building
Problem: convert distances from miles to kilometers Algorithm: Get the distance in miles Convert the distance to kilometers (multiply by 1.609) Display the distance in kilometers How to write this in C? 142 B-11

12 contains info about printf and scanf for the compiler
#include<stdio.h> int main(void) { double miles; /*distance in miles */ double kms;/*distance in kilometers*/ /* Get the distance in miles. */ printf("Enter the distance in miles: "); scanf("%lf", &miles); /*Convert the distance to kilometers*/ kms = * miles; /*Display the distance in kilometers*/ printf("That equals %f Kms.\n",kms); return 0; } declarations print on the screen what is between the quotes read from the keyboard to read a double see later (but don’t forget &) multiplication to print a double to print a linefeed 142 B-12

13 Compilers, Linkers, etc C O .obj file L .c file M I P N I K L E E R R
executable program .obj file .c file your program library (ANSI) header (stdio.h) debugger Source code Object code 142 B- 13


Download ppt "What's a Computer? Monitor Disk Main mouse Memory Keyboard Network"

Similar presentations


Ads by Google