Top Banner
REACH CECS 130 Exam 2 Test Review
53

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()

Dec 26, 2015

Download

Documents

Coral Carson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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()

REACH CECS 130 Exam 2 Test Review

Page 2: 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()

C - Files

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 3: 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()

fopen(“file name”, “Mode”) 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”);

}

Page 4: 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()

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 5: 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(file pointer)

Pretty basic.

Page 6: 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()

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 7: 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()

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

}

Page 8: 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()

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 9: 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()

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

}

Page 10: 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()

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 11: 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()

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

elseprintf(“\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 12: 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()

Quiz

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 13: 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()

C++ Input/Output

Summary Include #include <iostream> directive

at beginning of program Use cin to take data from user Use cout to display data on screen▪ Display multiple strings and integers in the

same cout statement by separating items with <<

Page 14: 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()

C++ Input/Output Example#include <iostream>#include<string>using namespace std;

string name = “”;

int main(void){

cout<<“What is your name?”;cin>>name;cout<<endl<<“Hello”<<name.c_str();return 0;

}

Page 15: 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()

Can you predict the printout?

#include <iostream>using namespace std;int x = 25;string str2 = “This is a test”;

int main( void ){

cout<<“Test”<<1<<2<<“3”;cout<<25 %7<<endl<<str2.c_str();return 0;

}

Page 16: 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()

Answer

Test 1234This is a test

Page 17: 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()

Data

How a computer stores data in its internal memory RAM (Random-Access Memory) - temporary ROM (Read-Only Memory) – non volatile Store data in bytes

How you store data temporarily Create variables based on fundamental

types (bool, char, int, float) constants: #define CONSTNAME value sizeof()

Page 18: 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()

Variable Types

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 19: 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()

Control Statements – Boolean Operators

What do each of the following evaluate to?1. long elves = 8;

int dwarves = 8;if(elves==dwarves) //true or false?if(elves!=0) //true or false?

2. int elves = 4;int dwarves = 5;if(dwarves > (2/3)) //true or false?

3. if(0 < x < 99) //true or false?4. if(0<= (0<1))//true or false?

Page 20: 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()

Control Statements – Boolean Operators -Answers

What do each of the following evaluate to?1. long elves = 8;

int dwarves = 8;if(elves==dwarves) //trueif(elves!=0) //true

2. int elves = 4;int dwarves = 5;if(dwarves > (2/3)) //true

3. if(0 < x < 99) //true …TRUE (1) and FALSE (0) < 994. if(0<= (0<1))//true

Page 21: 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()

If statements

if(condition)statement;

else if (condition)statement;

condition ? expr1 : expr2 ex. z = ( x > y ) ? y : x ; cannot do

(x>y) ? count << “x is greater than y.” : cout << “x isn’t greater than y.”

Page 22: 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()

Switch-case statements

switch(expression){case expr1:

statement;break;

case expr2:statement;break;

case expr3:statement;break;

default:statementsbreak;

}

Page 23: 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()

Loops

while (condition){

statements;}

do{

statements;}while(condition);

Page 24: 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()

Loops

for (initialization; condition; expression)

{statements;

} Incrementing: Prefix and Postfixint x = 5;int y = 6;int z = y++ //z=6, y=7 postfix

operator int z = ++x //z=6, x=6 prefix

operator

Page 25: 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()

Branching statements

Keyword Purpose

break Exits the nearest enclosing “switch” statement or iteration statement

continue Starts the next loop of the nearest enclosing iteration statement

goto Jumps to a particular place in your code

return Ends a function and returns a value

Page 26: 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()

FOR LOOP TEST

Can you write a program that prints out the following?

0 1 2 3 4 5 6 7 8 9

Page 27: 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()

Answer

for ( int count = 0; count < 10; count ++)

{cout <<count<<“”;

}

Page 28: 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()

Other Challenge questions for Loops

1. Write a conditional statement that will assign x/y to x if y doesn’t equal 0.

2. Write a while loop that calculates the summative of positive integers from 1 to some number n.

3. Write a conditional statement that assigns x*y if x is even; otherwise , if x is odd and y doesn’t equal 0, assign x to x/y; if neither of the preceding cases is true, output to the screen that y is equal to 0.

Page 29: 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()

How to declare and implement functions

Function declaration Function definition Function call

Page 30: 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()

#include <iostream>using namespace std;

int add(int a, int b);

int main(void){

int number1, number2;cout << “Enter the first value to be summed:”’

cin >> number1;cout << “\nEnter the second:”;

cin >> number2;cout << “\n The sum is: “ << add (number1, number2) <<endl;

}

int add(int a, int b){

return a+b;}

Page 31: 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()

Functions Challenge

Write a function, called multiply that multiplies two numbers and returns the result

Page 32: 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()

OOP

Declare classes Create objects

3 MAIN PRINCIPLES OF OOP Data abstraction – hiding data members and

implementation of a class behind an interface so that the user of the class corrupt that data

Encapsulation – each class represents a specific thing or concept. Multiple classes combine to produce the whole

Polymorphism-objects can be used in more than one program

Page 33: 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()

Classes

Classes are general models from which you can create objects

Classes have data members either data types or methods

Classes should contain a constructor method and a destructor method

See handout for example of a program that utilizes a class

Page 34: 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()

Declaring Classes

class ClassName{

memberList};

memberList can be either data member declarations or method declarations

Page 35: 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()

Class Declaration ExampleClass Bow{

//data member declarationsstring color;bool drawn;int numOfArrows;

Bow(string aColor); //constructor~Bow(); //destructor

//methodsvoid draw();int fire();

};

Page 36: 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()

Creating Methods

Return_type ClassName::methodName(argumentList)

{methodImplementation

}

Page 37: 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()

Methods Creation Example//draws the bowVoid Bow::draw(){

drawn = true;cout<< “The “<<color<<“bow has been drawn.”<<endl;

}

Page 38: 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()

Advanced Data Types

Arrays Pointers

Page 39: 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()

How to create arrays

data_type array_name [number-of-elements];

Two Dimensional Arrayarray_type array_name [number_ofelements1]

[number_of_elements2];

Page 40: 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()

How to create pointers

type* pointer_name;

ex. int my_int;int* my_int_pointer =

&my_int;

Assigns the address of my_int to the pointer

Page 41: 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()

Useful string functions

Copying strings from one to another char* strcpy(char* p, const char* q); char s[6];

strcpy(s, “Hello”);

To combine strings char* strcat(char* p, const char* q); char s[12] = “Hello”

strcat(s, “World”);

Page 42: 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()

Useful string functions

To copy n characters from q to the of p.

char* strncpy(char* p, const char* q, int n); char s [7] = “Say “;

char t[] = “Hi”;strncpy (s, t, 2)

Page 43: 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()

Challenge question

Can you write a program using C++ that uses a FOR loop to initialize a 2D array that looks like the following {0,5,10,15}{0,2,4,6}

Page 44: 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()
Page 45: 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()

Answer

#include<iostream>using namespace std;int main(void){int array[4][4];

for( int i = 0; i< 5; i++){

Page 46: 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()

Basics of C++

Basic framework for a program How to Comment How to Print How to store variables How to Print stored variables How to find the size of a variable How to convert from one data type

to another How to Declare Constants

Page 47: 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()

Basics of C++

If statements Conventional Using conditional operator

Switch-case statements Loops

While Do-While For

Branching statements

Page 48: 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()

Basics of C++

How to declare and implement functions

How to create arrays How to create pointers Useful string functions Classes

Page 49: 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()

How to Comment

//this is how you comment /*this is how

you comment */ Use for Multiple lines

Page 50: 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()

Namespaces

Used to create functions, classes, and variables of the same name

Ex.

Namespace combat{

void fire()}Namespace exploration{

void fire()}

Page 51: 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()

Namespaces

To call a namespace combat::fire()

Say (to avoid having to put combat:: every time

using namespace combat;

fire()

Page 52: 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()

Inheritance

class aClass{

public:int anInt;

}

class aDerivedClass : public aClass{

protected:float aFloat;

};

Page 53: 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()

Templates

Used in place of a specific data type. For example, use a template to add data types together, whichever data type the user wishes (i.e integers, floats)