ge 211 programming in c dr. ahmed telba room :ac -134

Post on 17-Jan-2016

231 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

GE 211 Programming in C

Dr. Ahmed TelbaRoom :Ac -134

http://faculty.ksu.edu.sa/atelba/default.aspx

2

Comment Example// omar's Homework #1

// This program is awesome!

#include <iostream.h>

/* This program computes the

coefficient of expansion of the

universe to 27 decimal places.

*/

int main() {

cout << 1.0000000000000000000001;

}

3

Another C++ Program// C++ Addition of integers

#include <iostream.h>

int main() {

int integer1, integer2, sum;

cout << "Enter first integer\n";

cin >> integer1;

cout << "Enter second integer\n";

cin >> integer2;

sum = integer1 + integer2;

cout << "Sum is " << sum << endl;

return 0;

}

// C++ Addition of integers

#include <iostream.h>

int main() {

int integer1, integer2, sum;

cout << "Enter first integer\n";

cin >> integer1;

cout << "Enter second integer\n";

cin >> integer2;

sum = integer1 + integer2;

cout << "Sum is " << sum << endl;

return 0;

}

Statements

Declaration statements

Operation –Assignment

Printf() Format

Printf format

Scanf()

/* Lesson for 3_1• */

#include <stdio.h>void main(void){int month; float expense, income; month = 12; expense = 111.1;income=100.;

printf("Month=%2d, Expense=$%9.2f\n",month,expense);

month = 11; expense = 82.1;

printf("For the %2dth month of the year\n" "the expenses were $%5.2f \n" "and the income was $%6.2f\n\n", month,expense,income);}

/* Lesson for 3_2 */

#include <stdio.h>#define DAYS_IN_YEAR 365#define PI 3.14159

void main (void){

float income = 1234567890.12;

printf ("CONVERSION SPECIFICATIONS FOR INTEGERS \n\n");printf ("Days in year = \n"

"[[%1d]] \t(field width less than actual)\n""[[%9d]] \t(field width greater than actual)\n""[[%d]] \t(no field width specified) \n\n\n",DAYS_IN_YEAR, DAYS_IN_YEAR, DAYS_IN_YEAR);

printf ("CONVERSION SPECIFICATIONS FOR REAL NUMBERS\n\n");printf ("Cases for precision being specified correctly \n");printf ("PI = \n"

"[[%1.5f]] \t\t(field width less than actual) \n""[[%15.5f]] \t(field width greater than actual)\n""[[%.5f]] \t\t(no field width specified) \n\n",PI,PI,PI);

printf ("Cases for field width being specified correctly \n");printf ("PI = \n"

"[[%7.2f]] \t\t(precision less than actual) \n""[[%7.8f]] \t\t(precision greater than actual)\n""[[%7.f]] \t\t(no precision specified) \n\n",PI,PI,PI);

printf ("PRINTING SCIENTIFIC NOTATION \n\n");printf ("income = \n"

"[[%18.2e]] \t(field width large, precision small) \n""[[%8.5e]] \t(field width and precision medium size)\n""[[%4.1e]] \t\t(field width and precision small) \n""[[%e] \t(no specifications) \n\n",income, income, income, income);

printf ("USING A FLAG IN CONVERSION SPECIFICATIONS \n\n");printf ("Days in year= \n"

"[[%-9d]] \t\t(field width large, flag included)\n",DAYS_IN_YEAR); }

#include <stdio.h>void main(void){

int i,j,k,p,m,n;float a,b,c,d,e,f,g,h,x,y;

i=5; j=5;k=11; p=3; x=3.0; y=4.0;printf("...... Initial values ......\n");printf("i=%4d, j=%4d\nk=%4d, p=%4d\nx=%4.2f, y=%4.2f\n\n",

i,j,k,p,x,y);/*--------------- Section 1 -------------------*/a=x+y; b=x-y; c=x*y; d=x/y; e=d+3.0; f=d+3; i=i+1; j=j+1;

printf("...... Section 1 output ......\n");printf("a=%5.2f, b=%5.2f\nc=%5.2f, d=%5.2f\n" "e=%5.2f, f=%5.2f\ni=%5d, j=%5d \n\n", a,b, c,d, e,f, i,j);/*--------------- Section 2 -------------------*/m=k%p; n=p%k; i++; ++j; e--; --f;

printf("...... Section 2 output ......\n"); printf("m=%4d, n=%4d\ni=%4d, j=%4d\n" "e=%4.2f, f=%4.2f\n",m,n, i,j, e,f);

}

/* Lesson for 3_5 */#include <stdio.h>void main(void) { float income; double expense; int month, hour, minute;

printf("What month is it?\n"); scanf("%d", &month); printf("You have entered month=%5d\n",month);

printf("Please enter your income and expenses\n"); scanf("%f %lf",&income,&expense); printf("Entered income=%8.2f, expenses=%8.2lf\n", income,expense);

printf("Please enter the time, e.g.,12:45\n");scanf("%d : %d",&hour,&minute); printf("Entered Time = %2d:%2d\n",hour,minute); }

/* Lesson for 3_7 */

#include <math.h>#include <stdio.h> void main(void) { double x=3.0, y=4.0, a,b,c,d,e,f;

float g; a=sin(x); b=exp(x);

c=log(x); d=sqrt(x); e=pow(x,y); f=sin(y)+exp(y)-log10(y)*sqrt(y)/pow(3.2,4.4); g=log(x); printf("x=%4.1f y=%4.1f \n\n\r\ a=sin(x) = %11.4f\n\r\ b=exp(x) = %11.4f\n\r\ c=log(x) = %11.9f\n\n\r\ d=sqrt(x) = %11.4f\n\r\ e=pow(x,y) = %11.4f\n\r\ f=sin(y)+exp(y)log10(y)*sqrt(y)/pow(3.2,4.4)= %11.4f\n\n\r\ g=log(x) = %11.9f\n",x,y, a,b,c,d,e,f,g); }

/* For application A3_2*/ #include <stdio.h> void main(void) { double degC, degF; printf ("Table of Celsius and Fahrenheit degrees\n\n" " Degrees Degrees \n" " Celsius Fahrenheit \n"); degC = 0.; degF = degC * 9./5. +32.; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./5. +32.; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./5. +32.; printf ("%16.2f %20.2f\n", degC, degF);

degC += 20.; degF = degC * 9./5. +32.; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./5. +32.; printf ("%16.2f %20.2f\n", degC, degF); degC += 20.; degF = degC * 9./5. +32.; printf ("%16.2f %20.2f\n", degC, degF); }

/* Lesson for 3_6 */#include <stdio.h>void main(void){ double xx ; int ii, kk; FILE *inptr; inptr=fopen ("C3_6.IN","r"); fscanf(inptr,"%d",&ii); fscanf(inptr,"%d %lf",&kk,&xx);

fclose(inptr);

printf("ii=%5d\nkk=%5d\nxx=%9.3lf\n",ii, kk, xx); }

30

•scanf() Example: scanf(“%d”, &x);

•printf() Example: printf(“The value of x is %d\n”, x);

•#include <stdio.h>

Recall

31

Input/Output

Program

32

Streams

• Text input or output is dealt with as a sequence of characters

• A stream serves as a channel to convey characters between I/O and programs

33

Streams: Input -- Example

135 25.5_

1 3 5 2 5 . 5 \n

int item;

float cost;

scanf(“%d %f”, &item, &cost);

input buffer

34

Streams: Input -- Example (cont)

135 25.5_

1 3 5 2 5 . 5 \n

int item;

float cost;

scanf(“%d %f”, &item, &cost);

item cost

35

Streams: Input – Example (cont)

135 25.5_

2 5 . 5 \n

int item;

float cost;

scanf(“%d %f”, &item, &cost);

item

135

cost

36

Streams: Input – Example (cont)

135 25.5_

\n

int item;

float cost;

scanf(“%d %f”, &item, &cost);

item

135

cost

25.5

37

Streams: Output -- Example

H e l l o ! \n

printf(“Hello!\n”);

output buffer

38

H e l l o ! \n

printf(“Hello!\n”);

Streams: Output – Example (cont)

39

e l l o ! \n

H

printf(“Hello!\n”);

Streams: Output – Example (cont)

40

l l o ! \n

He

printf(“Hello!\n”);

Streams: Output – Example (cont)

41

l o ! \n

Hel

printf(“Hello!\n”);

Streams: Output – Example (cont)

42

o ! \n

Hell

printf(“Hello!\n”);

Streams: Output – Example (cont)

43

! \n

Hello

printf(“Hello!\n”);

Streams: Output – Example (cont)

44

\n

Hello!

printf(“Hello!\n”);

Streams: Output – Example (cont)

45

Hello!_

printf(“Hello!\n”);

Streams: Output – Example (cont)

46

Streams

• From the program's point of view, the characters are queued in a pipe

• The sequence of characters is organized into lines

• Each line: – can have zero or more characters– ends with the "newline" character '\n'

47

"Standard" Streams • Standard streams:

– stdin - standard input• usually from keyboard

– stdout - standard output• usually to screen

– stderr - standard error• usually to screen

• must have at the top of your program#include <stdio.h>

• can be redirected

48

stdin: Input

• Data is read in from stdin (into a variable) using the scanf() function

• When input ends, the scanf() function returns a special value: EOF

49

Example: ReadData

Input name, age, gender, idNumber

50

#include <stdio.h>

51

#include <stdio.h>

/*************************************\

Read in important info about a student

\**************************************/

52

#include <stdio.h> /*************************************\ Read in important info about a student \**************************************/ int main() {

return 0; }

53

#include <stdio.h> /*************************************\ Read in important info about a student \**************************************/ int main() {

char name[100] ;

float age ;

char gender ;

int idNumber ;

return 0; }

54

#include <stdio.h>

/*************************************\

Read in important info about a student

\**************************************/

int main()

{

char name[100] ;

float age ;

char gender ;

int idNumber ;

scanf("%s %f %c %d", name, &age, &gender, &idNumber);

return 0;

}

55

#include <stdio.h> /*************************************\

Read in important info about a student

\**************************************/

int main()

{

char name[100] ;

float age ;

char gender ;

int idNumber ;

scanf("%s %f %c %d", name, &age, &gender, &idNumber);

return 0;

}

Alkady

19.2

M

3825

Input: Akady 19.2 M 3825

56

stdout:Output

• Data (e.g., from a variable) is written out to stdout using the printf() function.

57

Example: WriteData

Set name to “Alkady” Set age to 18.2 Set gender to ‘M’ Set idNumber to 3825 Output name, age, gender, idNumber

58

#include <stdio.h>

/*****************************************\ Write out important info about a student \*****************************************/

int main() { char *name = ”Akady" ; float age = 18.2; char gender = ’M'; int idNumber = 3825 ;

printf("%s\n%f\n%c\n%d\n", name, age, gender, idNumber); return 0; }

Alkady18.2M3825_

59

Formatted Input and Output• General form:printf(format-control-string, other-arguments);

scanf(format-control-string, other-arguments);

• Examples:printf("%s\n%f\n%c\n%d\n",name,age,gender,idNumber);

scanf("%s %f %c %d", name, &age, &gender, &idNumber);

60

• Describes the format of the data for output• Contains “conversion specifiers” and “literal

characters”

Example:printf(“%s is %d years old.\n”, name, age);

printf -- Format-Control-String

61

• Describes the format of the data for output• Contains “conversion specifiers” and “literal

characters”

Example:printf(“%s is %d years old.\n”, name, age);

conversion specifiers

printf -- Format-Control-String (cont)

62

printf(“%s is %d years old.\n”, name, age);

literal characters

• Describes the format of the data for output• Contains “conversion specifiers” and “literal

characters”

Example:

printf -- Format-Control-String (cont)

63

• For printf: variables containing data for output

Example:printf(“(“%s is %d years old.\n”, name, age);

printf -- Other-Arguments

64

• Describes the format of the data given as input• Contains “conversion specifiers”

scanf -- Format-Control-String

Example:scanf("%s %f %c %d", name, &age, &gender,&id);

conversion specifiers

65

• For scanf: “pointers” to variables where the input will be stored

scanf -- Other-Arguments

scanf("%s %f %c %d", name, &age, &gender, &id);

Example:

66

• ‘&’ is for scanf only!

scanf("%s %f %c %d", name, &age, &gender, &id);

scanf -- Other-Arguments (cont)

• For scanf: “pointers” to variables in which the input will be stored

Example:

• Variables of type int, float or char need ‘&’• Do NOT use ‘&’ with strings!

67

Common Conversion Specifiers for Numerical Information

• decimal integer: %dprintf(“What is %d plus %d?\n”, x, y);scanf(“%d”, &sum);

• float: %fprintf(“%f squared is...? ”, x);

scanf(“%f”, &ans);

• double: printf(“%f squared is...? ”, x);

scanf(“%lf”, &ans);

68

Conversion Specifiers for Alphanumeric Information

• char: %cprintf(“What letter follows %c?\n”,ch);scanf(“%c”, &nextchar);

• string: %sprintf(“Name: %s\n”, name);scanf(“%s”, name);

69

• i or d: display a signed decimal integer• f: display a floating point value• e or E: display a floating point value in

exponential notation• g or G: display a floating point value in either f form or e form

• L: placed before any float conversion specifier to indicate that a long double is displayed

printf: Conversion Specifiers

70

scanf: Conversion Specifiers

• d: read an optionally signed decimal integer • i: read an optionally signed decimal, octal, or

hexadecimal integer

i and d: the argument is a “pointer” to an integerint idNumber;

scanf("%d", &idNumber);

71

scanf: Conversion Specifiers (cont)

• h or l: placed before any integer conversion specifiers to indicate that a short or long integer is to be input long int idNumber;

scanf("%ld", &idNumber);

• l or L: placed before any float conversion specifiers to indicate that a double or long double is to be input

72

Conversion Example

Input octal integer Output integer as decimal

73

Conversion Example (cont)#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

74

Conversion Example (cont)#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

_

75

Conversion Example (cont) #include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

_

76

Conversion Example (cont)#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

_

i

77

Conversion Example (cont)#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

_

i

78

Conversion Example (cont)#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

70_

i

79

Conversion Example (cont)#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

70_

i

56

80

Conversion Example (cont)#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

70_

i

56

81

Conversion Example (cont)#include <stdio.h>

int main()

{

int i ;

scanf("%o", &i);

printf("%d\n", i);

return 0;

}

7056_

i

56

82

Skipping Characters in Input Stream • Skipping blank spaces

scanf("%d %d %d", &day, &month, &year);

• Skipping dashes– Enter data as dd-mm-yyyy: 16-3-1999 – Store each number in date variables

scanf("%d-%d-%d", &day, &month, &year);

top related