Structured Programming Lab Writeup
Problems 1-6 are basic level problems meant to check your understanding of specific topics. 7-9
are more complex and use multiple topics. You can write them all in one file or in separate ones.
This lab will not be taken up for any submission, but please email
cooper.oxford@smail.astate.edu if you have questions or want feedback.
DO NOT USE AI ASSISTANCE UNTIL YOU TRY ON YOUR OWN. This is not for a grade,
take your time to get comfortable with these topics.
1. Functions
a. Create a function named addValue(int num) that adds 10 to num and prints it
inside the function.
b. Create a function named addReference(int &num) that adds 10 to num.
c. In main, declare an integer score = 50.
d. Call addValue(score) and print score afterwards to see if it changed.
e. Call addReference(score) and print score afterwards to see if it changed
2. Loops
a. Write a standard for loop that counts from 1 to 20.
b. Inside the loop, use an if statement to check if the number is even.
c. Print only the even numbers on a single line, separated by spaces
3. Vectors
a. Include the <vector> library.
b. Create an empty vector of integers named dailyTemps.
c. Use .push_back() to add the following temperatures: 72, 75, 80, 68.
d. Print the size of the vector.
e. Print the value of the third element (index 2).
f. Bonus: Use a for loop to find the highest temp and lowest temp. Print them both.
4. Structs
a. Define a struct name playingCard outside of main
b. Give it members (remember data types):
i. Suit
ii. Rank (number)
iii. Face card (use a boolean for this. A face card should have true for this
member)
c. In main, initialize an object named card1
d. Use the dot operator to modify its members
e. Initialize another object named card2, but define its member values on the same
line that you define the object.
f. Use an if statement and the dot operator to compare the suits of the two cards. If
they match, print something to the console showing that.
i. Bonus: Create a function to accept 5 card objects and use if statements to
see if the hand contains a full house (3 of a kind and pair, cannot be the
same cards)
5. Strings
a. Declare two strings, firstName and lastName with values of your choosing.
b. Create a new variable fullName by concatenating the firstName and lastName
together. Make sure the fullName has a space in it.
Create a new variable, title, that contains the first initial of the first name,
concatenated with the last name.
i. For example, for firstName = John, lastName = Doe, title = J. Doe
6. Pointers
a. Declare an integer variable myAge = 25.
b. Declare an integer pointer ptrAge and assign it the address of myAge.
c. Print the address stored in the pointer.
d. Print the value of the variable by dereferencing the pointer..
e. Change the value of myAge to 26 using only the pointer.
7. Average of a Vector
a. Create a function calculateAverage that accepts a vector<double> (passed by
reference to save memory) and returns a double.
b. Inside the function, use a for loop to sum the elements and divide by the vector
size to find the average.
c. In main, create a vector with 5 distinct grades.
d. Call the function and print the returned average.
i. Bonus: print the class high and low as well.
8. Inventory System (Vector of Structs)
a. Define a struct called Item with string name, double price, and int quantity.
b. In main, create a vector of type Item.
c. Push back 3 distinct items (e.g., "Apple" 0.99, 2 , "Bread" 2.50, 1, "Milk" 3.00,
4).
d. Use a for loop to iterate through the vector and print the name and price of every
item in a formatted list.
e. Bonus: Create a function that will calculate the total value of all currently held
inventory and print it to the console
9. Pointer Swap
a. Write a function called swapPtr that takes two integer pointers (int *a, int *b) as
arguments.
b. Inside the function, use a temporary variable and dereferencing to swap the values
of the integers the pointers point to.
c. In main, declare x = 100 and y = 200.
d. Pass the addresses of x and y into swapPtr.
e. Print x and y to prove they have swapped