Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 2 Variables, flow control and the Debugger

Similar presentations


Presentation on theme: "Week 2 Variables, flow control and the Debugger"— Presentation transcript:

1 Week 2 Variables, flow control and the Debugger
C Programming Week 2 Variables, flow control and the Debugger

2 Example 1: Summing user variables
#include <stdio.h> int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; }

3 Example 1: Summing user variables
#include <stdio.h> int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; } We read the first variable from the user (5) We read the second value from the user (7) Output: Answer: = 12

4 Example 1: Summing user variables
#include <stdio.h> int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; } Memory ??? b sum a 4

5 Example 1: Summing user variables
#include <stdio.h> int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; } Memory ??? b sum a

6 Example 1: Summing user variables
#include <stdio.h> int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; } Memory 5 b sum a ???

7 Example 1: Summing user variables
#include <stdio.h> int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; } Memory 5 b sum a 7 ???

8 Example 1: Summing user variables
#include <stdio.h> int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; } Memory 5 b sum a 7 12

9 Will store in location 17 and 85
Why we need & in scanf? The & sign refers to the memory address of the variable – where it should be stored. scanf( "%d %lf", student_num, average); Error!! Will store in location 17 and 85 17 student_num address: 1760 85 average address: 7510

10 Will store in location 1760 and 7510
Why we need & in scanf? The & sign refers to the memory address of the variable – where it should be stored. scanf( "%d %lf", &student_num, &average); OK!! Will store in location 1760 and 7510 17 student_num address: 1760 85 average address: 7510

11 scanf When reading numbers:
The computer expects white space between numbers White space: space, tab, new line. scanf ignores the white space. Numbers separated with white space scanf("%d%d",&i,&j); 12 34 scanf("%d %d",&i,&j); 12,34 Numbers separated with “,” scanf("%d, %d",&i,&j); Numbers separated with “+” scanf("%d + %d",&i,&j);

12 scanf When reading characters:
White spaces are considered as part of the input. ab Consecutive characters scanf("%c%c",&i,&j); a b Characters separated with space scanf("%c %c",&i,&j);

13 scanf warning In visual 2010 when you use scanf you might get the following warning: “warning C4996: ‘scanf’ was declared deprecated …” You can disregard the warning.

14 Exercise Write a program that gets from the user the amount of US $, the current exchange rate and converts it to NIS.

15 Solution Exercise_Exchange.c

16 The Debugger Some programs may compile correctly, yet not produce the desirable results These programs are valid and correct C programs, yet not the programs we meant to write! The debugger can be used to follow the program step by step and may help detecting bugs in an already compiled program

17 Example (for debugging)
A program that sums the digits of a number with three digits. For example: The input 369 yields the output 18

18 Example 2: Sum Digits #include <stdio.h> int main() {
int sum = 0, num; /* Read a 3-digit number from the user */ printf("Enter 3-digits number\n"); scanf("%d", &num); sum = sum + num % 10; num = num / 10; /* Print the result */ printf("The sum of the digits is %d\n", sum); return 0; } 18

19 Exercise Copy the above program
Run in the debugger and see how it works

20 Insert Breakpoint

21 Start Debugging (F5)

22 Debugger

23 Step Over (F10)

24 Other Commands

25 Update Variables

26 Example 3 Write a program that gets from the user a number and returns its absolute value

27 Example 3 - Absolute Value
/* The absolute value of a number */ int main() { double num = 0.0; printf("Please enter a real number: "); scanf("%lf", &num); if (num < 0) num = -num; printf("The absolute value is %lf\n", num); return 0; }

28 Example 3 - Absolute Value
/* The absolute value of a number */ int main() { double num = 0.0; printf("Please enter a real number: "); scanf("%lf", &num); if (num < 0) num = -num; printf("The absolute value is %g\n", num); return 0; } What happens if we change %lf to %g?

29 From %lf to %g

30 משפט if-else מה קורה אם בהתקיים התנאי נרצה לבצע משפט מסוים ואם אינו מתקיים משפט אחר? נשתמש ב-if-else : אם התנאי מתקיים יתבצע משפט 1 אחרת יתבצע משפט 2 if (grade >= 0 && grade <= 100) printf("The grade is in range\n"); else printf("The grade is not in range\n");

31 Example 4 Read a single letter from the user
Make sure the input is valid In case the letter is lowercase convert to uppercase. In case the letter is uppercase convert to lowercase.

32 Solution #include <ctype.h> int main(){ char c=0;
printf("Please enter a letter: "); scanf("%c", &c); if (c >= 'a' && c <= 'z')//a lowercase character printf("%c in uppercase is %c\n", c, toupper(c)); else if (c >= 'A' && c <= 'Z') //an uppercase character printf("%c in lowercase is %c\n", c, tolower(c)); else { printf("%c is not a letter!\n", c); return 1; } return 0;}

33 אפשרות נוספת לקלט/פלט של תווים
הפונקציה getchar(): קולטת תו מהמשתמש. הפונקציה putchar: מדפיסה תו למסך. white spaces נחשבים כתווים רגילים עבור הפונקציות הללו. scanf("%c", &c); c = getchar(); printf("%c", c); putchar(c);

34 Example 5 – a calculator Ignore space/enter
int main( ){ double n1=0.0, n2=0.0, res=0.0; char op=0; printf("Please enter two numbers: "); scanf("%lf%lf", &n1, &n2); printf("Please enter an arithmetical operator (+, -, * or /): "); scanf(" %c", &op); Ignore space/enter

35 Example 5 cont. if(op == '+‘) res = n1 + n2; else if (op == ‘-‘)
else if(op == '/‘) /* We're not checking for division by zero for clarity*/ res = n1 / n2; else printf("%c is an invalid arithmetical operator!\n", op); printf(“%g %c %g = %g\n", n1, op, n2, res); return 0;}

36 Example 5 – same example using switch
switch(op) { case '+': res = n1 + n2; break; case '-': res = n1 - n2; break; case '*': res = n1 * n2; break; case '/': res = n1 / n2; break; default: printf("%c is an invalid arithmetical operator!\n“, op); } printf(“%g %c %g = %g\n", n1, op, n2, res); return 0;

37 Solution to class exercise
/* This program calculates, for a given amount of US $, their value in NIS, according to a given echange rate. */ int main()} double usd = 0.0, xrate = 0.0, nis = 0.0; printf("Please enter amount of US dollars: "); scanf("%lf", &usd); printf("Please enter the exchange rate: "); scanf("%lf", &xrate); /* Convert */ nis = usd * xrate; printf("%g USD = %g NIS (exchange rate = %g)\n", usd, nis, xrate); return 0; }


Download ppt "Week 2 Variables, flow control and the Debugger"

Similar presentations


Ads by Google