Docsity

Solved Questions for Assignment 1 - Data Structures | CSCI 1200, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
CSCI-1200 Computer Science II — Spring 2006
Homework 1 — Getting Started
This two-part homework explores the background and review material covered 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 pro-
gramming 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. Program-
ming assignments will become more challenging soon, so if you struggle with this one, you should work
hard to practice and catch up. Before starting this assignment, make sure you have read and understood
the statement of Academic Integrity for Programming Assignments.
Part 1: Short Answer
While it is clearly possible for you to create a program file containing the code below 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.
In your main CSII folder on your laptop, create a subfolder for homeworks and within that folder create
a subfolder hw1 for this assignment. Create and open a plain text file named part1.txt and write your
solutions for Part 1 in this file.
1. What is the output of the following program, which is supposed to compute the average, max and
min of an array of numbers? Explain 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) {
--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);
pf3

Partial preview of the text

Download Solved Questions for Assignment 1 - Data Structures | CSCI 1200 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Computer Science II — Spring 2006

Homework 1 — Getting Started

This two-part homework explores the background and review material covered 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 pro- gramming 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. Program- ming assignments will become more challenging soon, so if you struggle with this one, you should work hard to practice and catch up. Before starting this assignment, make sure you have read and understood the statement of Academic Integrity for Programming Assignments.

Part 1: Short Answer

While it is clearly possible for you to create a program file containing the code below 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. In your main CSII folder on your laptop, create a subfolder for homeworks and within that folder create a subfolder hw1 for this assignment. Create and open a plain text file named part1.txt and write your solutions for Part 1 in this file.

  1. What is the output of the following program, which is supposed to compute the average, max and min of an array of numbers? Explain 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; }

  1. 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) { float y = 1.0; int a = 6; cout << "x = " << x << ", y = " << y << ", a = " << a << endl; x = 2; }

cout << "x = " << x << ", y = " << y << ", a[0] = " << a[0] << endl; return 0; }

  1. For each of the two functions in the following code, give an order notation count of the number of operations it requires. Justify your answer briefly. For extra credit, rewrite the second function so that it has a better order notation estimate, and give this estimate.

// Count the number of entries in an array that have both an even // index (subscript) and an even value. int count_evens( int arr[], int n ) { int count = 0; for (unsigned int i=0; i<n; i+=2) if (arr[i] % 2 == 0) ++count; return count; }

// Given an array of floats, form an array of sums. Entry 0 // in the array of sums is the addition of all values in the // array. Entry 1 is the addition of values in locations // 1..n-1. In general, entry i is the addition of the values // in locations i..n-1. void subsequence_sum( float arr[], int n, float sums[] ) { for (int i = 0; i < n; ++i) { sums[i] = 0.0; for ( int j=i; j<n; ++j ) sums[i] += arr[j]; } }