Shared Flashcard Set

Details

C++ Programming
basic codes and other programming techniques
38
Computer Science
Undergraduate 3
09/11/2013

Additional Computer Science Flashcards

 


 

Cards

Term
Before a variable is used it must be
Definition
Declared
Term
Which of the following names in a program is equivalent to the name int?
Definition
None of the above (Int, INT, All of the above, None of the above)
Term
Which of the following IS a legal identifier?
Definition
___________.
Term
Which of the following is NOT a legal identifier?
Definition
7thheaven (no number in the front)
Term
Write a statement that declares a double variable named dosage.
Definition
double dosage;
Term
Which is the best identifier for a variable to represent the amount of money your boss pays you each month?
Definition
monthlyPay
Term
Declare a floating point variable named distance.
Definition
float distance;
Term
A location in memory used for storing data and given a name in a computer program is called a_________ because the data in the location can be changed.
Definition
variable
Term
Of the following variable names, which is the best one for keeping track of whether a patient has a fever or not?
Definition
hasFever
Term
Which comment below is the most helpful?
Definition
int volume;// size of trunk in cubic feet
Term
Of the following variable names, which is the best one for keeping track of whether an integer might be prime or not?
Definition
mightBePrime
Term
Write a statement that declares an int variable named count .
Definition
int count;
Term
Declare an integer variable named degreesCelsius .
Definition
int degreesCelsius;
Term
Write a complete program that
declares an integer variable ,
reads a value from the keyboard into that variable , and
writes to standard output the square of the variable 's value .

Besides the number, nothing else should be written to standard output.
Definition
#include using namespace std; int main() { int D; cin>>D; int square=D*D; cout<
Term
Declare two integer variables named profitStartOfQuarter and cashFlowEndOfYear
Definition
int profitStartOfQuarter;
int cashFlowEndOfYear;
Term
Write a declaration for two integer variables, age and weight.
Definition
int age;
int weight;
Term
Write a complete program that
declares an integer variable ,
reads a value from the keyboard into that variable , and
writes to standard output the variable 's value , twice the value , and the square of the value , separated by spaces.

Besides the numbers, nothing else should be written to standard output except for spaces separating the values .
Definition
#include using namespace std; int main() { int u; cin>>u; cout<< u << " " << 2*u << " " << u*u; }
Term
Write an if/else statement that adds 1 to the variable minors if the variable age is less than 18 , adds 1 to the variable adults if age is 18 through 64 , and adds 1 to the variable seniors if age is 65 or older.
Definition
if(age<18)
{
minors++;
}

if(age >=18 && age<=64)
{
adults++;
}

if(age>=65)
{
seniors++;
}
Term
Write an if/else statement that compares the double variable pH with 7.0 and makes the following assignments to the boolvariables neutral , base , and acid :
false , false , true if pH is less than 7
false , true , false if pH is greater than 7
true , false , false if pH is equal to 7
Definition
if (pH < 7)
{
neutral = false;
base = false;
acid = true;
}
if (pH > 7)
{
neutral = false;
base = true;
acid = false;
}
if (pH == 7)
{
neutral = true;
base = false;
acid = false;
}
Term
Write a statement that increments (adds 1 to) one and only one of these five variables : reverseDrivers parkedDrivers slowDrivers safeDrivers speeders .

The variable speed determines which of the five is incremented as follows:

The statement increments reverseDrivers if speed is less than 0 , increments parkedDrivers if speed is less than 1 , increments slowDrivers if speed is less than 40 , increments safeDrivers if speed is less than or equal to 65 , and otherwise increments speeders
Definition
if(speed<0)
{
reverseDrivers++;
}

if(speed>=0 && speed<1)
{
parkedDrivers++;
}

if(speed>=1 && speed<40)
{
slowDrivers++;
}

if(speed>=40 && speed<=65)
{
safeDrivers++;
}

if(speed>=65 && speed>65)
{
speeders++;
}
Term
Write a statement that compares the values of score1 and score2 and takes the following actions. When score1 exceeds score2 , the message "player1 wins" is printed to standard out. When score2 exceeds score1 , the message "player2 wins" is printed to standard out. In each case, the variables player1Wins, , player1Losses , player2Wins, and player2Losses, , are incremented when appropriate.

Finally, in the event of a tie, the message "tie" is printed and the variable tieCount is incremented .
Definition
if(score1>score2)
{
cout<<"player1 wins";

player1Wins++;
player2Losses++;
}

if(score2>score1)
{
cout<<"player2 wins";
player1Losses++;
player2Wins++;
}

if(score1==score2)
{
cout<<"tie";

tieCount++;
}
Term
Write an expression using the conditional operator (? :) that compares the values of the variables x and y and results in the larger of the two.
Definition
(x > y) ? x : y
Term
Write the necessary preprocessor directive to enable the use of file streams.
Definition
#include
Term
Define an object named infile that can be used to read data into program variables from a file.
Definition
ifstream infile;
Term
Define two objects named infile1 and infile2 that can be used to read data into program variables from two different files.
Definition
ifstream infile1;
ifstream infile2;
Term
Given an ifstreamobject named input1 , associate it with a file named winterdata.txt by opening the file for reading.
Definition
input1.
open("winterdata.txt");
Term
Given four files named winter2003.txt , spring2003.txt , summer2003.txt , fall2003.txt , define four ifstreamobjects named wntr , sprg , sumr , and fall , and use them, respectively, to open the four files for reading.
Definition
ifstream wntr, sprg, sumr, fall;
wntr.open("winter2003.txt");
sprg.open("spring2003.txt");
sumr.open("summer2003.txt");
fall.open("fall2003.txt");
Term
Given an ifstream object named input , associate it with a file named hostdata by opening the file for input.
Definition
input.
open("hostdata");
Term
Given that corpdata is an ifstreamobject that has been used for reading data and that there is no more data to be read, write the necessary code to complete your use of this object .
Definition
corpdata.close();
Term
Given the availability of an ofstreamobject named output , write the other statements necessary to write the string "3.14159" into a file called pi . (Do not define a main function.)
Definition
output.open("pi");
output << "3.14159";
output.close();
Term
Define an object named outfile that can be used to write data from program variables to a file.
Definition
ofstream outfile;
Term
Define two objects named outfile1 and outfile2 that can be used to write data from program variables to two different files.
Definition
ofstream outfile1;
ofstream outfile2;
Term
Given the availability of an ifstreamobject named input , write the other statements necessary to read an integer into a variable datum that has already been declared from a file called rawdata . Assume that reading that one integer is the only operation you will carry out with this file. (Note: write just the statements , do not define a main function.)
Definition
input.open("rawdata");
input >> datum;
input.close();
Term
Given an ofstreamobject named output , associate it with a file named yearsummary.txt by opening the file for writing.
Definition
output.open("yearsummary.txt");
Term
Given four files named asiasales2009.txt , europesales2009.txt , africasales2009.txt , latinamericasales2009.txt , define four ofstreamobjects named asia , europe , africa , and latin , and use them, respectively, to open the four files for writing.
Definition
ofstream asia, europe, africa, latin;
asia.open("asiasales.txt");
europe.open("europesales2009.txt");
africa.open("africasales2009.txt");
latin.open("latinamericasales2009.txt");
Term
Given the availability of an ifstreamobject named indata and an ofstreamobject named outdata , write the other statements necessary to read one integer from a file called currentsales and write twice its value into a file called projectedsales . Assume that this is the extent of the input and output that this program will do.
Definition
double sales=0.0;

indata.open("currentsales");
outdata.open("projectedsales");

indata >> sales;
outdata << sales * 2.0;

indata.close();
outdata.close();
Term
Use an ifstreamobject named indata to read the first three integers from a file called lottowins and write each number to standard output , on a line by itself. Assume that this is the extent of the input that this program will do.
Definition
int int1, int2, int3;

indata.open("lottowins");
indata >> int1 >> int2>> int3;

cout << int1 << "\n";
cout << int2 << "\n";
cout << int3 << "\n";

indata.close();
Term
Read first a user's given name followed by the user's age from standard input. Then use an ofstreamobject named outdata to write this information separated by a space into a file called outdata . Assume that this is the extent of the output that this program will do.
Definition
string name;
int age;

cin >> name >> age;
outdata.open("outdata");
outdata << name << " " << age;
outdata.close();
Supporting users have an ad free experience!