Thursday, February 27, 2014

Section 8.2 Text Files: StreamReaders, StreamWriters, and Structured Exception Handling




40011:  Assume that s is a string variable that is supposed to contain a value to be converted to integer.  Write a fragment of code that converts the value to integer variable and displays that value multiplied by 2.  If the value cannot be converted, display the message 'Invalid number' instead.

Try
MessageBox.Show(CInt(s) * 2)
Catch
MessageBox.Show("Invalid number")
End Try

Section 7.1 Creating and Using Arrays - Traversing Arrays




40210:  Given an array temps of Doubles, containing temperature data, compute the average temperature.  Store the average in a variable called avgTemp.  Besides temps and avgTemp, you may use only two other variables -- an Integer variable k and a Double variable named total, which have been declared.

k = 0
total = 0
Do
total += temps(k)
k += 1
Loop Until (k = temps.length)
avgTemp = total / k

40212:  Reversing the elements of an array involves swapping the corresponding elements of the array:  the first with the last, the second with the next to last, and so on, all the way to the middle of the array.  Given an array a and two other Integer variables, k and temp, write a loop that reverses the elements of the array.  Do not use any other variables besides a, k, and temp.

For k = 0 to ((a.length-1)/2)
temp = a(k)
a(k) = a((a.length-1)-k)
a((a.length-1)-k) = temp
Next

40213:  Given an Integer variable k, an Integer array currentMembers that has been declared and initialized, an Integer variable memberID that has been initialized, and a Boolean variable isAMember, write code that assigns True to isAMember if the value of memberID can be found in currentMembers, and that assigns false to isAMember otherwise.  Use only k, currentMembers, memberID and isAMember.

isAMember = false
k = 0
Do
If memberID = currentMembers(k) Then
isAMember = true
End If
k += 1
Loop Until (k = currentMembers.length)

40214:  You are given an Integer variable k, an Integer array zipcodeList that has been declared and initialized, and a Boolean Variable duplicates.  Write some code that assigns True to duplicates if there are two adjacent elements in the array that have the same value, and that assigns False to duplicates otherwise.  Use only k, zipcodeList, and duplicates.

duplicates = false
k = 0
Do
If zipcodeList(k) = zipcodeList(k+1) Then
duplicates = true
End If
k += 1
Loop Until (k = zipcodeList.length - 1)




40215:  You are given two Integer variables j and k, an Integer array zipcodeList that has been declared and initialized, and a Boolean variable duplicates.  Write some code that assigns True to duplicates if any two elements in the array have the same value, and that assigns False to duplicates otherwise.  Use only j, k, zipcodeList, and duplicates.

duplicates = false
k = 0
j = 0
For k = 0 to zipcodeList.length-1
For j = 0 to zipcodeList.length-1
If (zipcodeList(k) = zipcodeList(j)) And (j<>k) Then
duplicates = true
End If
Next
Next

40216:  Given an Integer variable k, an Integer array incompletes that has been declared and initialized, an int variable studenID that has been initialized, and an int variable numberOfIncompletes, write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes.  You may use only k, incompletes, studentID, and numberOfIncompletes.

numberOfIncompletes = 0
For k = 0 to (incompletes.length-1)
If studentID = incompletes(k) Then
numberOfIncompletes += 1
End If
Next

40219:  An array of integers named parking Tickets has been declared and initialized to the number of parking tickets given out by the city police each day since the beginning of the current year.  (Thus, the first element of the array contains the number of tickets given on January 1; the last element contains the number of tickets given today.)  A variable named mostTickets has been declared, along with a variable k.  Without using any additional variables, write some code that results in mostTickets containing the largest value found in parkingTickets.

mostTickets = 0
For k = 0 to (parkingTickets.length-1)
If mostTickets < parkingTickets(k) Then
mostTickets = parkingTickets(k)
End If
Next

Tuesday, February 25, 2014

Section 7.1 Creating and Using Arrays - Accessing Elements




40195:  Given an array a, write an expression that refers to the first element of the array.

a(0)

40196:  Given an array a, declared to contain 34 elements, write an expression that refers to the last element of the array.

a(33)

40197:  Assume that the array arr has been declared.  Write a statement that assigns the next to last element of the array to the variable x, which has already been declared.

x = arr(arr.length - 2)

40198:  Given that the array monthSales of integers has already been declared and that its elements contain sales data for the 12 months of the year in order (i.e., January, February, etc.), write a statement that writes to standard output the element corresponding to October.  Do not write anything else out to standard output.

Console.Write(monthSales(9))

40211:  We informally define the term corresponding element as follows:  The first element in an array and the last element of the array are corresponding elements.  Similarly, the second element and the element just before the last element are corresponding elements.  The third element and the element just before the element just before the last element are corresponding elements -- and so on.  Given an array a, write an expression for the corresponding element of a(i).

a(a.length - 1 - i)

40199:  Given that an array named a with elements of type Integer has been declared, assign 3 to its first element.

a(0) = 3

40200:  Assume that an array named salarySteps whose elements are of type int and that has exactly five elements has already been declared.  Write a single statement to assign the value 30000 to the first element of this array.

salarySteps(0) = 30000

40500:  Given that an array named a has been declared, assign -1 to the last element in a.

a(a.length - 1) = -1




40201:  Assume that an array of integers named salarySteps that contains exactly five elements has been declared.  Write a statement that assigns the value 160000 to the last element of the array salarySteps.

salarySteps(salarySteps.length - 1) = 160000

40202:  Assume that an array named a containing exactly 5 integers has been declared and initialized.  Write a single statement that adds 10 to the value stored in the first element of the array.

a(0) += 10

40203:  Given that an array of Integers named a with 30 elements has been declared, assign 5 to its last element.

a(29) = 5

40204:  Assume that an array of Integers named a has been declared with 12 elements.  The integer variable k holds a value between 0 and 6.  Assign 15 to the array element whose index is k.

a(k) = 15

40205:  Given that an array named a whose elements are of type Integer has been declared, assign the value -1 to the last element in a.

a(a.length - 1) = -1

40206:  An array of Integers named a has been delcared with 12 elements.  The integer variable k holds a value between 0 and 6.  Assign 9 to the element just after a[k].

a(k+1) = 9

40207:  An array of Integers named a has been declared with 12 elements.  The integer variable k holds a value between 2 and 8.  Assign 22 to the element just before a[k].

a(k-1) = 22

40208:  Assume that an array of Integers named a that contains exactly five elements has been declared and initialized.  In addition, an Integer variable j has also been declared and initialized to a value somewhere between 0 and 3.  Write a single statement that assigns a new value to the element of the array indexed by j.  This new value should be equal to twice the value stored in the next element of the array (i.e. the element after the element indexed by j).

a(j) = a(j+1) * 2




40209:  Assume that an array of integers named a has been declared and initialized.  Write a single statement that assigns a new value to the first element of the array.  The new value should be equal to twice the value stored in the last element of the array.

a(0) = a(a.length - 1) * 2

Friday, February 21, 2014

Section 6 Examples




40187:  Assume the int variables i, lo, hi and result have been declared and that lo and hi have been initialized.  Assume further tha result has been initialzied to the value 0.  Write a for loop that adds the integers between lo and hi (inclusive), and stores the result in result.  Your code should not change the values of lo and hi.  Also, do not declare any additional variables -- use only i, lo, hi, and result.

For i = lo to hi
result += i
Next

40223:  Give that two Integer variables, total and amount have been declared, write a loop that reads non-negative values into amount and adds them into total.  The loop termiantes when a value less tha 0 is read into amount.  Don't forget to initialize total to 0.  To read a value into amount use a method, getNum() that we provide for you a class named tC:  amount = TC.getNum();

total = 0
amount = 0
Do
total += amount
amount = TC.getNum()
Loop Until (amount < 0)

Section 6.2 For...Next Loops




40174:  Given an int variable k that has already been declaredm use a for loop to print a single line consisting of 97 asterisks.  Use no variables other than k.

For k = 1 to 97
Console.Write("*")
Next

40502:  Assume the integer variables counter, low, high, and result have been declared and that low and high have been initialized.  Write a For loop that adds the integers between low and high (inclusive), and stores the result in result.  Your code should not change the values of low and high.  Also, do not declare any additional variables -- use only counter, low, high and result.

result = 0
For counter = low to high
result = result + counter
Next

40176:  Given int variables k and total that have already been declared, use a for loop to compute the sum of the squares of the first 50 whole numbers, and store this value in total.  Thus your code should put 1*1 + 2*2 + 3*3 + ... + 49*49 + 50*50 into total.  Use no variables other than k and total.

total = 0
For k = 0 to 50
total = total + k*k
Next

40177:  Given an integer variable n that has been initialized to a positvie value and, in addition, Integer variables k and total that have already been declared, use a For loop to compute the sum of the cubes of the first n whole numbers, and store this value in total.  Use no variables other than n,k, and total.

total = 0
For k = 1 to n
total += k*k*k
Next

40288:  Write a for loop that computes the following sum: 5+10+15+20+...+485+490+495+500.  The sum should be placed in a variable sum that has already been declared and initialized to 0.  In addition, there is another variable, num that has also been declared.  You must not use any other variables.

sum = 0
For num = 5 to 500 step 5
sum += num
Next




40185:  Given an int variable n that has been initialzied to a positive value and, in addition, int variables k and total that have already been declared, use a do...while loop to compute the sum of the cubes of the first n whole numbersx, and store this value in total.  Use no variables other than n, k, and total.

total = 0
For k = 0 to n
total += k*k*k
Next

40385:  Assume the int variables i, lo, hi and result have been declared and that lo and hi have been initialized.  Write a for loop that adds the integers between lo and hi (inclusive), and stores the result in result.  Your code should not change the values of lo and hi.  Also, do not declare any additional variables -- use only i, lo, hi, and result.

result = 0
For i = lo to hi
result += i
Next

40374:  Given int variables k and total that have already been declared, use a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total.  Thus your code should put 1*1 + 2*2 + 3*3 + ... + 49*49 + 50*50 into total.  Use no variables other than k and total.

total = 0
For k = 0 to 50
total = total + k*k
Next

40375:  Given an integer variable n that has been initialized to a positvie value and, in addition, Integer variables k and total that have already been declared, use a For loop to compute the sum of the cubes of the first n whole numbers, and store this value in total.  Use no variables other than n,k, and total.

total = 0
For k = 1 to n
total += k*k*k
Next

40187:  Assume the int variables i, lo, hi and result have been declared and that lo and hi have been initialized.  Assume further tha result has been initialzied to the value 0.  Write a for loop that adds the integers between lo and hi (inclusive), and stores the result in result.  Your code should not change the values of lo and hi.  Also, do not declare any additional variables -- use only i, lo, hi, and result.

For i = lo to hi
result += i
Next




40223:  Give that two Integer variables, total and amount have been declared, write a loop that reads non-negative values into amount and adds them into total.  The loop termiantes when a value less tha 0 is read into amount.  Don't forget to initialize total to 0.  To read a value into amount use a method, getNum() that we provide for you a class named tC:  amount = TC.getNum();

total = 0
amount = 0
Do
total += amount
amount = TC.getNum()
Loop Until (amount < 0)

Section 6.1: Posttest Do Loops




40182:  Given an Integer variable k that has already been declared, use a Do...Loop While loop to print a single line consisting of 53 asterisks.  Use no variables other than k.

k = 0
Do
Console.Write("*")
k += 1
Loop Until (k = 53)

40183:  Given an int variable n that has already been declared and initialized to a positive value, use a Do...Loop While loop to print a single line consisting of n asterisks.  Use no variables other than n.

Do
Console.Write("*")
n -= 1
Loop Until (n = 0)


40387:  Write a statement that increments the value of the int variable total by the value of the int variable amount.  That is, add the value of amount to total and assign the result to total.

total += amount

40184:  Given int variables k and total that have already been declared, use a Do...Loop While loop to compute the sum of the squares of the first 50 whole numbers, and store this value in total.  Thus your code should put 1*1 + 2*2 + 3*3 + ... + 49*49 + 50*50 into total.  Use no variables other than k and total.

k = 1
total = 0
Do While (k <= 50)
total = (total + (k*k))
k += 1
Loop

Section 6.1 Pretest Do Loops




40178:  Given an integer variable k that has already been declared, use a While loop to print a single line consisting of 88 asterisks.  Use no variables other than k.

k = 0
Do While (k < 88)
Console.Write("*")
k += 1
Loop

40179:  Given an Integer variable n that has already been declared and initialized to a positive value, use a While loop to print a single line consisting of n asterisks.  Use no variables other than n.

Do While (n > 0)
Console.Write("*")
n -= 1
Loop

40180:  Given Integer variables k and total that have already been declared, use a While loop to compute the sum of the squares of the first 50 whole numbers, and store this value in total.  Thus your code should put 1*1 + 2*2 + 3*3 + ... + 49*49 + 50*50 into total.  Use no variables other than k and total.

k = 1
total = 0
Do While (k <= 50)
total = (total + (k*k))
k += 1
Loop

40181:  Given an Integer variable n that has been initialized to a positive value and, in addition, Integer variables k and total that have already been declared, use a While loop to compute the sum of the cubes of the first n whole numbers, and store this value in total.  Use no variables other than n,k, and total.

k = 0
total = 0
Do While (k <= n)
total = (total + (k*k*k))
k += 1
Loop




40379:  Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a while loop to compute the sum of the cubes of the first n counting numbers, and store this value in total.  Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total.  Use no variables other than n, k , and total.  Do NOT modify n.

k = 0
total = 0
Do While (k <= n)
total = (total + (k*k*k))
k += 1
Loop

40378:  Given int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total.  Thus your code should put 1*1 + 2*2 + 3*3 + ... + 49*49 + 50*50 into total.  Use no variables other than k and total.

k = 1
total = 0
Do While (k <= 50)
total = (total + (k*k))
k += 1
Loop

Section 5.3: Sub Procedures, Part II




41082:  Write the definition of a function named quadratic that receives three double parameters a,b,c.  If the value of a is 0 then the function prints the message "no solution for a=0" and returns.  If the value of "b squared" - 4ac is negative, then the code prints out the message "no real solutions" and returns.  Otherwise the function prints out the largest solution to the quadratic equation.  The formula for the solutions to this equation can be found here:  Quadratic Equation of Wikipedia.

Function quadratic(a as double, b as double, c as double) as double
Dim x,y as double
If (a = 0) Then
System.Console.Writeline("no solution for a=0")
ElseIf ((b*b - 4*a*c) < 0)
System.Console.Writeline("no real solutions")
Else
x = ((-b + Math.Sqrt(b*b - 4*a*c)) / (2*a))
y = ((-b - Math.Sqrt(b*b - 4*a*c)) / (2*a))
If (x > y) Then
System.Console.Writeline(x)
Else
System.Console.Writeline(y)
End If End If
End Function

Section 5: General Procedures




40439:  printTodaysDate is a function that accepts no parameters and returns no value.  Write a statement that calls printTodaysDate.

printTodaysDate()

40073:  Write the code for a message whose method name is sendSignal.  There are no arguments for this message.

sendSignal()

40440:  printErrorDescription is a function that accepts one int parameter and returns no value.  Write a statement that calls the function printErrorDescription, passing it the value 14.

printErrorDescription(14)

40074:  Write the code for a message whose method name is sendNumber.  There is one argument for this message, which is an int.  Send the number 5 as an argument int he message.

sendNumber(5)

40075:  Write the code for a message whose method name is sendVariable.  There is one argument for this message, which is an int.  Suppose an int variable called x has been declared and initialized to some value.  Send this variable in your message.

sendVariable(x)

40441:  printLarger is a function that accepts two int parameters and returns no value.  Two int vaiables, sales1 and sales2, have already been declared and initialized.  Write a statement that calls printLarger, passing it sales1 and sales2.

printLarger(sales1, sales2)

40076:  Write the code for a message whose method is sendTwo.  There are two arguements for thsi message: a double and an int.  Send the Double value of 15.955 and the Integer value of 133 in the message.

sendTwo(15.955, 133)




40077:  Write the code for a message whose mtehod name is sendObject.  There is one arguement for this message, which is of type Customer.  Suppose there is an object of type Customer, called John_Doe.  Send this object within your message.

sendObject(John_Doe)

Thursday, February 20, 2014

Section 4.3: Select Case Blocks




40503:  Write a Select Case statement that tests teh value of the Char variable response and performs the following actions:  if response is y, the message "Your request is being processed" is printed.  if response is n, the message "Thank you anyway for your consideration" is printed.  if response is h, the message "Sorry, no help is currently available is printed.  for any other value of response, the message "Invalid entry; please try again" is printed.

Select Case response
Case "y"
System.Console.Writeline("Your request is being processed")
Case "n"
System.Console.Writeline("Thank you anyway for your consideration")
Case "h"
System.Console.Writeline("Sorry, no help is currently available")
Case Else
System.Console.Writeline("Invalid entry; please try again")
End Select

40018:  Write a Select Case statement that tests the value of the integer variable semesterAverage and assigns a grade to the string variable grade based upon the usual criteria (i.e., greater than or equal to 90 is an A, between 80 and 89 inclusively is a B, etc).  If the average falls outside the range of 0 and 100, grade should be assigned the empty string and the message "Invalid semester average" should be displayed in a message box.

Select Case semesterAverage
Case 90 to 100
grade = ("A")
Case 80 to 89
grade = ("B")
Case 70 to 79
grade = ("C")
Case 60 to 69
grade = ("D")
Case 0 to 59
grade = ("F")
Case Else
MessageBox.Show("Invalid semester average")
grade = ("")
End Select




41121:  Given an int variable named yesCount and another int variable named noCount and a char variable named response, write the necessary code to read a value from the text box textBox into response and then carry out the following:  if the character typed in is a y or a Y then increment yesCount and print out "YES WAS RECORDED".  if the character typed in is an n or an N then increment noCount and print out "NO WAS RECORDED".  If the input is invalid just print the message "INVALID" and do nothing else.

response = textBox.Text
Select Case response
Case "y"
yesCount += 1
System.Console.Writeline("YES WAS RECORDED")
Case "Y"
yesCount += 1
System.Console.Writeline("YES WAS RECORDED")
Case "n"
noCount += 1
System.Console.Writeline("NO WAS RECORDED")
Case "N"
noCount += 1
System.Console.Writeline("NO WAS RECORDED")
Case Else
System.Console.Writeline("INVALID")
End Select

Tuesday, February 18, 2014

Section 4.2: If Blocks - ElseIf Clauses:




40121:  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.

If (age < 18) Then
minors += 1
ElseIf (age < 65) Then
adults += 1
Else
seniors += 1
End If

40367:  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 thorugh 64, and adds 1 to the variable seniors if age is 65 or older.

If (age < 18) Then
minors += 1
ElseIf (age < 65) Then
adults += 1
Else
seniors += 1
End If

40368:  Write an if/else statement that compares the double variable pH with 7.0 and makes the following assignments to the bool variables 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.

If (pH < 7) Then
neutral = false
base = false
acid = true
ElseIf (pH > 7) Then
neutral = false
base = true
acid = false
ElseIf (pH = 7) Then
neutral = true
base = false
acid = false
End If

41064:  Online Book Merchants offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with every purchase of 8 or more books.  It offers regular customers 1 free book with every purchase of 7 or more books, and offers 2 free books with every purchase of 12 or more books.  Write a statement that assigns freeBooks the appropriate value based on teh values of the bool variable isPremiumCustomer and the int variable nbooksPurchased.

If isPremiumCustomer Then
if (nbooksPurchased > 4) And (nbooksPurchased < 8)
freebooks = 1
elseif (nbooksPurchased >= 8)
freebooks = 2
elseif (nbooksPurchased < 5)
freebooks = 0
End If
Else
if (nbooksPurchased > 6) And (nbooksPurchased < 12)
freebooks = 1
elseif (nbooksPurchased >= 12)
freebooks = 2
elseif (nbooksPurchased < 7)
freebooks = 0
End If
End If




41062:  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 is 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.

If (speed > 65) Then
speeders += 1
Elseif (speed >= 40)
safeDrivers += 1
Elseif (speed >= 1)
slowDrivers += 1
Elseif (speed >= 0)
parkedDrivers += 1
Elseif (speed < 0)
reverseDrivers += 1
End If

40367:  Write an if/else statement that adds 1 to the variable minors if the variable age is elss 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.

If (age < 18) Then
minors += 1
ElseIf (age < 65) Then
adults += 1
Else
seniors += 1
End If

41062:  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 is 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.

If (speed > 65) Then
speeders += 1
Elseif (speed >= 40)
safeDrivers += 1
Elseif (speed >= 1)
slowDrivers += 1
Elseif (speed >= 0)
parkedDrivers += 1
Elseif (speed < 0)
reverseDrivers += 1
End If




41063:  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.

If (score1 > score2) Then
System.Console.Writeline("player1 wins")
player1Wins += 1
player2Losses += 1
ElseIf (score1 < score2)
System.Console.Writeline("player2 wins")
player2Wins += 1
player1Losses += 1
ElseIf (score1 = score2)
System.Console.Writeline("tie")
tieCount += 1
End If

Monday, February 17, 2014

Section 4.2 If Blocks: If Else




40364:  Write an if/else statement that compares the variable age with 65, adds 1 to the variable seniorCitizens if age is greater than or equal to 65, and adds 1 to the variable nonSeniors otherwise.

If (age >= 65) Then
seniorCitizens += 1
Else
nonSeniors += 1
End If

40365:  Write an if/else statement that compares the value of the variables soldYesterday and soldToday, and based upon that comparison assigns salesTrend the value -1 or 1.  -1 represents the case where soldYesterday is greater than soldToday; 1 represents the case where soldYesterday is not greater than soldToday.

If (soldYesterday > soldToday) Then
salesTrend = -1
Else
salesTrend = 1
End If

40366:  Write an if/else statement that assigns true to the variable fever is the variable temperature is greater than 98.6; otherwise it assigns false to fever.

If (temperature > 98.6) Then
fever = true
Else
fever = false
End If

41120:  Note: in mathematics, division by zero is undefined.  Assume a text box named textBox from which datat will ber ead.  Given a int variable named callsReceived and another int variable named operatorsOnCall write the necessary code to read values into callsReceived and operatorsOnCall and print out the number of calls received per operator (integer division with truncation will do).  HOWEVER, if any value read in is not valid input, just print the message "INVALID".

callsReceived = textBox.Text
operatorsOnCall = textBox.Text
If (operatorsOnCall = 0) Then
System.Console.WriteLine("INVALID")
Else
System.Console.WriteLine(callsReceived \ operatorsOnCall)
End If

Friday, February 14, 2014

Section 4.2 If Blocks: If Statement




40360:  Write a conditional that assigns true to the variable fever is the variable termperature is greater than 98.6.

If (temperature > 98.6) Then
fever = true
End If


40361:  Write a conditional that assigns 10,000 to the variable bonus if the value of the variable goodsSold is greater than 500,000.

If (goodsSold > 500000) Then
bonus = 10000
End If


40362:  Write a conditional that decreases the variable shelfLife by 4 if the variable outsideTemperature is greater than 90.

If outsideTemperature > 90 Then
shelfLife = shelfLife - 4
End If

40363:  Write a conditional that multiplies the values of the variable pay by one-and-a-half if the value of the boolean variable workedOvertime is true.

If workedOvertime = true Then
pay = pay * 1.5
End If

41061:  Assume that isIsosceles is a bool variable, and that the variables isoCount, triangleCount, and polygonCount have all been declared and initialized.  Write a statement that adds 1 to each of these count variables (isoCount, triangleCount, and polygonCount) if isIsosceles is true.

If isIsosceles = true Then
isoCount = isoCount + 1
triangleCount = triangleCount + 1
polygonCount = polygonCount + 1
End If

41060:  Assume that the variables gpa, dealsnList and studentName, have been declared and initialized.  Write a statement that both adds 1 to deansList and prints studentName to standard out if gpa exceeds 3.5.

If gpa > 3.5 Then
deansList = deansList + 1
MessageBox.Show(studentName)
End If




40217:  Given the integer variables x and y, write a fragment of code that assigns the larger of x and y to another integer variable max.

If x > y Then
max = x
Else
max = y
End If


41066:  Clunker Motors Inc is recalling all vehicles from model years 2001 - 2006.  Given a variable modelYear write a statement that prints the message "RECALL" to standard output if the value of  modelYear falls within that range.

If modelYear >= 2001 And modelYear <= 2006 Then
MessageBox.Show("RECALL")
End If

41069:  Clunker Motors Inc is recalling all vehicles in its Extravagant line from model years 1999 - 2002.  Given an int variable modelYear and a string modelName write a statement that prints the message "RECALL" to standard output if the values of modelYear and modelName match the recall details.

If modelYear >= 1999 And modelYear <= 2002 And modelName = "Extravagant" Then
MessageBox.Show("RECALL")
End If

41071:  Clunker Motors Inc is recalling all vehicles in its Extravagant line from model years 1999 - 2002 as well as all vehicles in its Guzzler line from model years 2004-2007.  Given an int varialbe modelYear and a string modelName write a statement that prints the message "RECALL" to standard output if the values of modelYear and modelName match the recall details.

If (modelYear >= 1999 And modelYear <= 2002 And modelName = "Extravagant") Or (modelYear >= 2004 And modelYear <= 2007 And modelName = "Guzzler")  Then
MessageBox.Show("RECALL")
End If

41065:  Clunker Motors Inc is recalling all vehicles from model years 1995 - 1998 and 2004 - 2006.  Given a variable modelYear write a statement that prints the message "RECALL" to standard output if the value of modelYear falls within those two ranges.

If (modelYear >= 1995 And modelYear <= 1998) Or (modelYear >= 2004 And modelYear <= 2006)  Then
MessageBox.Show("RECALL")
End If




41116:  Clunker Motos Inc is recalling all vehicles from  m odel years 2001 - 2006.  Given an int variable modelYear write a statement that prints the message "NO RECALL" to standard output if the value of modelYear DOES NOT fall within that range.

If modelYear < 2001 Or modelYear > 2006 Then
MessageBox.Show("NO RECALL")
End If

Section 4.1: Relational and Logical Operators - Ranges




41117:  Clunker Motors Inc. is recalling all vehicles from model years 2001-2006.  A bool variable named norecall has been declared.  Given an int variable modelYear write a statement that assigns true to norecall if the value of modelYear does NOT fall within the recall range and assigns false otherwise.  Do not use an if statement in this exercise!

norecall = (modelYear < 2001 Or modelYear > 2006)

41067:  Clunker Motors Inc. is recalling all vehicles from model years 2001-2006.  A bool variable named recalled has been declared.  Given an int variable modelYear write a statement that assigns true to recalled if the value of modelYear falls within the recall range and assigns false otherwise.

recalled = (modelYear >= 2001) And (modelYear <= 2006)

41068:  Clunker Motors Inc is recalling all vehicles from model years 1995-1998 and 2004-2006.  A bool variable named recalled has been declared.  Given an int variable modelYear write a statement that assigns true to recalled if the value of modelYear falls within the two recall ranges (the first range being 1995-1998, the second range being 2004-2006) and assigns false otherwise.  Do not use an if statement in this exercise!

recalled = ((modelYear >= 1995) And ((modelYear <= 1998)) Or (modelYear >=2004) And (modelYear <=2006))

41118:  Clunker Motors Inc is recalling all vehicles from model years 1995 - 1998 and 2004 - 2006.  A bool variable named recalled has been declared.  Given an int variable modelYear write a statement that assigns true to norecall if the value of modelYear does NOT fall within the two recall ranges (2001-2006) and assigns false otherwise.  Do not use an if statement in this exercise!

norecall = (modelYear < 1995 Or modelYear > 1998) And (modelYear < 2004 Or modelYear > 2006)

41101:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is an upper-case letter.

(x >= "A") And (x <= "Z")

41102:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is a lower-case letter.

(x >= "a") And (x <= "z")

41103:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is a decimal digit (0-9).

(x >= "0") And (x <= "9")




41104:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is an octal (Base 8) digit (0-7).

(x >= "0") And (x <= "7")

41115:  Assume that s is a string.  Write an expression whose value is true if and only if the value of s would come between "mortgage" and "mortuary" in the dictionary.

(s >= "mortgage") And (s <= "mortuary")

41105:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is a letter.

(x >= "A") And (x <= "Z") Or (x >= "a") And (x <= "z")

41106:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is alphanumeric, that is either a letter or a decimal digit.

((x >= "A") And (x <= "Z")) Or ((x >= "a") And (x <= "z")) Or ((x >= "0") And (x <= "9"))

41107:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is an hexadecimal (Base 16) digit (0-9 plus A-F or a-f).

((x >= "A") And (x <= "F")) Or ((x >= "a") And (x <= "f")) Or ((x >= "0") And (x <= "9"))

41113:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is NOT un upper-case letter.

((x < "A") Or (x > "Z"))

41114:  Assume that x is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if x is NOT a letter.

((x < "A") Or (x > "Z")) And ((x < "a") Or (x > "z"))

41072:  Clunker Motors, Inc is recalling all vehicles in its Extravagant line from model years 1999 - 2002 as well as all vehicles in its Guzzler line from model years 2004 - 2007.  A bool variable named recalled has been declared.  Given an int variable modelYear and a string modelName write a statement that assigns true to recalled if the values of modelYear and modelName match the recall details and assigns false otherwise.  Do not use an if statement in this exercise!

recalled = (modelYear >= 2004) And (ModelYear <= 2007) And (modelName = "Guzzler") Or (modelYear >= 1999) And (ModelYear <= 2002) And (modelName = "Extravagant")




41070:  Clunker Motos, Inc is recalling all vehicles in its Extragagant line from model years 1999-2002.  A bool variable named recalled has been declared.  Given an int variable modelYear and a string modelName write a statement that assigns true to recalled if hte values of modelYear and modelName match the recall details and assigns false otherwise.  Do not use an if statement in this exercise!

recalled = (modelYear >= 1999) And (ModelYear <= 2002) And (modelName = "Extravagant")

Thursday, February 13, 2014

Section 4.1: Relational and Logical Operators - Boolean Variables




40109:  Write an expression that evaluates to true if and only if the value of the boolean variable workedOvertime is true.

workedOvertime

41059:  Assume that a bool variable isQuadrilateral has been declared, and that an int variable, numberOfSides has been declared and initialized.  Write a statement that assigns the value true if numberOfSides is exactly 4 and false otherwise.

If numberOfSides = 4 Then
isQuadrilateral = true
Else
isQuadrilateral = false
End if

41058:  Assume that a bool variable worked Overtime has been declared, and that an int variable hoursWorked has been declared and initialized.  Write a statement that assigns the value true if hoursWorked is greater than 40 and false otherwise.

If hoursWorked > 40 Then
workedOvertime = true
Else
workedOvertime = false
End if

40050:  Write a literal representing the true value.

true

40049:  Write a literal representing the false value.

false

Section 4.1: Relational and Logical Operators - Logical Operators





40061:  Given that the variables x and y have already been declared and assigned values, write an expression that evaluates to true if x is positive (including zero) and y is negative.

(x >= 0) And (y < 0)

40062:  Given that the variables x and y have already been declared and assigned values, write an expression that evaluates to true if x is postivie (including zero) or y is negative.

(x >= 0) Or (y < 0)

40063:  Given two variables, isEmpty of type boolean, indicating whether a class roster is empty or not, and numberOfCredits of type int, containing the number of credits for a class, write an expression that evaluates to true if the class roster is empty or the class is exactly three credits.

(numberOfCredits = 3) Or isEmpty

40356:  Given the variables termperature and humidity, write an expression that evaluates to true if and only if the termperature is greater than 90 and the humidity is less than 10.

(temperature > 90) And (humidity < 10)

41111:  Assume that c is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if c is what is called a whitespace character (that is a space or a tab or a newline-- none of which result in ink being printed on paper).

(c = Chr(010)) Or (c = Chr(009)) Or (c = Chr(032))

40064:  Given two variables, isEmpty of type boolean, indicating whether a class roster is empty or not, and numberOfCredits of type int, containing the number of credits for a class, write an expression that evaluates to true if the class roster is not empty and the class is more than 2 credits.

numberOfCredits > 2 And isEmpty = false

40065:  Given two variables, isEmpty of type boolean, indicating whether a class roster is empty or not, and numberOfCredits of type integer, containing the number of credits for a class, write an expression that evaluates to true if the class roster is not empty and the class is one or three credits.

Not isEmpty And ((numberOfCredits= 1) Or (numberOfCredits = 3))



Section 4.1: Relational and Logical Operators - Relational Operators




40393:  Write an expression that evaluates to true if and only if the value of the integer variable x is equal to zero.

x = 0

40394:  Write an expression that evaluates to true if and only if the variables profits and losses are exactly equal.

profits = losses

40396:  Write an expression that evaluates to true if the value of index is greater than the value of lastIndex.

index > lastIndex

40397:  Working overtime is defined as having worked more than 40 hours during the week.  Given the variable hoursWorked, write an expression that evaluates to true if the employee worked overtime.

hoursWorked > 40

40399:  Given the variables numberOfMen and numberOfWomen, write an expression that evaluates to true if the number of men is greater than or equal to the number of women.

numberOfMen >= numberOfWomen

40400:  Given a double variable called average, write an expression that is true if and only if the variable's value is less than 60.0.

average < 60.0

40401:  Given an int variable grossPay, write an expression that evaluates to true if and only if the value of grossPay is less than 10,000.

grossPay < 10000

40354:  Write an expression that evaluates to true if and only if the integer x is greater than the integer y.

x > y

40398:  Write an expression that evaluates to true if the value x is greater than or equal to y.

x >= y

40906:  Write an expression that evaluates to true if and only if the integer variable numberOfShares is less than 100.

numberOfShares < 100

41109:  Assume that c is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if c is a space character.

c = " "




40395:  Given the char variable c, write an expression that is true if and only if the value of c is not the space character.

c <> " "

40907:  Write an expression that evaluates to true if the value of the integer variable x is divisible (with no remainder) by the integer variable y.  (Assume that y is not zero.)

x MOD y = 0

40346:  Write an expression that evaluates to true if the value of the integer variable widthOfBox is not evenly divisible by the integer variable widthOfBook.  (Assume that widthOfBook is not zero.  Note:  evenly divisible means divisible without a non-zero remainder.)

widthOfBox MOD widthOfBook <> 0

40345:  Write an expression that evaluates to true if the value of the integer variable numberOfPrizes is divisible (with no remainder) by the integer variable numberOfParticipants.  (Assume that numberOfParticipants is not zero.)

numberOfPrizes MOD numberOfParticipants = 0

40347:  Write an expression that evaluates to true if the integer variable x contains an even value, and false if it contains an odd value.

x MOD 2 = 0

41108:  Assume that c is a char variable that has been declared and already given a value.  Write an expression whose value is true if and only if c is a newline character.

c = Chr(010)

41110:  Assume that c is a char variable that has been delcared and already given a value.  Write an expression whose value is true if and only if c is a tab character.

c = Chr(009)

Monday, February 10, 2014

Section 3.3 Input and Output: Input and Output




41011:  Assume that name has been declared suitably for storing names (like "Amy", "Fritz" and "Moustafa")  Write some code that areads a value from a text box named textBox into name then prints the message "Greetings, NAME!!!" where NAME is replaced the value that was read into name.  For example, if your code read in "Hassan" it would print out "Greetings, Hassan!!!".

name = textBox.Text
MessageBox.Show("Greetings, " & name & "!!!")

41010:  Assume you have a text box named textBox.  Write some code that inputs the value in the text box into a variable name and then outputs "Greetings, NAME" to a message box (where NAME is replace by the value that was input read in the variable.  For example, if the text box originally contained "Rachel" your code would display "Greetings, Rachel" in the message box.

name = textBox.Text
MessageBox.Show("Greetings, " & name)

Section 3.3 Input and Output: Input





41006:  Write a statement that reads a word from standard input into firstWord.  Assume that firstWord has already been declared as an char array large enough to hold a 50-letter word.

firstWord = textBox.Text

40980:  Write an expression that attempts to read a double value from the TextBox textBox and stores it in a Double variable, x that has already been declared.

x = CDbl(textBox.Text)

40313:  Given an int variable datum that has already been declared, write a statement that reads an integer value from standard input into this variable.

datum = CInt(textBox.Text)

Section 3.3 Input and Output: Displaying Variables






40305:  Given an integer variable count, write a statement that writes the value of count to standard output.

MessageBox.Show(count)

40306:  Given a floating-point variable fraction, write a statement that writes the value of fraction to standard output.  Do not write anything else to standard output -- just the value of fraction.

MessageBox.Show(fraction)

40898:  Write a statement that prints the following to standard output:  "i=".  Just write one statement that generates the message above:  do not generate any extraneous spaces.  Do not declare variables, do not write a main() function, do not write a whole program.

MessageBox.Show("i=")

40307:  Given an integer variable i and a floating-point variable f, write a statement that writes both of their values to standard output in the folowing format: i=value-of-i f=value-of-f.  Thus, if i has the value 25 and f has the value 12.34, the output would be i=25 f=12.34.  But if i has the value 187 and f has the value 24.06, the output would be: i=187 f=24.06

MessageBox.Show("i=" & i & "f=" & f)

Section 3.3: Input and Output: Displaying Strings Literals




40003:  Write a statement that displays Hello, world to a message box.

MessageBox.Show("Hello, world")

40301:  Write a statement that displays Hello World in a MessageBox.

MessageBox.Show("Hello World")

40004:  Write a complete Main method that prints Hello, world to the screen.

Answer Coming Soon!

40005:  Suppose your name was Alan Turing.  Write a statement that would display your last name, followed by a comma, followed by a space and your first name in a MessageBox.

MessageBox.Show("Turing, Alan")

40006:  Suppose your name was George Gershwin.  Write a complete main method that would print your last name, followed by a comma, followed by a space and your first name.

Answer Coming Soon!

Monday, February 3, 2014

Section 3.2: Strings




41098:  Assume that message is a String variable.  Write a message to display its value in a MessageBox.

MessageBox.Show(message)

41099:  Assume that word is a String variable.  Write a statement to display the message "Today's Word-Of-The_Day is: " followed by the value of word in a MessageBox.

MessageBox.Show("Today's Word-Of-The-Day is: " + word)

Section 3.1: Numbers - Variable Names





85016:  Of the following variable names, which is the best one for keeping track of whether a patient has a fever or not?

temperature
feverTest
hasFever - correct answer
fever

Of the following variable names, which is the best one for keeping track of whether an integer might be prime or not?

divisible
isPrime
mightBePrime - correct answer
number

60106:  Which of the following is NOT a legal identifier?

outrageouslyAndScockinglyLongRunon
_42
_
lovePotionNumber9
7thheaver - correct answer

60110:  Which is the best identifier for a variable to represent the amount of money your boss pays you each month?

notEnough
wages
paymentAmount
monthlyPay - correct answer
money

Section 3.1: Numbers - Precedence




40036:  Write an expression that computes the average of the values 12 and 40.

(12 + 40) / 2

40037:  Write an expression that computes the average of the variables exam1 and exam2 (both declared and assigned values).

(exam1 + exam2) / 2

40333:  Write an expression that cmputes the average of the values 12 and 40.

(12 + 40) / 2

40334:  Write an expression that computes the average of the varialbes exam1 and exam2 (both declared and assigned values).

(exam1 + exam2) / 2

41015:  Each of the walls of a room with square dimensions has been built with two pieces of sheetrock, a smaller one and a larger one.  The length of all the smaller ones is the same and is stored in the variable small.  Similarly, the length of all the larger ones is the same and is stored in the variables large.  Write a single expression whose value is the total area of this room.  DO NOT use the pow function.

(large + small)^2

41013:  The dimensions (width and length) of room1 have been read into two variables: width1 and length1.  The dimensions of room2 have been read into two other variables: width2 and length2.  Write a single expression whose value is the total of the two rooms.

(width1 * length1) + (width2 * length2)

41020:  Assume that children is an integer variable containing the number of children in a community and that families is an integer variable containing the number of families in the same community.  Write an expression whose value is the average number of children per family.

children / families

41017:  In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 _ 1/4 + ... + 1/n.  So, the first harmonic number is 1, the second is 1.5, the third is 1.833... and so on.  Write an expression whose value is the 8th harmonic number.

1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8

41016:  Three classes of school children are selling tickets to the school play.  The number of tickets sold by these classes, and the number of children in each of the classes have been read into these variables:  tickets1, tickets2, tickets3 and class1, class2, class3.  Write an expression for the average number of tickets sold per school child.

(tickets1 + tickets2 + tickets3) / (class1 + class2 + class3)

41019:   In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 _ 1/4 + ... + 1/n.  So, the first harmonic number is 1, the second is 1.5, the third is 1.833... and so on.   Assume that n is an integer variable whose value is some integer N greater than 1.  Assume also that hn is a double variable whose value is the Nth harmonic number.  Write an expression whose value is the (n-1)th harmonic number.

hn - (1.0/n)




41018:  In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 _ 1/4 + ... + 1/n.  So, the first harmonic number is 1, the second is 1.5, the third is 1.833... and so on.   Assume that n is an integer variable whose value is some integer N greater than 1.  Assume also that hn is a double variable whose value is the Nth harmonic number.  Write an expression whose value is the (N+1)th harmonic number.

hn + 1.0 / (n + 1)

Section 3.1: Numbers - Mathematical Functions




41036:  The area of a square is stored in a double variable named area.  Write an expression whose value is the length of the diagonal of the square.

Math.Sqrt(2*area)

41037:  The length of a rectangle is stored in a double variable named length, the width in one named width.  Write an expression whose value is the length of the diagonal of the rectangle.

Math.Sqrt(length^2 + width^2)

Section 3.1: Numbers - Compound Assignments




40341:  Given an integer variable bridgePlayers, write a statement that increases the value of that variable by 4.

bridgePlayers += 4

40342:  Given an integer variable profits, write a statement that increases the value of that variable by a factor of 10.

profits *= 10

40189:  Write a statement that increments the value of the int variable total by the value of the int variable amount.  That is, add the value of amount to total and assign the result to total.

total += amount

40960:  Write a statement using a compound assignment operator to add 5 to val (an integer variable that has already been declared and initialized).

val += 5

40961:  Write a statement using a compound assignment operator to subtract 10 from minutes_left (an integer variable that has already been declared and initialized).

minutes_left -= 10

40962:  Write a statement using a compound assignment operator to cut the value of pay in half (pay is an integer variable that has already been declared and initialized).

pay \= 2

40963:  Write a statement using a compound assignment operator to multiply num_rabbits by 4, changing the value of num_rabbits (num_rabbits has already been declared and initialized).

num_rabbits *= 4

Section 3.1: Numbers - Assignment




40338:  Given an integer variable drivingAge that has already been declared, write a statement that assigns the value 17 to drivingAge.

drivingAge = 17

40947:  Write a statement to set the value of num to 4 (num is a variable that has already been declared).

num = 4

40339:  Given two integer variables oldRecord and newRecord, write a statement that gives newRecord the same value that oldRecord has.

newRecord = oldRecord

40340:  Given two integer variables matricAge and gradAge, write a statement that gives gradAge a value that is 4 more than the value of matricAge.

gradAge = matricAge + 4

40953:  Write a statement to set the value of area to the value of length times the value of width.  (The variables have already been declared and length and width have already been initialized.)

area = length * width

40948:  Given two integer variables num and highest, write a statement that gives highest the same value that num has.

highest = num

40501:  Given two integer variables OldRecord and NewRecord, write a statement that gives NewRecord the same value that OldRecord has.

newRecord = oldRecord

40949:  Write a statement to set the value of ans equal to the value of num plus 5.  (These variables have already been declared and num has already been initialized.

ans = num + 5

40950:  Write a statement to set the value of price equal to three times the value of cost.  (The variables have already been declared and cost has already been initialized.)

price = 3 * cost

40951:  Write a statement to add the values of x and y together, storing the result in sum.  (The variables have already been declared and x and y have already been initialized.)

sum = x + y

40952:  Write a statement to subtract tax from gross_pay and assign the result to net_pay.  (The variables have already been declared and groww_pay and tax have already been initialized.)

net_pay = gross_pay - tax




40957:  Write a statement to find the remainder remdr when num is divided by 5.  (The variables have already been declared and num has already been initialized.)

remdr = num Mod 5

40956:  Write a statement to assign to kilos the value of pounds divided by 2.2.  (The varialbes have already been declared and pounds has already been initialized.)

kilos = pounds / 2.2

40954:  Write a statement to multiply diameter by 3.14159 and assign the result to circumference.  (The variables have already been declared and diameter has already been initialized.

circumference = diameter * 3.14159

40955:  Write a statement to assign to weight_in_pounds the value of weight_in_kilos times 2.2.  (The variables have already been declared and weight_in_kilos has already been initialized.)

weight_in_pounds = weight_in_kilos * 2.2

40348:  Given two int variables, i and j, which have been declared and initialized, and two other int variables, itemp and jtemp, which have been declared, write some code that swaps the values i and j by copying their values to itemp and jtemp, respectively, and then copying itemp and jtemp to j and i respsectively.

     itemp = i
     jtemp = j
     j = itemp
     i = jtemp

40349:  Given three already declared int variables, i, j, and temp, write some code that swaps the values i and j.  Use temp to hold the value of i and then assign j's value to i.  The original value of i, which was saved in temp, can now be assigned to j.

     temp = i
     i = j
     j = temp

40350:  Given two int variables, firstPlaceWinner and secondPlaceWinner, write some code that swaps their values.  Declare any variables as necessary, but do not redeclare firstPlaceWinner and secondPlaceWinner.

     dim temp as Integer
     temp = firstPlaceWinner
     firstPlaceWinner = secondPlaceWinner
     secondPlaceWinner = temp

40351:  Given two double variables, bestValue and secondBestValue, write some code that swaps their values.  Declare any additional variables as necessary.

     dim temp as Double
     temp = bestValue
     bestValue = secondBestValue
     secondBestValue = temp