Introduction to Computers and Programming

advertisement
Introduction to Computers and
Programming
Class 15
Introduction to C
Professor Avi Rosenfeld
#include <stdio.h>
void main()
{
int num, tenthou, thou, hund, ten, one;
printf("Please enter a number\n");
scanf("%d",&num);
tenthou=num/10000;
thou=(num%10000)/1000;
hund=(num%1000)/100;
ten=(num%100)/10;
one=(((num%10000)%1000)%100)%10;
printf("%d%d%d%d%d",tenthou,ten, hund, thou,
one);
}
#include <stdio.h>
void main()
{
int num, tenthou, thou, hund, ten, one;
printf("Please enter a number\n");
scanf("%d",&num);
one=num%10; /*gets the last digit */
num=num / 10;
ten=num%10;
num=num / 10;
hund=num%10;
num=num / 10;
thou=num%10;
num=num / 10;
tenthou=num%10;
num=num / 10;
printf("%d%d%d%d%d",tenthou,ten, hund, thou, one);
}
Understanding Repetition
#include <stdio.h>
#define digits 7
void main()
{
int num, temp;
printf("Please enter a number\n");
scanf("%d",&num);
for (int i = 1; i <= digits; i++)
{
temp=num % 10;
num = num / 10;
printf("Digit #%d is %d\n", i, temp);
}
}
#include <stdio.h>
void main()
{
float num, high, low;
printf("Please type the first number ");
scanf("%f",&num);
high = low = num; /* new notation same as one two lines */
for (int i = 2; i <=10; i++)
{
printf("Please enter number #%d ",i);
scanf("%f",&num);
if (num>high)
high = num;
if (num<low)
low = num;
}
printf("The highest was %f\n",high);
printf("The lowest was %f\n",low);
}
#include <stdio.h>
void main()
{
int size;
printf("Please enter the size\n");
scanf("%d",&size);
for (int i = 0; i < size; i++)
{
for (int j = 0; j <= i; j++)
printf("*");
printf("\n");
}
}
#include <stdio.h>
/* The following program converts a number that the user enters from
Celcius to Fahrenheit (decimals allowed). The correct formula is: F =
9/5 C + 32. If the user enters a value less than 0, the program should
print, "Error". */
int main()
{
float Celcius;
printf("Please enter a value in Celcius ");
scanf("%f",&Celcius);
if (Celcius < 0)
printf("Error");
else
printf("%f degrees Celcius is %f degrees fahrenheit", Celcius, 9/5
* Celcius + 32);
return 0;
}
Short Answer
a. What is the value after the following statements?
int x = 5;
x -= 1+2 * 3 % 2; Answer:1 or 4
b. If x = 1 and y = 3, what does the following print?
printf("%d", 2 * x++ + ++y + 1); Answer: 7
c. If a = 0 and b = 1, does the following expression
in parentheses evaluate to true or false?
( !a && b ) Answer: T_____
Short Answer
d. What will the following print:
#include <stdio.h>
void main()
{
int x = 1, y = 2;
if (x=2);
printf("stage 1");
if (x == y)
printf("stage 2");
}
Short Answer
#include <stdio.h>
void main()
{
int x = 90;
switch(x)
{
case 100:
printf("Here ");
case 90:
printf("I ");
case 80:
printf("am ");
break;
default:
printf("not! ");
}
}
Character constant
• Characters are represented internally through int
values.
• You can reference them by using single quotes
around the character
– E.g. ‘A’, ‘a’, ‘1’ or ‘\n’
• What is actually happening is the computer stores
these as numbers
– The int value for ‘A’ is 65, and for ‘a’ it is 97, etc,
• See Appendix D, page 1198, in your text for a
chart of the int values for each character
getchar()
• Function contained in stdio.h
• A function is like a small program other
programs can use to perform actions (an
example you’ve used is printf() )
• getChar() returns an int code for a character
typed on the standard input (the keyboard)
char c;
c = getchar() ;
How to print a char
• Can use printf with a format control string of %c.
• For example,
printf( “The character is %c\n”, c );
• Can use another function in stdio.h called
putchar()
• For example,
putchar( c ) ;
putchar()
• Prints a character to the standard output
(screen)
• Is a function also referenced in stdio.h
Using getchar() example
#include <stdio.h>
int main()
{
char c ;
/* declare character variable */
/* read a character and store it in c */
c = getchar() ;
/* print it twice, two different ways */
printf("%c\n", c );
putchar( c ) ;
/* one character at a time so here’s the newline */
c = '\n' ;
putchar( c );
} /* end program */
Char Input
#include <stdio.h>
void main()
{
int count=0;
char letter;
printf("Please type some letters\n");
do
{
scanf("%s",&letter);
if (letter=='a')
count++;
}while(letter != 'b');
printf("There were %d 'A's in the sentence\n",count);
}
#include <stdio.h>
void main()
{
int count=0;
char letter;
printf("Please type some letters\n");
do
{
letter = getchar();
/*fflush(stdin);*/
if (letter=='a')
count++;
}while(letter != EOF);
printf("There were %d 'a's in the sentence\n",count);
}
Download