cecs 130 exam 1. int main() { printf (“%c %c \n", 'a', 65); printf ("%d...

43
REACH TEST REVIEW CECS 130 EXAM 1

Upload: georgina-thompson

Post on 17-Jan-2016

248 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

REACH TEST REVIEWCECS 130 EXAM 1

Page 2: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Can you predict the printout? 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"); return 0; }

}

Page 3: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Printouts

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 string

Page 4: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Review Printing with Precision

Example Printout

printf(“%.1f”,3.123456); 3.1

printf(“\n%.2f”,3.123456); 3.12

printf(“\n%.3f”,3.123456); 3.123

printf(“\n%.4f”,3.123456); 3.1234

printf(“\n%.5f”,3.123456); 3.12345

printf(“\n%.6f”,3.123456); 3.123456

Page 5: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Switch-Case Statments

Page 6: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Example Switch-Case Statement

Page 7: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

While(){} and Do{} While()

What’s the syntax of While(){} Do{} While()

What’s the difference between While and Do{} While()?

Page 8: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Answers

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

do {

} while ( condition );

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

Page 9: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Use when the number of iterations is already known

Syntax:

FOR Loops

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

Page 10: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Can you predict the print out?

#include <stdio.h>

int main() {

int x; for ( x = 0; x < 10; x+

+ ) { printf( "%d\n", x );

}getchar();

}

Page 11: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Practice FOR Loops

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

Page 12: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Answer

#include <stdio.h>

int main() {

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

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

getchar(); }

Page 13: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

BREAK and CONTINUE

Use to manipulate flow in loops What does a break statement do

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

when executed within a loop?

Page 14: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Break and Continue Example

#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 );

}}

Page 15: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Function Prototypes & Definitions

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 16: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Function Prototypes

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

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

Page 17: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Calling Functions

#include <stdio.h>

int mult ( int x, int y );

int main() { int x = 0; int y = 0;

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

}

int mult (int x, int y) { return x * y; }

Page 18: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

What’s Wrong with This Code?

#include <stdio.h> Void printReportHeader();

main(){

printReportHeader;}

void printReportHeader(){

printf(“\n Column1\tColumn2\tColumn3\tColumn4 \n”)}

Page 19: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Can You Pick Out the Local and Global Variables?#include <stdio.h>

void printNumbers();int iNumber = 0;

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 20: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Variable Scope

Variable scope defines the lifetime 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 21: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

How to Declare a One-Dimensional Array

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 22: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

How to Initialize a 1-D Array

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 23: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Example of Initializing an Array Using a For() Loop

#include <stdio.h>

main(){

int x;int iArray[5];

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

}

Page 24: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Printing Arrays

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 25: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Answer

#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 26: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Accessing the elements in an array How do you search through an array?

Page 27: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

#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 28: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Manipulating Strings

Function Description

strlen() Returns numeric string length up to, but not including null character

tolower() and toupper() Converts a single character to upper or lower case

strcpy() Copies the contents of one string into another string

strcat() Appends one string onto the end of another

strcmp() Compares two strings for equality

strstr() Searches the first string for the first occurrence of the second string

Page 29: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

String Examples

#include <stdio.h>#include <string.h>

main(){

char *str1 = “Michael”;char str2[] = “Vine”;

printf(“\nThe length of string 1 is %d \n”, strlen(str1));printf(“The length of string 2 is %d\n”, strlen(str2));

}

Page 30: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

String Examples

#include <stdio.h>#include <string.h>

void convertL(char *);

main(){

char name1[] = “Michael”;convertL(name1);

}

void convertL(char *str){

int x;for ( x = 0; x <=strlen(str) ; x++)

str[x] = tolower(str[x]);

printf(“\nThe name converted to lower case is %s\n”, str);}

Page 31: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Data File Hierarchy

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 32: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Quiz

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”?

Page 33: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Data Control

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 34: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

fopen(“file name”, “Mode”) fopen() returns a FILE pointer back to the pRead

variable

#include <stdio.h>

main(){

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

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

}

Page 35: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Common Text File Modes

Mode Meaning Already Exists Does Not Exist

“r” Open a file for reading read from start error

“w” Create a file for writing destroy contents create new

“a” Append to a file write to end create new

“r+“ Open a file for read/write read from start error

“w+“ Create a file for read/write destroy contents create new

“a+“ Open a file for read/write write to end create new

Page 36: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

fclose(file pointer)

Pretty basic.

Page 37: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

fscanf(FILE pointer, “data type”, variable in which to store the value)

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 38: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

#include <stdio.h>

main(){

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

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

elseprintf(“\nContents of names.dat\n”);

fscanf( pRead, “%s”, name );

while( !feof(pRead) ) {

printf( “%s\n”, name );fscanf( pRead, “%s”, name );

}}

Page 39: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Quiz

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 40: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

#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, birthdate, &number, hometown );

while( !feof(pRead) ) {

printf( “%s \t %s \t %f \t %s\n”, name, birthdate, number, hometown ); fscanf( pRead, “%s%s%f%s”, name, birthdate, &number, hometown ); }}

Page 41: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

fprintf(FILE pointer, “list of data types”,list of values or variables)

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 42: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

#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 43: CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);

Questions?