Schedule‎ > ‎

Quiz 3 Prep

Quiz 3 Prep   (Selected answers at end:  try the questions first  before looking at the answers for best results.  If the answers aren’t there,  I expect that you will look them up)

Note:  Look at the link KEY SAMPLE CODE at the top of the Class Schedule page and make sure you can write code like that.  Notes on your index card can help.

1.        What is the difference between a while and a do-while loop?

 

2.       What is a sentinel value?

 

 

3.       What is sensor calibration and what is it used for?  What follows is the code similar to what you wrote in Ex. 1: Sensor Calibration to jog your memory: (also see answer).

 

void loop() {
  bool keypress = false;
  int reading;
  double doubleReading = 0.0;
  int max = 0, min = 1023;
  cout << "Calibrate the sensor, then enter any key..." << endl;
  while (!keypress) {
    if (Serial.available())  {
      Serial.read();
      keypress = true;
    }
    reading = analogRead(SENSOR);
    if (reading > max) {
      max = reading;
    }
    else  if (reading < min)  {
      min = reading;
    }
  }   //end of while loop
  cout << "min is " << min << " max is " << max << endl;
  keypress = false;
  do {
    if (Serial.available()) {
      Serial.read();
      keypress = true;
    }
    reading = analogRead(SENSOR);
    double scaledPercentage = 0.0;
    if (keypress == true)  {
      cout << "new reading is " << reading << endl;


      scaledPercentage = 100.0 * (reading - min) / (max - min);
      cout << "scaled doubleReading as a percentage is  " << scaledPercentage << "%" << endl;
    }
  } while (! keypress);
  delay(1000);
}

 

4.        If I want to actually put the hat on a human, and not just on a human copy:  do I define my putHatOn function like which of the following?  

a)  int putHatOn(object human, int color);

b)  int putHatOn(object& human, int color);

note:  object is a made-up type for example purposes only

 

5.        What is function overloading?  Give an example of an overloaded function.

 

 

6.       What is the purpose of this statement: randomSeed(analogRead(0));    As part of your explanation, what does the analogRead(0) do?

 

 

7.       What does the following code print?  (09A)

int myArray[] = {12,30,7,8,15,99,23};

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

     cout <<  myArray[i];

}

 

 

8.        What is the difference between a C-string array and a char array and a String?

 

9.        Write the code to declare a vector of integers named myIntegers which is initially empty.

 

10.    Write the code to add 3 integers to your vector:   3, 5, 7

 

 

11.    Write the code to print out these 3 integers in your vector.

 

12.    Declare a function prototype of printArray which will print out all elements of an integer array.

 

You only need to write the first line of the function definition.

 

13.    Write a statement that will call your printArray Function.  Assume that you have already declared an array myArray which contains 5 integers.

 

14.   Declare a function prototype of printVector which will print out all elements of a vector of floats.

You only need to write the first line of the function definition.

 

15.    Write a statement that will call your printVector Function.   Assume that you have already declared a vector myVectorFloats which contains float numbers.

 

 

 

Selected Answers:

2.  a sentinel value is a value which is used as a condition of termination of a loop

 

3.  Sensor  Calibration

If a meaningful measurement is to be made, it is necessary to measure the output of a sensor in response to an accurately known input. This process is known as calibration.  You’ll need to know what is the minimum reading of your sensor and what is the maximum reading of your sensor.  You find this by blocking the sensor for a minimum reading and putting a maximum input (like a flashlight for a light sensor) for a maximum reading. 

 

4.   b)  be sure you clearly understand the difference between pass by value and pass by reference (07B)

5.  function overloading is described in 07B

 

6.  randomSeed() and random() are described in 08A

 

7.  Answer:  8 15  arrays are described in 09A. 

 

8.  C-string arrays, char arrays are described in 09A.  String is described in 04A

 

9-11.   Vectors are described in 09A and 09B

  vector<int> myIntegers;

  myIntegers.push_back(3);

  myIntegers.push_back(5);

  myIntegers.push_back(7);

  for (int i = 0; i < myIntegers.size(); i++)  {

    cout << myIntegers[i] << endl;

  }

12.  void printArray(int myArr[], int size);

 

13.  printArray(myArray, 5);

14. void printVector(vector<float> &myVect);

15. printVector(myVectorFloats);

 

Comments