old material escape sequences operator precedence printf() scanf() if() switch case &&,||...

73

Upload: blanche-knight

Post on 29-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 2: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Old Materialescape sequencesoperator

precedenceprintf()scanf()if()switch case&&,||while, do-while, +

+, --functions

New Material1-D arraysstring

functionsfopen()fclose()fscanf()fprintf()

Page 3: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Definition: Escape sequences are specially sequenced characters used to format output

\” Ex: printf(“ \ “This is quoted text \ “ “)

\’Ex: printf(“ \n A single quote looks like \’ \n”);

\* *\ Comment Block

Page 4: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h> Using a directive to include a header file

Stdio.h = standard input output header file

Page 5: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

A computer’s long-term memory is called nonvolatile memory and is generally associated with mass storage devices, such as hard drives.

A computer’s short term memory is called volatile memory. It loses is data when power is removed from the computer, such as RAM

Page 6: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

To declare a constant (read only) value:const int x = 20const float PI = 3.14

Page 7: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

TYPE SIZE VALUES

bool 1 byte true (1) or false (0)

char 1 byte ‘a’ to‘z’ , ‘A’ to ‘Z’, ‘0’ to ‘9’, space, tab, and so on

int 4 bytes -2,147,483,648 to 2,147,483,647

short 2 bytes -32,768 to 32,767

long 4 bytes -2,147,483,648 to 2,147,483,647

float 4 bytes + - (1.2 x 10^-38 to 3.4 x 10^38)

double 8 bytes +- (2.3 x 10^-308 to -1.7 x 10^308)

Page 8: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 9: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Can you explain what the code is doing?

Page 10: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

int main(){ printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977); printf ("floats: %4.2f \n", 3.1416); printf ("%s \n", "A string"); printf(“%f \n”, 55.55);return 0; }

}

Page 11: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

printf (“%c %c \n", 'a', 65); aAprintf ("%d %ld\n", 1977, 650000L);

1977650000printf (" %10d \n", 1977);

1977printf ("%010d \n", 1977);

0000001977printf ("floats: %4.2f \n", 3.1416); 3.14

printf ("%s \n", "A string"); A stringprintf(“%f \n”, 55.55); 55.550000

Page 12: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 13: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Syntaxscanf(“conversion specifier”, variable);

Page 14: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h>

main(){

int iOperand1 = 0;int iOperand2 = 0;

printf(“\n Enter first operand: “);scanf(“%d”, &iOperand1);printf(“\n Enter second operand: “);scanf(“%d”, &iOperand2);

printf(“The result is %d \n”, 24/(iOperand1 * iOperand2)+6/3);

}

Page 15: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Do you know the answers to these?A. !( 1 || 0 ) B. !( 1 || 1 && 0 ) C. !( ( 1 || 0 ) && 0 )

Page 16: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

A. !( 1 || 0 ) ANSWER: 0

B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)

C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

Page 17: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 18: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Can you write code that will ask a user to enter a number 1 , 2 , or 3 and print out the following:

Page 19: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

int a;

int main(){ printf (“Enter one of the following: %d, %d, or %d\n”, 1, 2, 3); scanf(“%d”, &a);

if(a==1|| a==2|| a ==3){

if(a==1){printf(“\n %d is the loneliest number \n“, 1); }

if(a==2){printf(“\n%d is better than %d \n”,2,1); }

if(a==3){printf(“\n%d \’ s a crowd \n”,3); }

elseprintf(“Sorry, you entered an invalid value\n”);

return 0; }}

Page 20: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 21: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 22: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

while ( condition ) { Code to execute while the condition is true }

Quiz: Can you write a program that prints xwhile x increments from 0 to 10?

Page 23: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 24: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

do { } while ( condition );

What is the main difference between “Do while” and “while”?

Page 25: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

while ( condition ) { Code to execute while the condition is true }

do { } while ( condition );

Do{} while() executes code at least once!

Page 26: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

How do you generate a new line?

What will happen if a user tries to enter a decimal into scanf(“%d”, &number);

What does each of the following do? *, - , + , / , = , == , > , <

Page 27: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Use when the number of iterations is already known

Syntax:for ( variable initialization; condition; variable increment/decrement) { Code to execute while the condition is true }

Page 28: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h>

int main() { int x;

for ( x = 0; x < 10; x++ ){ printf( "%d\n", x );

} getchar(); }

Page 29: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Write a program using a FOR Loop to display all of the multiples of 5 from 0 to 100.

Page 30: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h>

int main() { int x; for ( x = 0; x < =20; x++ ){

printf( "%d\n", x*5 ); }

getchar(); }

Page 31: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

x++; x--; Postfix++x; --x; Prefixmain(){ int x = 0;

int y = 0;printf(“\n The value of y is %d \n”, y++);printf(“\n The value of x is %d \n”, ++x);

}

Answer:01

Page 32: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Use to manipulate flow in loopsWhat does a Break statement do

when executed within a loop?What does a Continue statement do

when executed within a loop?

Page 33: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h>

main() { int x; for ( x = 10; x >5;

x-- ){ if (x==7) break;

} printf( “\n %d \n ”, x );}

#include <stdio.h>

main() { int x; for ( x = 10; x >5;

x-- ){ if (x==7) continue;

printf( “\n %d \n ”, x );

}}

kmnich01
"In this program, the condition (x==7) becomes true after the third iteration. Next, the break statement is executed and program control is sent out from the for loop and continues with the printf statement"(Vine)
kmnich01
7 is not included in the output because once x==7 is true, the rest of the statements are skipped and the next iteration is executed.
Page 34: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Function Prototype Syntax

return-type function_name ( arg_type arg1, ..., arg_type argN )

Function Prototypes tell you the data type returned by the function, the data type of parameters, how many parameters, and the order of parameters

Function definitions implement the function prototype

Where are function prototypes located in the program?

Where do you find function definitions?

Page 35: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Where are function prototypes located in the program? Answer: before the main(){} Function!

Function Definitions are self contained outside of the main(){} function

Page 36: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h> int mult ( int, int);

int main() { int x; int y;

printf( "Please input two numbers to be multiplied: " ); scanf( "%d", &x ); scanf( "%d", &y ); printf( "The product of your two numbers is %d\n",

mult( x, y ) ); getchar();

}

int mult (int a, int b) {return a * b; }

Page 37: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h> printNumber();

main(){

int x;printNumber(x);

}

printNumber(){

printf(“\n The number is %d \n”, x)}

Page 38: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h> void printNumber( int x);

main(){

int x;printNumber(x);

}

void printNumber(int y){

printf(“\n The number is %d \n”, y);}

*Note: it’s not absolutely necessary to write VOID, but it’s a good practice.

Page 39: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h>

void printNumbers();int iNumber;

main() { int x;

for(x=0, x<10,x++){printf(“\n Enter a number:”);scanf(“%d”, &iNumber);printNumbers();

}}

void printNumbers(){

printf(“\n Your number is: %d \n”, iNumber);}

Page 40: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Variable scope defines the life time of a variable

Local Scope: defined within functions and loses scope after function is finished. Can reuse in other functions (ex. p.123)

Global Scope: defined outside of functions and can be accessed by multiple functions

Page 41: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Can you write code that asks a user to input 4 integers, adds the numbers together in one function, multiplies them in another, and finally prints out the difference between the sum and product? The user should have to do this 5 times.

Page 42: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h>

int addNumbers(int, int, int, int);int multNumbers(int, int, int, int);

main() { int a, b, c, d, counter=0; while(counter<6){

printf(“\n Please enter 4 integers separated by spaces\n”);scanf(“%d %d %d %d”, &a, &b, &c, &d);printf(“\n %d \n”, addNumbers(a,b,c,d)-multNumbers(a,b,c,d));count++;}

}

int addNumbers(int f, int g, int h, int y){

return f+g+h+y;}int multNumbers(int f, int g, int h, int y){

return f*g*h*y;}

Page 43: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Can you declare a one-dimensional array made up of 10 integers? Answer: int iArray[10]

How to declare an Array int iArray[10]; float fAverages[30]; double dResults[3]; short sSalaries [9]; char cName[19]; //18 characters and 1 null

character

Page 44: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Why do we initialize? Because memory spaces may not be cleared from previous values when arrays are created

Can initialize an array directly Example int iArray[5]={0,1,2,3,4};

Can initialize an array with a loop such as FOR()

Page 45: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h>

main(){

int x;int iArray[5];

for( x=0; x < 5 ; x++) { iArray[x] = 0;}

}

Page 46: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Can you add code to print out the values of the program below?

#include <stdio.h> main(){

int x;int iArray[5];

for( x=0; x < 5 ; x++) { iArray[x] = 0;}

}

Page 47: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h> main(){

int x;int iArray[5];

for( x=0; x < 5 ; x++) { iArray[x] = 0;}for(x=0 ; x<5; x++){ printf(“\n The value of iArray index %d is %d \n”, x,

iArray[x]);}

}

Page 48: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

How do you search through an array?

Page 49: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h> main(){

int x;int iValue;int iFound = -1;int iArray[5];

for( x=0; x < 5 ; x++) iArray[x] = (x+x);printf(“\n Enter value to search for:”);scanf(“%d”, &iValue);

for(x=0 ; x<5; x++){ if( iArray[x] ==iValue){ iFound =x;

break; )}if(iFound >-1) printf(“\n I found your search value in element %d \n”,

iFound);else printf(“\n Sorry, your search value was not found \n”);

}

Page 50: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 51: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Entity Description

Bit Binary digit, 0 or 1 Smallest value in a data file

Byte Eight bits Stores a single character

Field Grouping of bytes i.e a word, social security number

Record Grouping of fields a single row of information, student name, age, ID, GPA

File Grouping of records separate fields in a record using spaces, tabs, or commas

Page 52: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 53: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 54: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 55: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 56: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 57: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Please enter how long your name is: 21Please enter your name:NawafHello Nawaf

Please enter how long your name is: -7Failed allocation memory

Page 58: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 59: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

int *n;int * n1;n=( int * ) calloc(5, sizeof(int)); // Reserves a block of memory for 5 integers

//Decide you need to reallocate more memory later in the program

n1= (int *) realloc(n, 10 * sizeof(int));//allocate 10 integers instead of 5if (n1!=NULL){

n=n1; }else printf("Out of memory!");

realloc() returns null if unable to complete or a pointer to the newly reallocated memory.

Page 60: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Do you know the syntax for each of these, used to read and write to data files?

Pointers: think of it as the memory address of the file

fopen()

fclose()

fscanf()

fprintf()

Page 61: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

fopen() returns a FILE pointer back to the pRead variable

#include <cstdio>

Main(){

FILE *pRead;pRead = fopen(“file1.dat”, “r”);

if(pRead == NULL) printf(“\nFile cannot be opened\n”);else printf(“\nFile opened for reading\n”);fclose(pRead);

}

Page 62: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string
Page 63: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Pretty basic, yet important.

Page 64: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Reads a single field from a data file

“%s” will read a series of characters until a white

space is found

can do fscanf(pRead, “%s%s”, name, hobby);

Page 65: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h>

Main(){ FILE *pRead;

char name[10]; pRead = fopen(“names.dat”, “r”);

if( pRead == NULL ) printf( “\nFile cannot be opened\n”); else printf(“\nContents of names.dat\n”); fscanf( pRead, “%s”, name );

while( !feof(pRead) ) { printf( “%s\n”, name ); fscanf( pRead, “%s”, name ); }

fclose(pRead);}

Page 66: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Kelly 11/12/86 6 LouisvilleAllen 04/05/77 49 AtlantaChelsea 03/30/90 12Charleston

Can you write a program that prints out the contents of this information.dat file?

Page 67: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h>

main(){ FILE *pRead;

char name[10]; char birthdate[9]; float number; char hometown[20];

pRead = fopen(“information.dat”, “r”);

if( pRead == NULL ) printf( “\nFile cannot be opened\n”); else fscanf( pRead, “%s%s%f%s”, &name[0], birthdate, &number,

hometown );

while( !feof(pRead) ) { printf( “%s \t %s \t %f \t %s\n”, name, birthdate, &number,

hometown ); fscanf( pRead, “%s%s%f”, name, birthdate, &number, hometown

); }

fclose(pRead);}

Page 68: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

The fprintf() function sends information (the arguments) according to the specified format to the file indicated by stream. fprintf() works just like printf() as far as the format goes.

Page 69: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

#include <stdio.h>

main(){

FILE *pWrite;

char fName[20];char lName [20];float gpa;

pWrite = fopen(“students.dat”,”w”);

if( pWrite == NULL )printf(“\nFile not opened\n”);

else{

printf(“\nEnter first name, last name, and GPA separated”printf(“Enter data separated by spaces:”);

scanf(“%s%s%f”, fName, lName, &gpa);fprintf(pWrite, “%s \t %s \t % .2f \n”, fName, lName, gpa);

}fclose(pWrite);

}

Page 70: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Can you write a program that asks the user for their Name Phone Number Bank account balanceAnd then prints this information to a data file called accounts.dat ?

Page 71: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Good Luck in your final Exam from

REACH

Page 72: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

TEXTBOOK RESOURCE: C Programming for the Absolute Beginner 2nd Edition by Michael Vine

www.cprogramming.com

Page 73: Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string

Kelly 11/12/86 6 LouisvilleAllen 04/05/77 49 AtlantaChelsea 03/30/90 12Charleston

How many fields are there?How many records are there?How many bytes are there in the first record?How many bits are there in “Kelly”?

How many fields are there?How many records are there?How many bytes are there in the first record?How many bits are there in “Kelly”?