ACCIS Online Portfolio
Beginning C++ Assignment 3
Graded by John Webb
51/56 = 91%
Good job, Janine. I left some comments for you below:
Name: Janine Bouyssounouse
Student ID: 4782181
E-mail: jcbouyss@yahoo.com
Course Number: CS110
Course Code: 03A2
Assignment Number: 3
Compiler Version: CodeWarrior 9
Version of Word: 2002
7.1 How does an array of int differ from an ordinary int?
An int is just one number, but an array of int is a list of numbers all referred to by one variable name.
-1
7.2 Write a function that takes an array as a parameter and returns a count of odd numbers in the array. Assume the array has MAX elements where MAX is a global constant int.
I have included just the function here, but the full program has been attached. I believe this is what I am supposed to do when a function is the assignment.
int countOdd(const int b[], int MAX, int numOdd)
// -----------------------------------------------------------
// Preconditions : b[] is set to the preset array of integers
// MAX is the size of the array
// odd is the number of odd integers.
// Postconditions: Counts the number of odd integers
// and returns the number of odd integers.
// -----------------------------------------------------------
{
for (int i = 0; i < MAX; i++)
{
numOdd += b[i] % 2;
}
return numOdd;
}
Here is my sample answer to this problem:
7.1 int CountOdd(int intArray[])
{
int intCount = 0;
for(int x = 0; x < MAX; x++)
{
if(intArray[x] % 2 != 0)
{
intCount++;
}
}
return intCount;
}
Notice that the test if(intArray[x] % 2 != 0) will count both negative and positive, odd integers. Your program counts only positive integers. I tried your full program using the array int a[MAX] = {1, 2, -1, 4, 3, 6, -3, 8, 12, 10}; //initializing array. Your program said that there were zero odd integers in this array, whereas the answer should have been 4. This is because intArray[x] % 2 can have three different values: -1, 0, or 1, depending on whether the integer is odd and negative, even or odd and positive.
7.3 Write a function that finds the component in a one-dimensional array (passed as a parameter) that is closest in value to the first component. Assume the array has MAX elements where MAX is a global constant int.
int findClosest(const int b[], int MAX, int closest)
// -----------------------------------------------------------
// Preconditions : b[] is set to the preset array of integers
// MAX is the size of the array
// closest is the index of the element which
// is closest in value to the first element.
// Postconditions: Finds the index of the element closest which
// is closest in value to the first element.
// -----------------------------------------------------------
{
int difference = 1000;
for (int i = 1; i < MAX; i++)
{
if (fabs(b[i] - b[0]) < difference)
{
closest = i;
difference = fabs(b[i] - b[0]);
}
}
return closest;
}
7.4 Write a function that initializes the components of a two-dimensional array in the following manner: components above the upper-left to lower-right diagonal should be set to 1. Those below the diagonal should get -1 and those on the diagonal should be initialized to 0. Assume the array has width and height equal to MAX, where MAX is a global constant. Write a short test program that calls your function and displays the resulting array. For example, a 3x3 array would be initialized to:
0 1 1
-1 0 1
-1 -1 0
// -------------------------------------------------------------
// Program CS110ques7_4
//
// Purpose: Initializes a two dimensional array in the form of
// 0 1 1
// -1 0 1
// -1 -1 0
//
// Author : Janine Bouyssounouse
// Date : September 12, 2004
// -------------------------------------------------------------
#include
using std::cout;
using std::cin;
using std::endl;
const int MAX = 4; // the maximum number of elements in the array
void showPurpose();
void showArray(const int b[][ MAX ], int, int);
void initArray( int b[][ MAX ], int, int);
void showPurpose()
// -------------------------------------------------------------
// Preconditions : none.
// Postconditions: The purpose of this program has been written
// to standard output.
// -------------------------------------------------------------
{
cout << "This program initializes a two dimensional array\n";
cout << "in the form of:\n";
cout << " 0 1 1\n";
cout << " -1 0 1\n";
cout << " -1 -1 0\n\n";
}
void showArray(const int b[][ MAX ], int x, int y)
// -------------------------------------------------------------
// Preconditions : b[] is set to the preset array of integers
// arraySize is the size of the array
// Postconditions: The elements of the array are written to
// standard output.
// -------------------------------------------------------------
{
cout << "The elements of the array are:\n";
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
cout << b[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void initArray( int b[][ MAX ], int x, int y)
// -----------------------------------------------------------
// Preconditions : b[][] is the array to be initialized
// MAX is the size of the array.
// Postconditions: Initialized the array in the form
// 0 1 1
// -1 0 1
// -1 -1 0
// -----------------------------------------------------------
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (i == j) b[i][j] = 0;
else if (j > i) b[i][j] = 1;
else if (j < i) b[i][j] = -1;
}
}
}
// function main begins program execution
int main()
{
int a[MAX][MAX]; //a two dimensional array
showPurpose();
initArray(a, MAX, MAX);
showArray(a, MAX, MAX);
return 0; // indicate that program ended successfully
} // end function main
sample output:
This program initializes a two dimensional array
in the form of:
0 1 1
-1 0 1
-1 -1 0
The elements of the array are:
0 1 1 1
-1 0 1 1
-1 -1 0 1
-1 -1 -1 0
8.1 What is a constructor? What is the difference between the public and private sections of a class declaration? Private sections can only be accessed by the class and friends of the class. Public sections can be accessed by programs using the class. It is best to have the variables be private and use manipulator functions to change the data from outside of the class/object.
8.2 Using enum and class, create a class declaration for a traffic light. Assume the light can be in one of three states: green, amber, or red. One should be able to check this state or change it. For this assignment, only the class declaration is required, not a full runnable program.
// -------------------------------------------------------------
// Program traffic.h for CS110ques8_2
//
// Purpose: This is a class definition for a traffic light.
// The traffic light has three states (green, amber, red).
//
// Author : Janine Bouyssounouse
// Date : September 19, 2004
// -------------------------------------------------------------
#ifndef TRAFFIC_H
#define TRAFFIC_H
enum lightState { GREEN, AMBER, RED }; //new variable type for light
class TrafficLight
{
public:
TrafficLight(); //constructor
lightState getState(); //checks what the state is
void printState(); //prints state of light
void setState(lightState); //changes state of light
private:
lightState lightColor;
};
#endif
8.3 A facilitator serves what purpose? A facilitator is a function in a class that is used to do something with the data in a function and is called by a program using the class. An example might be a print function, such as showTime.
9.1 Consider the following definitions:
class Thing {
public:
int Item;
Thing(int Value);
Thing(int Value1, int Value2);
int GetValue1() const;
int GetValue2() const;
private:
int DataItem1;
int DataItem2;
void SetValue1(int Value);
void SetValue2(int Value);
}
a. How many member functions does class Thing have? Explain.
There are six member functions. They are the two different Thing definitions, the two get functions, and the two set functions. Member functions don’t have to all be private.
b. Is the function SetValue1() a constructor? Explain.
SetValue1(int Value) is not a constructor. It is a mutator. SetValue1() is not defined in this class without a parameter.
c. What does the use of const after GetValue1() and GetValue2() indicate?
The const after the get functions indicates that a constant data item is being called from the member function. This makes double sure that the data cannot be changed by the program using the Thing class.
d. Can the public member function GetValue1() access the private data member DataItem1? Explain. Yes, because functions within the class are allowed to access the private data. Programs using objects from this class are not allowed to access the private data items.
-4
9.2 Can a const member function call a non-const member function? Why or why not? Yes, it can. The restriction is for consts only to be called by const functions, not the other way around.
My answer:
No, a const member function may not call a non-const member function. A member function declared with he qualifier const may not alter class data members. A non-const member function may modify member data of the class. If a const member function calls a non-const function, the non>const function may change the data, which violates the const function's role.
9.3 Create a class declaration for an alarm clock. You may want to make use of the Time structure from the text.
// -------------------------------------------------------------
// Program alarm.h for CS110ques9-3
//
// Purpose: This is a class definition for an alarm clock.
//
// Author : Janine Bouyssounouse
// Date : January 18, 2005
// -------------------------------------------------------------
#ifndef ALARM_H
#define ALARM_H
class AlarmClock
{
public:
AlarmClock( int = 0, int = 0, int = 0); //constructor
void setClock( int, int, int); //sets the time for the clock
void setAlarm( int, int, int); //sets the time for the alarm
void setOnOff( bool ); //turn alarm on or off
int getClockHour() const; //return hour
int getClockMinute() const; //return minute
int getClockSecond() const; //return second
int getAlarmHour() const; //return hour
int getAlarmMinute() const; //return minute
int getAlarmSecond() const; //return second
bool getOnOff() const; //return on off status of alarm
private:
int clockHour;
int clockMinute;
int clockSecond;
int alarmHour;
int alarmMinute;
int alarmSecond;
bool onoff;
}; //end class AlarmClock
#endif
9.4 Implement your traffic light class from Module 8, and write a short program to test it.
// -------------------------------------------------------------
// Program traffic.h for CS110ques8_2
//
// Purpose: This is a class definition for a traffic light.
// The traffic light has three states (green, amber, red).
//
// Author : Janine Bouyssounouse
// Date : September 19, 2004
// -------------------------------------------------------------
#ifndef TRAFFIC_H
#define TRAFFIC_H
enum lightState { GREEN, AMBER, RED }; //new variable type for light
class TrafficLight
{
public:
TrafficLight(); //constructor
lightState getState(); //checks what the state is
void printState(); //prints state of light
void setState(lightState); //changes state of light
private:
lightState lightColor;
};
#endif
// -------------------------------------------------------------
// Program traffic.cpp for CS110ques8_2
//
// Purpose: These are the member functions for traffic light traffic.h
// The traffic light has three states (green, amber, red).
//
// Author : Janine Bouyssounouse
// Date : September 19, 2004
// -------------------------------------------------------------
#include
using std::cout;
using std::cin;
using std::endl;
#include "traffic.h" //includes class definition
TrafficLight::TrafficLight() //constructs TrafficLight
{
lightColor = GREEN; //initializes lightColor (type lightState)
}
lightState TrafficLight::getState()
{
return lightColor;
}
void TrafficLight::printState()
{
cout << "\nThe light color is ";
switch (getState()) //prints current light color
{
case GREEN:
cout << "green." << endl;
break;
case AMBER:
cout << "amber." << endl;
break;
case RED:
cout << "red." << endl;
break;
default:
cout << "Invalid light color." << endl;
break;
}
//prints light color specific messages
if (getState() == AMBER )
cout << "\nGet ready to stop." << endl << endl;
else if (getState() == RED )
cout << "\nStop and wait for green." << endl << endl;
else
cout << "\nGreen means go." << endl << endl;
}
void TrafficLight::setState(lightState color) //set lightColor
{
lightColor = (color == GREEN || color == AMBER || color == RED )
? color : GREEN; //check for correct values
}
// -------------------------------------------------------------
// Program CS110ques9-4.cpp
//
// Purpose: This program tests the traffic light traffic.h class
// and thet raffic.cpp member functions for class traffic.
// The traffic light has three states (green, amber, red).
//
// Author : Janine Bouyssounouse
// Date : January 18, 2005
// -------------------------------------------------------------
#include
using std::cout;
using std::cin;
using std::endl;
#include "traffic.h" //includes class definition
int main()
{
TrafficLight light1; //create TrafficLight object light1
light1.printState(); //prints current lightState and msg
light1.setState( AMBER ); //sets lightState to amber
light1.printState(); //prints current lightState and msg
light1.setState( RED ); //sets lightState to red
light1.printState(); //prints current lightState and msg
light1.setState( GREEN ); //sets lightState to green
light1.printState(); //prints current lightState and msg
return 0;
}; //end main
9.5 Why are class data members normally declared in the private section? This way only the class and the member functions can access the data in the class. This helps to keep data integrity and to provide ways to check the rules for the data when it is being updated, so that invalid values are not entered. Why is this important when creating an ADT? ADTs hide their information. They keep implementation and functionality as separate concepts. The data used in an ADT needs to be private, so that other functions or classes cannot directly manipulate the data in the ADT, they must use the interface for the ADT.
9.6 Do you think the use of class friendship is consistent with the idea of ADTs? Explain. Class friendship can be seen as eroding away at the concept of information hiding, but when used correctly or responsibly, then it is acceptable. However, the ability for friend classes to modify the data directly of another class is not consistent with the concept of ADTs where the data can only be manipulated via an interface.
9.7 How does the principle of least privilege relate to the idea of ADTs? ADTs provide interfaces to access their data. Programmers use this interface to interact with the abstract data type, rather than manipulating the data directly. This is an example of the principle of least privilege because the programmer has the least amount of access to the data as possible while still being able to program using the data from the ADTs.
website created by Janine Bouyssounouse.
Last updated 02/08/07