Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

Similar presentations


Presentation on theme: "1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)"— Presentation transcript:

1 1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

2 2 Why do We Need Variables? Computer programs manipulate data Data is given as input or calculated throughout the program To be later accessed, variables must be remembered Thus, variables are stored in the memory Variable name  memory address

3 3 What is a Variable? A memory chunk that is linked to a name Stores data (of a predefined type) Read/write Definition: type name Assignment: name = value Definition + assignment: type name = value

4 4 Why do we Need Different Types? Saving memory Execution speed Makes compiler “life” easier

5 5 Data representation Bit – a binary digit: 0 or 1 Bits are always grouped! Byte – a group of 8 bits Therefore, a byte can represent up to 2 8 =256 values The values can range from 0 to 255 or from -128 to 127 The fundamental data unit of a computer Word – a group of (usually) 4 (or 8) bytes 4 bytes = 32 bits Value range: 0 to 2 32 -1 (4,294,967,295 ) Or, more often: -2 31 to 2 31 -1 (-2,147,483,648 to +2,147,483,647)

6 6 Data Types char – a single byte character Integer: short int (or short) – usually 2 bytes (rarely used) int - usually 4 bytes long int (or long) – 4 or 8 bytes (rarely used) Real: float – a single precision real number – usually 4 bytes double – a double precision real number – usually 8 bytes long double - a double precision real number – usually 8 bytes (rarely used) Signed vs. unsigned

7 7 Variable Naming Rules Letters, digits, underscores Legal: i, CSE_5a, a_very_long_name_that_isnt_very_useful, fahrenheit Illegal: 5a_CSE (first character cannot be a digit) my-var Case sensitive CSE_5a is different from cse_5a Variables names should have a meaning

8 8 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

9 9 Example: Swap Two Variables /* Swap1 – swaps two variables a,b using a temporary variable */ #include int main() { int a = 2, b = 5, temp; temp = a; a = b; b = temp; printf("a = %d, b = %d\n",a,b); return 0; }

10 10 Example: Swap Two Variables How to swap two variables without using a temporary variable?

11 11 Example: Swap Two Variables int a, b; // User is requested to enter a and b printf("Enter a: "); scanf("%d", &a); printf("Enter b: "); scanf("%d", &b); a = a + b; // this way b is still "alive" b = a - b; // a + b - b = a a = a - b; // a + b - a = b printf("a = %d, b = %d\n", a, b);

12 12 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

13 13 printf and scanf printf – prints to the screen. Can also accept variables and print their values scanf – gets values from the standard input and assigns to corresponding variables

14 14 printf can Print Variable Values printf ("z=%d\n", z ); The sequence %d is a special sequence and is not printed It indicates to printf to print the value of an integer variable written after the printed string

15 15 scanf Gets Input from the User scanf("%lf", &var); Wait for the user to type in a double value, and store it in the variable ‘var’ ‘&var’ – “address” of var (we shall deal with it later on the course) To get 2 doubles from the user, use – scanf("%lf %lf", &var1, &var2);

16 16 prinft/scanf Conversion Codes A % in the printf/scanf string is replaced by a respective variable %c – a character %d – an integer, %u – an unsigned integer %f – a float %lf – a double %g – a nicer way to show a double (in printf) % - the ‘%’ character (in printf)

17 17 Example int a = 2, b = 3, c, e; double h; c = a * b; printf(“%d\n”,c); // 6 printf(“%d * %d = %d\n”,a,b,c); // 2 * 3 = 6 printf(“%d * %d = %d\n”,a,b,a*b); // 2 * 3 = 6 printf(“%d\n”,2*3); // 6 scanf(“%d %lf”, &e, &h);

18 18 What does the ‘&’ means? scanf("%d", &var); var – value of var &var – address of var Our intension is to insert the input to the var’s “cell” in memory What happens if we discard the ‘&’?

19 19 Agenda Variables (Review) Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

20 20 Arithmetic operators An operator is a mathematical action performed on constants and/or variables (operands) Some operators: Braces () Assignment = Addition + Subtraction - Multiplication * Division / Modulo %

21 21 int number, newNumber; int ones, tens, hundreds; printf("Please enter a 3-digit number "); scanf("%d", &number); // divide number to ones, tens, hundreds ones = number % 10; tens = (number%100)/10; hundreds = number/100; // calculate the new number newNumber = ones * 100 + tens * 10 + hundreds; printf("The reversed number is %d\n", newNumber); Example: Reverse a 3-digit Number

22 22 Operations on Different Types Operation on two different types (e.g., 5 + 6.5) The result is of the generalized operand (e.g., int is a float is a double) 5/2.0  2.5 When the operands are of the same type, the result is of that type as well 5/2  2

23 23 Examples 3 + 4 = 7 3.0 + 4 = 7.0 3 / 4 = 0 3.0 / 4 = 0.75

24 24 Agenda Variables (Review) Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

25 25 Casting ( המרה ) Sometimes it is desirable for a variable of one type to be considered as belonging to another in an operation We say the variable is cast to the new type Syntax: (type)operation/variable

26 26 Example – find what’s wrong #include int main() { int a = 10; int b = 20; printf("The average of %d and %d is %d\n", a, b, (a + b) * (1 / 2)); return 0; }

27 27 Alternatives #include int main() { int a = 10; int b = 20; printf(“Avg of %d and %d is %g\n", a, b, (a + b) * ((double)1 / 2)); printf(“Avg of %d and %d is %g\n", a, b, (a + b) * (1.0 / 2)); // will the following work? printf(“Avg of %d and %d is %g\n", a, b, (a + b) * (double)(1 / 2)); return 0; }

28 28 More Examples int a = 11; int b = 20; double x = 4.5; int y = x; // implicit cast printf("y = %g\n",y); // ERROR - do not relay on implicit cast in printf printf("y = %d\n",y); printf ("The average of %d and %d is %g\n", a, b, ((a+b) * (1.0/2))); printf ("The average of %d and %d is %d\n", a, b, (int)((a+b) * (1.0/2))); printf ("The average of %d and %d is %d\n", a,b, (a + b)*(1.0 / 2)); // ERROR printf ("The average of %d and %d is %d\n", a,b, (int)((a + b)*(1.0 / 2)));

29 29 Will this Work? #include int main() { int a = 10; int b = 20; printf ("The average of %d and %d is %d\n", a,b, (a + b)*(1.0 / 2)); return 0; }

30 30 Summary on Implicit Cast (Q&A) int a = 4.5; // implicit cast from double (4.5) to int (4), it is legal but might cause problem, thus usually results with a compilation warning (possible lose of data) double x = 4; // implicit cast from int to double, no compilation warning since "int is a double" relation hold printf("%g\n", a); // printf/scanf is "dumb", in this case there is no compilation warnings, but the output will be wrong printf("%d \n", 2.5); // same here: no warnings, wrong output

31 31 Agenda Variables (Review) Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

32 32 Char is also a number! A char variable is used to store a text character: Letters. Digits. Keyboard signs. Non-printable characters. But also small numbers (0 to 255 or -128 to 127).

33 33 Text as numbers Every character is assigned a numeric code There are different sets of codes: ASCII (American Standard Code for Information Interchange) – most common EBCDIC – ancient, hardly used today Maybe others We will use ASCII The ASCII table.

34 34 More about character encoding most of the time, you don't care what the particular numbers are The table above shows only 128 characters (7 bits). Some are non- printable Extended ASCII code contains 256 characters

35 35 More about character encoding ASCII code 0 (NULL character) is important – we will see it again Note contiguous sets of numbers, upper case and lower case characters

36 36 Example of char as both a character and a small number #include int main(void) { char i = 'b'; printf("i as a character is %c\n", i); printf("i as an integer is %d\n", i); printf("The character after %c is %c\n", i, i + 1); return 0; }

37 37 Another example /* Get the position of a letter in the abc */ #include int main(void) { char letter; printf("Please enter a lowercase letter\n"); scanf("%c", &letter); printf("The position of this letter in the abc is %d\n", letter-'a'+1); return 0; }

38 38 Exercise Write a program that accepts as input – A lowercase letter and outputs – The same letter in uppercase (e.g., if the input is ‘g’, the output should be ‘G’)

39 39 Solution /* Convert a letter to uppercase */ #include int main(void) { char letter; printf("Please enter a lowercase letter\n"); scanf("%c", &letter); printf("This letter in uppercase is %c\n", letter-'a'+’A’); return 0; }


Download ppt "1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)"

Similar presentations


Ads by Google