Due Date: See Blackboard

Similar documents
Due Date: See Blackboard

Due Date: See Blackboard

Due Date: See Blackboard

Due Date: See Blackboard

Due Date: See Blackboard

Due Date: See Blackboard

Due Date: See Blackboard

Due Date: See Blackboard. {a n+1 b 2n n 0}

CS 1337 Computer Science II Page 1

CS 4301 Algorithmic Languages and Compilers Page 1

Due Date: See Blackboard

Due Date: See Blackboard

Due Date: See Blackboard

Lab 6 Due Date: Wednesday, April 5, /usr/local/3302/include/direct linking loader.h Driver File:

Lab Instructor : Jean Lai

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

Chapter 10 - Notes Applications of Arrays

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Sonoma State University Computer Science Department CS 115 Fall 2010 Watts. Project 3 Part 1

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

CS2141 Software Development using C/C++ C++ Basics

Midterm Practice Exam

Cours de C++ Introduction

Use the dot operator to access a member of a specific object.

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

Chapter Four: Loops II

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

VARIABLES & ASSIGNMENTS

Overloading Operators in C++

Summary of basic C++-commands

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

Lab 2: ADT Design & Implementation

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Lab 8 The Stack (LIFO) Structure

Object oriented programming

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

C/C++ Programming. Session 10

Lambda functions. Zoltán Porkoláb: C++11/14 1

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Object oriented programming

Week 5: Files and Streams

Programming Language. Functions. Eng. Anis Nazer First Semester

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Chapter 15 - C++ As A "Better C"

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

Unit 10. Linux Operating System

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Functions, Arrays & Structs

Ch 6. Functions. Example: function calls function

C++ For Science and Engineering Lecture 12

True or False (14 Points)

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Pre- Defined Func-ons in C++ Review for Midterm #1

Spring CS Homework 12 p. 1. CS Homework 12

Review of Important Topics in CS1600. Functions Arrays C-strings

The following program computes a Calculus value, the "trapezoidal approximation of

CSCE 110 PROGRAMMING FUNDAMENTALS

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

CS2141 Software Development using C/C++ Compiling a C++ Program

Lecture 23: Pointer Arithmetic

Exam 3 Chapters 7 & 9

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

pointers + memory double x; string a; int x; main overhead int y; main overhead

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

CS 610: Intermediate Programming: C/C++ Making Programs General An Introduction to Linked Lists

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Functions, Arrays & Structs

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

Comp151. Generic Programming: Container Classes

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Name SECTION: 12:45 2:20. True or False (12 Points)

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Lab 2.1: Fixing a C++ program

Getting Started with Visual Studio

True or False (15 Points)

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Assignment 4: SkipList

Transcription:

Source File: ~/2315/38/lab38.(C CPP cpp c++ cc cxx cp) Input: under control of main function Output: under control of main function Value: 3 A bucket sort begins with a one-dimensional array of positive integers to be sorted and a twodimensional array of integers with rows subscripted from 0 to 9 and columns subscripted from 0 to n 1, where n is the number of values in the array to be sorted. Each row of the two-dimensional array is referred to as a bucket. Write a function bucketsort that takes as arguments a vector of unsigned integers and the maximum number of digits of a vector element and performs as follows: a) Place each value of the one-dimensional array into a row of the bucket array based on the value s ones digit. For example, 97 is placed in row 7, 3 is placed in row 3 and 100 is placed in row 0. This is called a distribution pass. b) Loop through the bucket array row by row, and copy the values back to the original array. This is called a gathering pass. The new order of the preceding values in the one-dimensional array is 100, 3, and 97. c) Repeat this process for each subsequent digit position (tens, hundreds, thousands, etc.). On the second pass, 100 is placed in row 0, 3 is placed in row 0 (because 3 has no tens digit) and 97 is placed in row 9. After the gathering pass, the order of the values in the one-dimensional array is 100, 3, and 97. After the last gathering pass, the original array is now in sorted order. The contents of the one-dimensional vector should be printed to the standard output device at the conclusion of each gathering pass. Implement the buckets as either a matrix or a vector of vectors. A sample main function for testing this function is shown in Figure 1. A sample execution sequence is shown in Figure 2. A second main function, similar to the one used for testing the other sort functions, is shown in Figure 3. To use this function, modify your sort function to eliminate the printing of the vector after each of the gathering passes. The execution sequence for this second main function is shown in Figure 4. To use the Makefile as distributed in class, add a target of lab38 to targets2srcfiles. 1 #include <iostream> 2 #include <cstdlib> // contains prototypes for functions srand and rand 3 #include <vector> 4 #include <cmath> 5 6 using namespace std; 7 8 ostream& operator<<(ostream& os, const vector<uint>& v); 9 10 void bucketsort(vector<uint>& v, uint numdigits); 11 12 int main() 13 { 14 uint numdigits, n, shiftvalue, scalingfactor, i; 15 vector<uint> v; 16 Figure 1. /usr/local/2315/src/lab38main.c (Part 1 of 2) CS 2315 Data Structures and Algorithms Page 1

17 // randomize random number generator using current time 18 srand(time(0)); 19 20 cout << "Enter the number of digits in each of the values to be sorted:" 21 << endl; 22 cin >> numdigits; 23 24 cout << "Enter the number of values to be sorted:" << endl; 25 cin >> n; 26 27 shiftvalue = uint(pow(10.0, int(numdigits - 1))); 28 scalingfactor = uint(pow(10.0, int(numdigits))) - 1 - shiftvalue; 29 30 for (i = 0; i < n; ++i) 31 v.push_back(shiftvalue + rand() % scalingfactor); 32 33 cout << v << endl; 34 bucketsort(v, numdigits); 35 36 return 0; 37 } 38 39 ostream& operator<<(ostream& os, const vector<uint>& v) 40 { 41 vector<uint>::const_iterator itr; 42 43 os << "vector" << endl << { << endl; 44 for (itr = v.begin(); itr < v.end(); ++itr) 45 os << " [" << itr - v.begin() << "] = " << *itr << endl; 46 os << } << endl; 47 48 return os; 49 } Figure 1. /usr/local/2315/src/lab38main.c (Part 2 of 2) Page 2 CS 2315 Data Structures and Algorithms

1 newuser@csunix ~> cd 2315 2 newuser@csunix ~/2315>./getlab.ksh 38 3 * Checking to see if a folder exists for Lab 38...No 4 * Creating a folder for Lab 38 5 * Checking to see if Lab 38 has sample input and output files...no 6 * Checking to see if /usr/local/2315/src/lab38main.c exists...yes 7 * Copying file /usr/local/2315/src/lab38main.c to folder./38 8 * Checking to see if /usr/local/2315/include/lab38.h exists...no 9 * Copying file /usr/local/2315/src/makefile to folder./38 10 * Adding a target of lab38 to targets2srcfiles 11 * Touching file./38/lab38.cpp 12 * Edit file./38/lab38.cpp in Notepad++ 13 newuser@csunix ~/2315> cd 38 14 newuser@csunix ~/2315/38> ls 15 Makefile lab38.cpp lab38main.c lab38main.c.test 16 newuser@csunix ~/2315/38> make lab38 17 g++ -g -Wall -std=c++11 -c lab38main.c -I/usr/local/2315/include -I. 18 g++ -g -Wall -std=c++11 -c lab38.cpp -I/usr/local/2315/include -I. 19 g++ -o lab38 lab38main.o lab38.o -L/usr/local/2315/lib -lm -lbits 20 newuser@csunix ~/2315/38>./lab38 21 Enter the number of digits in each of the values to be sorted: 22 3 23 Enter the number of values to be sorted: 24 10 25 vector 26 { 27 [0] = 331 28 [1] = 294 29 [2] = 125 30 [3] = 664 31 [4] = 556 32 [5] = 220 33 [6] = 745 34 [7] = 470 35 [8] = 469 36 [9] = 449 37 } 39 vector 40 { 41 [0] = 220 42 [1] = 470 43 [2] = 331 44 [3] = 294 45 [4] = 664 46 [5] = 125 47 [6] = 745 48 [7] = 556 49 [8] = 469 50 [9] = 449 51 } 53 vector 54 { 55 [0] = 220 56 [1] = 125 57 [2] = 331 58 [3] = 745 59 [4] = 449 60 [5] = 556 61 [6] = 664 62 [7] = 469 63 [8] = 470 64 [9] = 294 65 } 67 vector 68 { 69 [0] = 125 70 [1] = 220 71 [2] = 294 72 [3] = 331 73 [4] = 449 74 [5] = 469 75 [6] = 470 76 [7] = 556 77 [8] = 664 78 [9] = 745 79 } 38 52 66 80 Figure 2. Commands to Compile, Link, & Run Lab 38 (Part 1 of 2) CS 2315 Data Structures and Algorithms Page 3

81 newuser@csunix ~/2315/38>./lab38 82 Enter the number of digits in each of the values to be sorted: 83 4 84 Enter the number of values to be sorted: 85 10 86 vector 87 { 88 [0] = 7265 89 [1] = 3039 90 [2] = 5678 91 [3] = 3581 92 [4] = 3955 93 [5] = 9075 94 [6] = 5897 95 [7] = 2043 96 [8] = 6465 97 [9] = 2703 98 } 100 vector 101 { 102 [0] = 3581 103 [1] = 2043 104 [2] = 2703 105 [3] = 7265 106 [4] = 3955 107 [5] = 9075 108 [6] = 6465 109 [7] = 5897 110 [8] = 5678 111 [9] = 3039 112 } 114 vector 115 { 116 [0] = 2703 117 [1] = 3039 118 [2] = 2043 119 [3] = 3955 120 [4] = 7265 121 [5] = 6465 122 [6] = 9075 123 [7] = 5678 124 [8] = 3581 125 [9] = 5897 126 } 128 vector 129 { 130 [0] = 3039 131 [1] = 2043 132 [2] = 9075 133 [3] = 7265 134 [4] = 6465 135 [5] = 3581 136 [6] = 5678 137 [7] = 2703 138 [8] = 5897 139 [9] = 3955 140 } 142 vector 143 { 144 [0] = 2043 145 [1] = 2703 146 [2] = 3039 147 [3] = 3581 148 [4] = 3955 149 [5] = 5678 150 [6] = 5897 151 [7] = 6465 152 [8] = 7265 153 [9] = 9075 154 } 99 113 127 141 155 156 newuser@csunix ~/2315/38> Figure 2. Commands to Compile, Link, & Run Lab 38 (Part 2 of 2) Page 4 CS 2315 Data Structures and Algorithms

1 #include <cmath> 2 #include <cstdlib> 3 #include <iostream> 4 #include <vector> 5 #include <algorithm> 6 #include <chrono> 7 #include <d_random.h> 8 9 using namespace std; 10 11 void bucketsort(vector<uint>& v, uint numdigits); 12 13 const int N = 100000; 14 enum TEST_TYPE {RANDOM, ASCENDING, DESCENDING}; 15 16 int main() 17 { 18 vector<uint> v, w, x; 19 randomnumber rnd; 20 TEST_TYPE testtype; 21 int i; 22 23 for (testtype = RANDOM; 24 testtype <= DESCENDING; 25 testtype = static_cast<test_type>(testtype + 1)) 26 { 27 if (!v.empty()) 28 v.clear(); 29 switch (testtype) 30 { 31 case RANDOM: 32 for (i = 0; i < N; ++i) 33 v.push_back(rnd.random(1000000)); 34 cout << "Random Data:" << endl; 35 break; 36 case ASCENDING: 37 for (i = 0; i < N; ++i) 38 v.push_back(i); 39 cout << "Ascending Data:" << endl; 40 break; 41 case DESCENDING: 42 for (i = 0; i < N; ++i) 43 v.push_back(n - i); 44 cout << "Descending Data:" << endl; 45 break; 46 } Figure 3. /usr/local/2315/src/lab38main.c.test (Part 1 of 2) CS 2315 Data Structures and Algorithms Page 5

47 x = v; 48 sort(x.begin(), x.end()); 49 50 w = v; 51 auto start = chrono::system_clock::now(); 52 bucketsort(w, 10); 53 auto stop = chrono::system_clock::now(); 54 cout << "Bucket Sort: " 55 << chrono::duration_cast<chrono::milliseconds>(stop-start).count() 56 << "ms" << endl; 57 if (x!= w) 58 cout << "Sort didn t work correctly" << endl; 59 cout << endl; 60 } 61 return EXIT_SUCCESS; 62 } Figure 3. /usr/local/2315/src/lab38main.c.test (Part 2 of 2) 1 newuser@csunix ~/2315/38> mv lab38main.c.test lab38main.c 2 newuser@csunix ~/2315/38> # Edit lab38.cpp to eliminate the printing of the vector 3 newuser@csunix ~/2315/38> # after each of the gathering passes 4 newuser@csunix ~/2315/38> make lab38 5 g++ -g -Wall -std=c++11 -c lab38main.c -I/usr/local/2315/include -I. 6 g++ -g -Wall -std=c++11 -c lab38.cpp -I/usr/local/2315/include -I. 7 g++ -o lab38 lab38main.o lab38.o -L/usr/local/2315/lib -lm -lbits 8 newuser@csunix ~/2315/38>./lab38 9 Random Data: 10 Bucket Sort: 40ms 11 12 Ascending Data: 13 Bucket Sort: 37ms 14 15 Descending Data: 16 Bucket Sort: 41ms 17 18 newuser@csunix ~/2315/38> Figure 4. Commands to Compile, Link, & Run Lab 38 Page 6 CS 2315 Data Structures and Algorithms