CS 1428 Practice Packet
CS 1428 Practice Packet
CS 1428 Practice Packet
Instructions:
This worksheet includes a mix of coding, tracing, and conceptual questions. These questions are
designed test your knowledge of the covered material. For the best practice, try doing the problems
by hand. You are welcome collaborate with a single partner but make sure to include their name.
When class is finished, upload your worksheet so you can get extra credit.
Problem 1. Trace the following code segment and show the values of the variables after the code
is executed. Assume the user inputs 2 followed by a 4.
1 #i n c l u d e <i o s t r e a m>
2 #i n c l u d e <cmath>
3 u s i n g namespace std ;
4
5 i n t main ( )
6 {
7 i n t iA , iB , iC ;
8 cout << " E n t e r iA : " ;
9 cin >> iA ;
10 cout << " E n t e r i B : " ;
11 cin >> iB ;
12 iA += ( iB − iA ) % 4 * 2 + 13;
13 iC = pow ( iB , 2) − 5 / 3 ;
14 cout << iA << " \n " << iB << " " << " iC " ;
15 }
iA iB iC Output
Category Cost
Housing $1250.00
Food $250.00
Entertainment $110.00
Misc $50.00
(a) Using C++, define a structure called MonthlyBudget which is designed to hold each of the
categories. Additionally, the structure should also hold the name of the student.
(b) Using the MonthlyBudget structure, create a MonthlyBudget called budgetA and populate with
Wazowski’s budget shown in the table.
(c) Assume you have have another MonthlyBudget called budgetB, write the line of code to find
the absolute difference between budgetA and budgetB entertainment costs.
Problem 3. AUTO is writing code to help with piloting the ship. Assume that the code snipet
is located in main and that all the needed libraries have been imported. If the input file logs.txt
exists, assume it is correct and is populated as follows:
logs.txt
820.56 -400.26
1 // C r e a t e v a r i a b l e s
2 double dDistance1 , dDistance2 , dDistanceTotal = 0 ;
3 ofstream fileInput ;
4
5 // Open t h e f i l e
6 fileInput . open ( " l o g s " ) ;
7
8 // Check i f f i l e e x i s t s . I f i t does not , t h e program should e x i t .
9 i f ( fileInput )
10 {
11 cout << " F i l e does not e x i s t . E x i t i n g program . " ;
12 }
13
14 // Read from t h e f i l e
15 cout << " Reading l o g . t x t f i l e \n "
16 fileInput >> dDistance1 ;
17 fileInput >> dDistance1 ;
18
19 // C a l c u l a t e TOTAL d i s t a n c e t r a v e l e d
20 dDistanceTotal = dDistance1 + dDistance2 ;
21
22 // C l o s e t h e f i l e
23 fileInput . close ;
(a) For each error, circle the type of error (either syntax or logic), state the line number the error
occurs on, and the write the fix for the error.
Error 1 Error 2 Error 3 Error 4
1. If "sales.txt" is not provided, your program should output "No file given" and return -1. How-
ever, if the file is provided, it will never contain invalid input, whitespace or blank lines. The
file will always contain 3 valid sales numbers.
Program
1 #i n c l u d e <i o s t r e a m>
2 #i n c l u d e <f s t r e a m>
3 u s i n g namespace std ;
4
5 i n t main ( )
6 {
7 // Your code goes here
8
9 return 0;
10 }
Sales.txt:
1 120.88
2 55.77
3 3.20
Program Output:
(a) Using C++, write a program output the total number of credits the JAWAS earned at the end
of the day.
Problem 4 Extra page: Using C++, write a program output the total number of credits the JAWAS
earned at the end of the day.