Cpp2 Test3

  1. Give an example of a highly cohesive function.
    A function that does one task
  2. Can a function be coded inside main()?
    no
  3. what is a void function?
    1. a function that does not receive or return a value.
  4. what is a void returning function?
    1. a function that does not return a value.
  5. If the prototype is global, where does it appear in your code?
    Before main
  6. Given the following prototype: double getValidHours(); Write the statement that will call the function. Declare any variables you use.
    double hours=getValidHours();
  7. Given the function call: int age=getValidAge(); Write the prototype for the function.
    Int getValidAge();
  8. Given an example of a function that has a coupling problem?
    A function that receives parameters it doesn’t use
  9. 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);
  10. 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
  11. 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.
  12. 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)
  13. 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');
  14. Write the prototype for
    • void printNumbSquared (int number)
    • {
    • statments
    • }
    • void printNumbSquared(int)
  15. What line(s) are required to close a file?
    None. Files are automatically closed when program ends.
  16. Write the prototype for
    int remove(int length, string car[], double price[])
    int remove(int, string[], double[])
  17. Write the prototype for
    int removeElement(string names[],int length,string name)
    int removeElement(string[], int, string)
  18. What must you remember before adding an element to an array?
    Check if the array is already full.
  19. Write a call for
    double calcAvg(const double fare[],int length)
    avg=calcAvg(fare, length);
  20. 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
  21. What is the difference between a function header and a prototype?
    prototype = header - variable names
  22. What is the difference between a function header and a function call?
    call = header - datatype([])
  23. int grades[4][3]; is an array with
    4 rows and 3 columns
  24. 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}}
  25. int grades[4][3]={60,50,50,80};
    • 60 50 50
    • 80  0   0
    •  0   0   0
    •  0   0   0
  26. int grades[4][3] = { { 60 }, { 50 }, { 50 }, { 80 } };
    • 60 0 0
    • 50 0 0
    • 50 0 0
    • 80 0 0
  27. 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
  28. 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
  29. Given: void printGrades(const int scores[][3])
    What is the prototype?
    void printGrades(const int [][3])
  30. 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);
  31. Given:
    int loadArrays(string names[],int grades[][3])
    Write the prototype?
    int loadArrays(string [], int [][3]);
  32. 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)
  33. 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.
  34. 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 &)
  35. 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;
    •    }
    • }
  36. 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 &
  37. prototype
    void getAgeGender(int &, char &);

    call
    getAgeGender(10,’f’);
    ERROR: can only pass variables not constants
  38. 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;
    • }
  39. 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)
  40. How do you test for end of file in fin?
    if (fin.eof())
  41. What is the standard preprocessor directive?
    #include <iostream>
  42. What preprocessor directive is need for files?
    #include <fstream>
  43. 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);
  44. 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];...
  45. 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
Author
slc53
ID
320703
Card Set
Cpp2 Test3
Description
Cpp2 Test3
Updated