Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views5 pages

CS 1428 Practice Packet

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Mini-Codeathon Name:

CS 1428: Fall 2024 Partner:


Instructor: Dr. David Patrick

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

(a) What is the value of iA at Line 15?

(b) What is the value of iB at Line 15?

(c) What is the value of iC at Line 15?

(d) What is the FULL output of the program?


Problem 2. Mike Wazowski has established the following monthly budget.

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

Syntax/Logic Syntax/Logic Syntax/Logic Syntax/Logic


Line #
Fix

Error 5 Error 6 Error 7 Error 8

Syntax/Logic Syntax/Logic Syntax/Logic Syntax/Logic


Line #
Fix
Problem 4. R2D2 is in charge of the JAWAS parts shop on Tatooine. At the end of every day, he gets
a file containing 3 sales, one from each register. You are in charge of writing software to update
R2D2 to calculate how much money the JAWAS made in sales that day. Below is the program start,
specifications, and an example output and input.

Program Specifications and Assumptions

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.

2. You will output the results to the terminal, not a file.

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:

1 Reading sales record


2 The total number of credit earned today was 1 7 9 . 8 5 .

(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.

You might also like