Homework 1 Solutions - Data Structures | CSCI 1200, Assignments of Data Structures and Algorithms

Material Type: Assignment; Class: DATA STRUCTURES; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 08/09/2009

koofers-user-jct
koofers-user-jct 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Science II CSci 1200
Homework 1
This two-part homework explores the background and review material cov-
ered in Lectures 1 and 2. Part 1 is a set of short problems, similar to problems
that might appear on a test. Part 2 is a short programming problem. These
problems are not particularly difficult. Students who feel uncomfortable about
their programming background should view this as an opportunity to gauge
their current skills. Programming assignments will become more challenging
quickly, so if this one is not relatively easy, you should work hard to practice
and catch up.
Solutions to the problems in Part 1 must be turned in during class on Fri-
day, January 28. Printouts and hand-written solutions are both acceptable.
Electronic submissions will NOT be accepted, and late homework (for Part 1)
will NOT be accepted. Put your name and lab section number on the top of
the first page.
Solutions to the programming problem in Part 2 are due electronically by
11:59:59pm on Thursday, January 27th. The standard rules about late
homework submissions (see the course syllabus) apply. See below for updated
electronic submssion instructions.
Part 1
1. (10 points) What is the output of the following program, which is sup-
posed to compute the average, max and min of an array of numbers? Ex-
plain why this output occurs and give a small set of changes to the code
to fix it. Do not fix the code by changing its structure; in other words
there must be a for loop and a while loop in the stats calculation
function, and the while loop must count backwards.
#include <iostream>
using namespace std;
void
stats_calculation( float values[], int & n, float & max_value, float min_value,
float & avg )
{
// Find the average value
float sum = values[0];
for ( int i=0; i<n; ++i )
sum += values[n];
avg /= n;
// Find the maximum and minimum values.
min_value = values[n-1], max_value = values[n-1];
while ( n > 0 )
pf3
pf4
pf5

Partial preview of the text

Download Homework 1 Solutions - Data Structures | CSCI 1200 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Computer Science II — CSci 1200

Homework 1

This two-part homework explores the background and review material cov- ered in Lectures 1 and 2. Part 1 is a set of short problems, similar to problems that might appear on a test. Part 2 is a short programming problem. These problems are not particularly difficult. Students who feel uncomfortable about their programming background should view this as an opportunity to gauge their current skills. Programming assignments will become more challenging quickly, so if this one is not relatively easy, you should work hard to practice and catch up. Solutions to the problems in Part 1 must be turned in during class on Fri- day, January 28. Printouts and hand-written solutions are both acceptable. Electronic submissions will NOT be accepted, and late homework (for Part 1) will NOT be accepted. Put your name and lab section number on the top of the first page. Solutions to the programming problem in Part 2 are due electronically by 11:59:59pm on Thursday, January 27th. The standard rules about late homework submissions (see the course syllabus) apply. See below for updated electronic submssion instructions.

Part 1

  1. (10 points) What is the output of the following program, which is sup- posed to compute the average, max and min of an array of numbers? Ex- plain why this output occurs and give a small set of changes to the code to fix it. Do not fix the code by changing its structure; in other words there must be a for loop and a while loop in the stats calculation function, and the while loop must count backwards.

#include using namespace std;

void stats_calculation( float values[], int & n, float & max_value, float min_value, float & avg ) { // Find the average value float sum = values[0]; for ( int i=0; i<n; ++i ) sum += values[n]; avg /= n;

// Find the maximum and minimum values. min_value = values[n-1], max_value = values[n-1]; while ( n > 0 )

--n; if ( values[n] > max_value ) max_value = values[n]; else if ( values[n] < min_value ) min_value = values[n]; } }

int main() { // Initialize an array in order to test the function. The size of // the array (6) is automatically determined from the number of // values in the list. float a[] = { 12.3, 15.4, 1.5, 7.8, 2.3, 8.9 }; int size = 6; float min_value = 0, max_value = 0, average = 0;

stats_calculation( a, size, max_value, min_value, average );

cout << "Here are the values: "; for ( int i=0; i<size; ++i ) cout << a[i] << " "; cout << ’\n’; cout << "average = " << average << ’\n’ << "max = " << max_value << ’\n’ << "min = " << min_value << ’\n’; return 0; }

While it is clearly possible for you to create a program file containing this code and then compile and execute the program, you will not have the luxury of doing this on a test. You should practice by studying the code carefully yourself first and trying to determine the answers. This is a good skill to develop.

  1. (5 points) What is the output of the following program?

#include using namespace std;

int main() { int x=5, y=4; float a[3] = {1.0, 2.0, 3.0};

if ( x > y ) {

Write a program that performs a simple version of income tax calculation for a set of individuals. Each individual is represented by a single line of input containing two integers and a float. The first integer is the person’s tax id number, the second is the number of dependents, and the third is person’s earned income this year. Before reading the individuals, your program must read a single integer giving the number of individuals for whom to calculate taxes. This number will be at most 100. The tax owed by an individual is calculated by first subtracting the $ times the number of dependents from the earned income. If this reduces the income below 0, no tax is owed. Otherwise, the tax calculation is

  • 5% of the first $10,000 dollars,
  • 15% of the earned money between $10,000 and $40,000, and
  • 25% of any money earned over $40,000.

For example, if a person with id 12345 has 3 dependents and earned $37,500, the subtraction for dependents reduces this amount to $30,000, and the tax calculation yields

  1. 05 ∗ 10000 + 0. 15 ∗ (30000 − 10000) = 3500

As another example, if a person with id 9999 has 2 dependent and earned $95,000, then the subtraction for dependents reduces this amount to $90,000, and the tax calculation yields

  1. 05 ∗ 10000 + 0. 15 ∗ 30000 + 0. 25 ∗ (90000 − 40000) = 500 + 4500 + 12500 = 17500

After reading all of the input the program should calculate all the taxes, output each tax id and the amount owed (each individual on a separate line), and then output the tax ids of individuals who owe no tax and the ids of individuals who owe at least $20,000.

Examples

Given the input

5 43231 2 27000 12324 3 7000 54989 0 50000 37434 4 12500 34763 3 85000

the output of the program should be

43231 owes $ 12324 owes $

54989 owes $ 37434 owes $ 34763 owes $ These individuals owe no taxes: 12324 No individuals owe at least $

If instead the input is

4 12324 3 70000 54989 2 500000 37434 4 77500 34763 3 185000

then the output should be

12324 owes $ 54989 owes $ 37434 owes $ 34763 owes $ No individuals owe $ These individuals owe at least $20000: 54989 34763

The output from your program should be as close to this as possible.

Further Requirements

  • You must write a function to calculate the tax for an individual.
  • You should use named constants for the thresholds and rates given above. This makes your program much easier to read and easier to change.
  • Clearly, arrays are needed to solve the problem. You don’t need to use structs or classes, which we will cover soon.

Submission Instructions

(These instructions, which are also posted on the course web site, differ slightly from what was indicated in the homework guidelines distributed on the first day of class.) Submit your program in a single file. For Homework 1, you should submit your program in a single .cpp file. For other assignments, if you have more than one source file, you will need to zip them into a single .zip file. Use your RPI login/password to access the following web site:

https://cgi.cs.rpi.edu/submit/submit.html?course=cs