-
Give an example of a highly cohesive function.
A function that does one task
-
Can a function be coded inside main()?
no
-
what is a void function?
1. a function that does not receive or return a value.
-
what is a void returning function?
1. a function that does not return a value.
-
If the prototype is global, where does it appear in your code?
Before main
-
Given the following prototype: double getValidHours(); Write the statement that will call the function. Declare any variables you use.
double hours=getValidHours();
-
Given the function call: int age=getValidAge(); Write the prototype for the function.
Int getValidAge();
-
Given an example of a function that has a coupling problem?
A function that receives parameters it doesn’t use
-
Write a function called printNumbSquared that will print a number passed to it times itself. 3. Write the prototype for printNumbSquared function.
- void printNumbSquared (int number)
- {
- cout << number*number;
- }
- 3. void printNumbSquared (int);
-
Other Boolean functions that are useful
- bool isalpha(char); checks if char is A-Z or a-z
- bool isalnum(char); checks if A-Z,a-z or 0-9
- bool isdigit(char); checks if 0-9
- bool islower(char); checks if lower case
- bool isupper(char); checks if upper case
-
What must be done in VS to open a file?
In the Solution Explorer, right click on Resources Files. Add a new item as text file (.txt). Call it dates.dat.
-
What code is needed to open and output file?
- 1.Need to include fstream.h
- 2. ofstream fout("pay.rpt");
- if (!fout.is_open())
- {
- cout << "error opening pay.rpt";
- system("pause");
- exit(-1);
- }
- destructive open(if file already exists, its erased)
-
What code is needed to open an input files?
- ifstream fin("dates.dat");
- #include
- if (!fin.is_open())
- {
- cout << "file dates.dat could not open" << endl;
- system("pause");
- exit(-1);
- }
- getline(fin, name);
- if (fin.eof())
- fin >> gender >> age;
- fin.ignore(80, 'n');
-
Write the prototype for
- void printNumbSquared (int number)
- {
- statments
- }
- void printNumbSquared(int)
-
What line(s) are required to close a file?
None. Files are automatically closed when program ends.
-
Write the prototype for
int remove(int length, string car[], double price[])
int remove(int, string[], double[])
-
Write the prototype for
int removeElement(string names[],int length,string name)
int removeElement(string[], int, string)
-
What must you remember before adding an element to an array?
Check if the array is already full.
-
Write a call for
double calcAvg(const double fare[],int length)
avg=calcAvg(fare, length);
-
How do you make sure a function does not change the values in an array?
add const before each parameter in the function header and prototype
-
What is the difference between a function header and a prototype?
prototype = header - variable names
-
What is the difference between a function header and a function call?
call = header - datatype([])
-
int grades[4][3]; is an array with
4 rows and 3 columns
-
int grades[4][3]={{60},{50},{50},{80}};
- //assigns first test only, other tests are zero
- {{60,0,0},
- {50,0,0},
- {50,0,0},
- {80,0,0}}
-
int grades[4][3]={60,50,50,80};
- 60 50 50
- 80 0 0
- 0 0 0
- 0 0 0
-
int grades[4][3] = { { 60 }, { 50 }, { 50 }, { 80 } };
-
int grades[][3]={60,70,80,50,30,80,50,30,30,80,60,70};
- 60 70 80
- 50 30 80
- 50 30 30
- 80 60 70
-
const int scores[][3];
Passing a row to a function (i.e. not the entire 2 dim array)
Prototype:
void printrow1(const int []);
//function uses as a 1 dim array
What is call to print 2nd students grade?
printrow1(grades[1]);//passes 2nd students row
-
Given: void printGrades(const int scores[][3])
What is the prototype?
void printGrades(const int [][3])
-
Given: void printGrades(const int scores[][3])
Using the following declaration:
int grades[20[3];
Write the statement that will call the function.
printGrades(grades);
-
Given:
int loadArrays(string names[],int grades[][3])
Write the prototype?
int loadArrays(string [], int [][3]);
-
Given:
int loadArrays(string names[],int grades[][3])
return length;
declarations:
string empID[20];
int score[20][3];
int noEmps;
Write the statement that will call the function.
noEmps=loadArrays(empID,score)
-
What is & in a function header?
& (address of) operator to denote call by ref. Called function has access to memory location of variable and can change its contents.
-
void getAgeGender(int &age,char &gender)
{
cout < “enter age and gender “;
cin >> age >> gender;
}
Write call and prototype.
- call
- getAgeGender(empAge,empGender)
- prototype
- void getAgeGender(int &, char &)
-
Convenient to use call by ref when you receive and return same variable
- void validateAge(int& age)
- {
- while (age < 0 || age > 150)
- {
- cout << “age invalid”;
- cin >> age;
- }
- }
-
want to return more than one value
prototype: void getAgeGender(int &, char &);
function?
call?
- void getAgeGender(int &age,char &gender)
- {
- cout < “enter age and gender “;
- cin >> age >> gender;
- }
- call
- getAgeGender(age,gender); //don’t put &
-
prototype
void getAgeGender(int &, char &);
call
getAgeGender(10,’f’);
ERROR: can only pass variables not constants
-
Call by reference is often used with files to avoid copying data files
- void writeFile(ofstream &fout, string name,int mark)
- {
- fout << name << mark << endl;
- }
- void readFile(ifstream &fin, string &name, int & mark)
- {
- getline(fin,name);
- fin >> mark;
- }
-
void loadArrays(ifstream &fin, string name[], int lapTimes[][15], int &noDrivers)
Write prototype
write call
- void loadArrays(ifstream &fin, string name[], int lapTimes[][15], int &noDrivers)
- Write prototypewrite.
- void loadArrays(ifstream &, string [], int [][15], int &);
- Write call.
- loadArrays(fin,names,lapTimes,noDrivers)
-
How do you test for end of file in fin?
if (fin.eof())
-
What is the standard preprocessor directive?
#include <iostream>
-
What preprocessor directive is need for files?
#include <fstream>
-
Write a function returns the age and gender.
Write a call.
- in main()
- int age; char gender;
- void getAgeGender(int & age, char & gender)
- {
- cout<<"Enter age and gender.";
- cin >>age>>gender;
- }
getAgeGender(age, gender);
-
Write a function that will load array with name, lapTimes from a file fin and update the number of drivers using by reference.
- void loadArrays(string names[],
- int lapTimes[][15], int & noDriv)
- {
- for(noDriv=0;noDriv<SIZE,noDriv++)
- { getline(fin,names[row]);
- if(fin.eof()) break;
- ...fin>>lapTimes[row][col];...
-
function header, call, prototype look like
- int addAtStart(string[], char[], int[], int);
- length = addAtStart(name, gender, age, length);
- int addAtStart(string name[], char gender[], int age[], int length)
- write prototypes w/wo Arrays byVal/byRef
- write headers w/wo Arrays byVal/byRef
- write calls w/wo Arrays byVal/byRef
- open file
- read from file
- write to file
|
|