Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

UCLA Summer 2009 Homework 3: Vector Library Implementation, Assignments of Cryptography and System Security

A programming assignment from ucla math department's introduction to programming course during the summer 2009 session. Students are required to write a vector library for vector<int> by implementing several functions such as tally, mode, find_first, partial_sum, position_of_min, disagree, subsequence, and reverse_vector. The functions should be implemented in hwk3.cpp, while main.cpp will be provided for testing. Students must adhere to certain guidelines, such as not writing to cout or reading from cin, and ensuring successful program building.

Typology: Assignments

Pre 2010

Uploaded on 08/30/2009

koofers-user-ds2
koofers-user-ds2 🇺🇸

10 documents

1 / 3

Toggle sidebar

Related documents


Partial preview of the text

Download UCLA Summer 2009 Homework 3: Vector Library Implementation and more Assignments Cryptography and System Security in PDF only on Docsity! PIC 10A: Introduction to Programming UCLA Math Department Summer 2009 Homework 3– Vector Library Due Tuesday, July 14, 2009 4:00 PM Sharp As a programmer you will spend lots of time working with vectors, thus it is important to feel comfortable manipulating them. In this assignment we will write a small library for vector<int>. A library is a collection of functions centered around a common theme. In our library, for example, one function will calculate the partial sum for the elements in an vector. Another function will find the position of an integer in a vector. For each function that you write the specification will tell you the interface, that is, what parameters the function takes, what it returns, and what it must do. It’s your job to decide how the function will do it (the implementation). Two files will be provided, main.cpp and hwk3.cpp. The source file, hwk3.cpp, will contain your implementation of the functions. The other source file, main.cpp, will test the implementation of the functions in hwk3.cpp. You should add additional tests to this file. However, you will only turn in hwk3.cpp. We will provide our own main.cpp that will thoroughly test your functions. You may define additional functions in hwk3.cpp, but we will not directly call these functions. Your program should build successfully. No function in hwk3.cpp should write to cout or read from cin. If you would like to to print debugging messages use cerr instead. You must implement these functions: int tally(vector<int> v, int target); Return the number of elements in the vector that are equal to target. int mode(vector<int> v); Return the value that occurs most frequently in a vector. If two or more values oc- cur with the same frequency choose the one with the following property: the first occurrence of chosen value in the vector has a smaller index than the first occur- rence of the other values. (For example, if v.size() == 5 and v[0] == 1, v[1] == 2, v[2] == 2, v[3] == 3, and v[4] == 1 then mode(v)==1.) If v is empty then mode throws runtime_error. int find_first(vector<int> v, int target); Return the index of the first value in the vector that is equal to target. If this value does not exist then throw runtime_error. int partial_sum(vector<int>, int n); Return the sum of the first n elements of vector. If n is greater than the size of the vector or if n is less than zero throw runtime_error. int position_of_min(vector<int> v); Return the index of the smallest value in the vector. If this value occurs multiple times return the smallest index. If v is empty throw runtime_error. int disagree(vector<int> v1, vector<int> v2); Return the index of the first corresponding elements of v1 and v2 that are not equal. If the vectors are equal up to the point where one or both of the vectors runs out, return the smaller of v1.size() and v2.size(). For example: vector<int> v5; v5.push_back(1); v5.push_back(2); v5.push_back(3); vector<int> v6; v6.push_back(1); v6.push_back(3); vector<int> v7; v7.push_back(1); disagree(v5, v5); // returns 3 disagree(v5, v6); // returns 1 disagree(v5, v7); // returns 1 bool subsequence(vector<int> v1, vector<int> v2); Return true if all the elements in v2 appear in v1 in the same order (although maybe not consecutively). Otherwise, return false. For example: vector<int> big; big.push_back(1); big.push_back(2); big.push_back(3); big.push_back(4); big.push_back(2); vector<int> v8; v8.push_back(1); v8.push_back(2); v8.push_back(2); vector<int> v9; v9.push_back(3); v9.push_back(3); v9.push_back(2); subsequence(big, v8); // returns true subsequence(big, v9); // returns false
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved