5 Problems on Algorithm and Data Structures - Practice Test 1 | CP SC 212, Exams of Algorithms and Programming

Material Type: Exam; Class: ALGS/DATA STRUCTURES; Subject: COMPUTER SCIENCE; University: Clemson University; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 07/28/2009

koofers-user-efy
koofers-user-efy 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CpSc 212—Goddard—SummerI 09
Solutions for Practice Test 1
1. (a) if( x%2 == 0 ) cout << "Even";}
(b) Gives bar access to the private parts of Foo
(c) Copying a huge parameter is wasterful.
(d) char[] name = "Goddard";
2. (a) O(n4) (b) O(nlog n)
3. Naive algorithm is O(n2). A binary search on each row gives O(nlog n). (There is even
a linear-time algorithm: start in top-right hand corner and move left on a 1and down
on a 0.)
4. a) it prevents the value val from being changed
b) c) d)
MyInteger ( int v ) : val(v) {} // value must be set in initializer
bool operator==( const MyInteger & oo )
{
return val == oo.val;
}
MyInteger operator+( const MyInteger & oo )
{
return MyInteger( val + oo.val );
}
pf2

Partial preview of the text

Download 5 Problems on Algorithm and Data Structures - Practice Test 1 | CP SC 212 and more Exams Algorithms and Programming in PDF only on Docsity!

CpSc 212—Goddard—SummerI 09

Solutions for Practice Test 1

  1. (a) if( x%2 == 0 ) cout << "Even";}

(b) Gives bar access to the private parts of Foo (c) Copying a huge parameter is wasterful. (d) char[] name = "Goddard";

  1. (a) O(n^4 ) (b) O(n log n)
  2. Naive algorithm is O(n^2 ). A binary search on each row gives O(n log n). (There is even a linear-time algorithm: start in top-right hand corner and move left on a 1 and down on a 0 .)
  3. a) it prevents the value val from being changed

b) c) d)

MyInteger ( int v ) : val(v) {} // value must be set in initializer

bool operator==( const MyInteger & oo ) { return val == oo.val; }

MyInteger operator+( const MyInteger & oo ) { return MyInteger( val + oo.val ); }

DBag( const DBag &other) : count( other.count), A(new double[MAX_COUNT]) { for(int i=0; i<count; i++) A[i] = other.A[i]; }

void delete(double item) { for(int i=0; i<count; i++) if( item==A[i] ) { A[i]=A[count-1]; count--; return; } }

ostream &operator<< (ostream &out, const DBag &myDBag) { for(int i=0; i<myDBag.count-1; i++) out << myDBag.A[i]+":"; out << myDBag.A[myDBag.count-1]; return out; }