n2 conditional

advertisement
IPC144 Week 6 Part 1
Agenda:
1
2
3
4
5
6
7
8
Review/Questions
Casting
Conditional Expression
Character Data
Detailed look at scanf()
getchar() function
Try it!
Homework
Review/Questions
Casting
Casting allows converting a value from one type to another. Used when the result of the calculation between
integer values needs to be stored in a double variable and precision should be preserved.
Example 1:
int num1 = 7, num2 = 2;
double result1, result2;
result1 = num1 / num2;
/* the result will get value 3.0 */
result2 = num1 / (double) num2; /* the result will get value 3.5 */
Example 2: /* Determine the exact output of the following program */
#include <stdio.h>
void main()
{
double n1=5.5;
int n2=2;
printf("%.2lf\n", n1/n2);
printf("%d\n", (int)n1/n2);
printf("%.4lf\n", (int)n1/(double)n2);
printf("%.3lf\n", (double)n2);
printf("%d\n", (int)n1);
printf("%.4lf\n", n2/(int)n1);
printf("%d\n", n2*(int)n1);
printf("%d\n", n2*n1);
printf("%.3lf\n", n2*n1);
printf("%d\n", n2*(int)(n1/n1));
}
Conditional Expression
Conditional Expression is another way of writing if/else statement. Used when each of the sub-statements is
fairly short, simple and they are almost identical.
Example 1:
if( a < b )
c = b;
else
c = a;
can be written as:
c = ( a < b ? b : a );
a < b is a condition followed by a ? mark
if the condition is true the first value is used b
if the condition is false the second value is used a
Example 2:
void change_numbers(int *a, int *b, int *c){
printf("%d\n", *a < *b ? *c + 10 : *a + (*b)++);
*a = get_number( *c, 'c');
c = a;
-- (*c);
}
Character Data
Since computers can only manipulate numeric data, character data is coded using ASCII (American Standard
Code for Information Interchange) table. For example the ASCII code for ‘A’ is 65, for ‘B’ is 66, for ‘a’ is 97
and for ‘b’ is 98. Follow this link to see the ASCII table: http://cs.senecac.on.ca/~ipc144/resources/ascii.txt
Example:
main(){
char a = ‘a’;
printf(“the ascii code for character %c is %d\n”, a, a);
/* will display – the ascii code for character a is 97 */
a = a + 1;
/* will change the value of a from ‘a’ to ‘b’
*/
printf(“the value of a is %c the ascii code is %d\n”, a, a);
/* will display – the value of a is b the ascii code is 98
}
Common Library Functions
http://cs.senecac.on.ca/~ipc144/resources/commonLibrary.html
*/
Try it!
/* Determine the exact output of the following program. */
#include <stdio.h>
main(){
int num1 = 5, num2 = 2;
double num3;
char letter = ‘A’;
num3 = num1 / num2;
letter += num2;
printf(“num1 is %d, num2 is %d, num3 is %.2lf, letter is %c\n”,
num1, num2, num3, letter);
num3 = num1 / (double)num2;
letter + = 32;
printf(“num3 is %.2lf, letter is %c\n”, num3, letter);
}
Detailed look at scanf
scanf() function gets data from the keyboard and assigns that data to the variable(s).
First parameter is a format string that will have one or more format specifiers: e.g. %d or %lf or %c. It can also
include other characters. Other characters can be any characters that must present in user’s input and will be
ignored after the scanf() finds them.
Examples:
int a, b;
double x;
char ch;
int *p;
p = &b;
scanf(“%d”, &a); /* reads one integer from the keyboard, and stores it into a */
scanf(“%lf,%d”, &x, &b); /* reads a double, skips a comma, reads an integer */
scanf(“%d”, p);
/* reads an integer, stores it into b ( using indirect method – a pointer p ) */
scanf(“ %c”, &ch);
/* skips white space ( new line character(s), space character(s), tab(s) ) , then reads a character into ch */
scanf(“%*c%c”, &ch);
/* skips one character and then reads a character into ch */
See fun18,19, and 20
How to get correct data from the user?
scanf() function has a return value. It returns the number of variables it was able to fill with the input data.
Example:
int x;
x = scanf(“%d”, &a); /* will return 1 if the user enters an integer */
/* the value of x will be 1 if the user enters an integer */
x = scanf(“%d%lf”, &a, &b);
/* will return 2 if the user enters an integer and a double */
In both cases if scanf() was unable to fill any variables with values it will return 0.
Example of validating user’s input:
/* fun17.c - using the return value of scanf() function to validate user's input */
#include <stdio.h>
int get_an_int(void){
int n;
char junk;
printf("Enter an integer:");
while( 0 == scanf("%d", &n)){
do{
scanf("%c", &junk);
} while( junk != '\n');
printf("Error! Please enter an integer:");
}
return n;
}
main(){
int x;
x = get_an_int();
printf("You've entered integer %d\n", x);
}
getchar function
getchar() function is used to read one character at a time.
Example:
ch = getchar();
is the same as
scanf(“%c”, &ch);
/* fun21.c - modified fun17.c - using getchar() function instead of scanf() */
#include <stdio.h>
int get_an_int(void){
int n;
char junk;
printf("Enter an integer:");
while( 0 == scanf("%d", &n))
{
while(getchar() != '\n')
;
/* empty body of the while loop */
printf("Error! Please enter an integer:");
}
return n;
}
main(){
int x;
x = get_an_int();
printf("You've entered integer %d\n", x);
}
Homework #10 (email your solution: arta.kogan@senecac.on.ca)
Write a program to get a character from the user and convert it to upper or lower case. For example if the user
enters a lower case ‘b’ the program should display ‘B’. Use two functions: first - to get a character from the
user, and second - to convert and display a character. Do not use built-in toupper() or tolower() functions.
Download