370 likes | 482 Views
C Programming. Week 2 Variables, flow control and the Debugger. Example 1: Summing user variables. #include <stdio.h> int main() { int a, b, sum; printf("Enter an integer<br>"); scanf("%d", &a); printf("Enter an integer<br>"); scanf("%d", &b); sum = a + b;
E N D
C Programming Week 2 Variables, flow control and the Debugger
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; }
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
#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
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
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
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
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
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
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
scanf • When reading numbers: • The computer expects white space between numbers • White space: space, tab, new line. • scanf ignores the white space.
scanf • When reading characters: • White spaces are considered as part of the input.
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.
Exercise • Write a program that gets from the user the amount of US $, the current exchange rate and converts it to NIS.
Solution Exercise_Exchange.c
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
Example (for debugging) A program that sums the digits of a number with three digits. For example: The input 369 yields the output 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
Exercise Copy the above program Run in the debugger and see how it works
Example 3 • Write a program that gets from the user a number and returns its absolute value
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; }
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?
משפט 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");
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.
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;}
אפשרות נוספת לקלט/פלט של תווים • הפונקציה getchar(): • קולטת תו מהמשתמש. • הפונקציה putchar: • מדפיסה תו למסך. • white spaces נחשבים כתווים רגילים עבור הפונקציות הללו. scanf("%c", &c); c = getchar(); printf("%c", c); putchar(c);
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
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;}
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; }
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; }