1 / 37

C Programming

C Programming. Week 2 Variables, flow control and the Debugger. Example 1: Summing user variables. #include &lt;stdio.h&gt; int main() { int a, b, sum; printf(&quot;Enter an integer<br>&quot;); scanf(&quot;%d&quot;, &amp;a); printf(&quot;Enter an integer<br>&quot;); scanf(&quot;%d&quot;, &amp;b); sum = a + b;

casey-guy
Download Presentation

C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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); printf("Enter an integer\n"); 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); printf("Enter an integer\n"); 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: 5 + 7 = 12

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

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

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

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

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

  9. student_num average 17 85 address: 1760 address: 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); Error!! Will store in location 17 and 85

  10. student_num average 17 85 address: 1760 address: 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

  11. scanf • When reading numbers: • The computer expects white space between numbers • White space: space, tab, new line. • scanf ignores the white space.

  12. scanf • When reading characters: • White spaces are considered as part of the input.

  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. #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; sum = sum + num % 10; num = num / 10; sum = sum + num % 10; /* Print the result */ printf("The sum of the digits is %d\n", sum); return 0; } Example 2: Sum Digits

  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 aletter: "); scanf("%c", &c); if (c >= 'a' && c <= 'z')//a lowercase character printf("%c in uppercase is %c\n", c, toupper(c)); elseif (c >= 'A' && c <= 'Z') //an uppercase character printf("%c in lowercase is %c\n", c, tolower(c)); else { printf("%c is not aletter!\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 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 == ‘-‘) res = n1-n2; else if(op == '*‘) res = n1*n2; 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); return0; }

  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; }

More Related