CS16: Programming Assignment 08

Introduction

The assignment for this week will utilize pointers and dynamic arrays.

This assignment is due on February 27th at 23:59.

Top

Step 1: Open a Terminal

The first step in every assignment will be to open a terminal window, which will be the environment you use to write, compile, and run your programs.

For a reminder on how to do this step, take a look at Step 2 of PA 01.

Top

Step 2: Programming Assignment 08 Directory

Start by changing into your CS 16 directory:

$ cd cs16

And then create and move into a PA 08 directory:

$ mkdir pa08
$ cd pa08

Remember that at any time, you can check what directory you are current in with the command pwd.

Top

Step 3: Create Your C++ Files

This week, you will need to create three files called numdoubles.cpp, reverse.cpp, and grades.cpp:

$ touch numdoubles.cpp reverse.cpp grades.cpp
Top

Step 4: Edit Your C++ Files

You will now need to complete each source file. Each corresponds to one of the problems listed below, which make up this lab.

For a reminder on how to open the text editor and how edit files, take a look at Step 5 of PA 01.

Top

Step 5: Write the Code

This assignment consists of three problems, each of which is described below. The first problem is worth 20 points, and the last two are each worth 40 points. Each should be solved in its own file and both must be submitted for full assignment credit.

Chapter 9: Practice Program 2

This should be solved in the numdoubles.cpp file.

Write a program that asks the user to input an integer named numDoubles. Create a dynamic array that can store numDoubles doubles and make a loop that allows the user to enter a double into each array entry. Loop through the array, calculate the average, and output it. Delete the memory allocated to your dynamic array before exiting.

You must delete all memory allocated to dynamic arrays before the program ends.

The program should print a string of text to the terminal before getting each line of input from the user. A session should look like one of the following examples (including whitespace and formatting), with possibly different numbers in the output:

Enter numDoubles:
5
Enter doubles (each on a new line):
3.14
4.7
1.6
16.16
-5.3
The average is:
4.060

Enter numDoubles:
1
Enter doubles (each on a new line):
99.9999
The average is:
100.000

The strings printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line).

The average should be printed to three places after the decimal.

Chapter 9: Programming Project 4

This should be solved in the reverse.cpp file.

Write a function that takes a char array as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the preceding character, and so on, until the entire string is reversed.

Then, write a main function that takes a string as input from the user and prints the reversed string (using the above function). All input strings will be 100 characters or fewer.

You must use pointers to reverse the array.

If you use dynamic arrays (not required), you must delete all memory allocated to any dynamic arrays before the program ends.

The program should print a string of text to the terminal before getting each line of input from the user. A session should look like one of the following examples (including whitespace and formatting), with possibly different numbers in the output:

Enter string:
CS16 is great!
The string reversed is:
!taerg si 61SC

Enter string:
oddlength
The string reversed is:
htgnelddo

The strings printed by the program should include a newline at the end.

Chapter 9: Programming Project 8

This should be solved in the grades.cpp file.

Write a program that outputs a histogram of student grades for an assignment. First, the program will input the number of grades and create a dynamic array to store the grades. Then, the program should input each student's grade as an integer and store the grade in the dynamic array.

The program should then scan through the array and compute the histogram. In computing the histogram, the minimum value of a grade is 0 but your program should determine the maximum value entered by the user. Use a dynamic array to store the histogram. Output the histogram to the console.

For example, if the input is:

Enter number of grades:
6
Enter grades (each on a new line):
20
30
4
20
30
30

Then the output histogram should be:

  4 *
 20 **
 30 ***

You must delete all memory allocated to dynamic arrays before the program ends.

The program should print a string of text to the terminal before getting each line of input from the user. A session should look like one of the following examples (including whitespace and formatting), with possibly different numbers and numbers of asterisks in the output:

Enter number of grades:
6
Enter grades (each on a new line):
20
30
4
20
30
30
Histogram:
  4 *
 20 **
 30 ***

Enter number of grades:
7
Enter grades (each on a new line):
1
1
100
100
100
99
50
Histogram:
  1 **
 50 *
 99 *
100 ***

The strings printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line).

The scores should be right-justified with width 3 (there should be two spaces before a one-digit number, and one space before a two-digit number). Hint: remember setw.

Top

Step 6: Compile the Code

To compile our code, we will use the same g++ command as last week. The following three commands will compile the three source files (in the same order as listed above):

$ g++ -std=c++11 -o numdoubles numdoubles.cpp
$ g++ -std=c++11 -o reverse reverse.cpp
$ g++ -std=c++11 -o grades grades.cpp

If the compilation is successful, you won't see any output from the compiler. You can then use the following commands to run your programs:

$ ./numdoubles
$ ./reverse
$ ./grades

Remember to re-compile the relevant files after you make any changes to the C++ code.

If you encounter memory errors (like segmentation faults or bus errors), you will need to make use of gdb to diagnose and solve the errors. Refer to the first problem of PA07 for a reminder on how to use gdb.

Top

Step 7: Submit

Once you are satisfied that your program is correct, it is time to submit it. Login at https://submit.cs.ucsb.edu/session, then navigate to “CS16_w16” and click on “Programming Assignment 8”. Then click “Make Submission”, and make your submission the same way as last week. Remember to submit all three .cpp files.

Please remember that you must submit the program to obtain any credit for the assignment; just completing the program is not enough.

Once you submit, you should see a page detailing your submission. The system will automatically grade your program and will show you the results on this page after a 1 minute delay.

You can alternatively submit your code from the command line (terminal) on any CS machine, including the Phelps lab machines or the CSIL server. You can use this method when logged in remotely. To submit the the three source files to this assignment by running the command:

$ ~submit/submit -p 434 numdoubles.cpp reverse.cpp grades.cpp

You can copy the URL shown in the output of the above and paste into a web browser to reach the submission result page.

Top

Step 8: Check Submission Results

After the 1 minute delay, the submit system will show your score and give you feedback on your submission. Refresh the webpage after a minute to see this information.

You may submit multiple times. The highest score among submissions uploaded before the deadline will be used as your assignment grade.

Top

Step 9: Done!

Once your submission receives a score of 100/100, you are done with this assignment.

If you are in the Phelps lab or in CSIL, make sure to log out of the machine before you leave. Also, make sure to close all open programs before you log out. Some programs will not work next time if they are not closed. Remember to save all your open files before you close your text editor.

If you are logged in remotely, you can log out using the exit command:

$ exit