Write a C++ program to fill a vector with 5000 random numbers. Then display:
There is a file called "Activity 25 Sort functions.zip" under Materials in this week’s module. Download and unzip the shellSort and swapper function. Since you will be using a binary search, the vector will have to be in order.
Each of the preceding values should be determined by a function and returned to main where they should be displayed.
shellShort doc:
void sort(vector<string>& names)
{
bool flag = true;
int i, numLength = names.size();
int d = numLength;
while (flag || (d>1)) // bool flag
{
flag = false; //reset flag to 0 to check for
// future swaps_
d = (d + 1) / 2;
for (i = 0; i < (numLength - d); i++)
{
if (names[i + d] < names[i])
{
swapper(names[i], names[i + d]);
flag = true; //tells swap has occurred
}
}
}
}
Activity 25 Sort functions.zip
void shellSort(vector<int>& V) { bool flag = true; int i, numLength = V.size(); int d = numLength; while (flag || (d>1)) // bool flag { flag = false; //reset flag to 0 to check for // future swaps d = (d + 1) / 2; for (i = 0; i < (numLength - d); i++) { if (V[i + d] < V[i]) { swapper(V[i], V[i + d]); flag = true; //tells swap has occurred } } } } void swapper(int &a, int &b) { int temp; temp = a; a = b; b = temp; }
REQUIREMENTS:
- must use comments
- must be in C++
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
void swapper(int& a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void shellSort(vector<int>& V)
{
bool flag = true;
int i, numLength = V.size();
int d = numLength;
while (flag || (d > 1)) // bool flag
{
flag = false; //reset
flag to 0 to check for
// future swaps
d = (d + 1) / 2;
for (i = 0; i <
(numLength - d); i++) {
if (V[i + d] < V[i]) {
swapper(V[i], V[i + d]);
flag = true; //tells swap has occurred
}
}
}
}
int smallest(vector<int> &vec) {
int num = vec[0];
for(int i = 0; i < vec.size(); ++i) {
if(vec[i] < num) {
num =
vec[i];
}
}
return num;
}
int largest(vector<int> &vec) {
int num = vec[0];
for(int i = 0; i < vec.size(); ++i) {
if(vec[i] > num) {
num =
vec[i];
}
}
return num;
}
int total(vector<int> &vec) {
int sum = 0;
for(int i = 0; i < vec.size(); ++i) {
sum += vec[i];
}
return sum;
}
double average(vector<int> &vec) {
return total(vec) / (double)vec.size();
}
int odd_count(vector<int> &vec) {
int num = 0;
for(int i = 0; i < vec.size(); ++i) {
if(vec[i] % 2 == 1) {
num++;
}
}
return num;
}
int even_count(vector<int> &vec) {
int num = 0;
for(int i = 0; i < vec.size(); ++i) {
if(vec[i] % 2 == 0) {
num++;
}
}
return num;
}
int bin_search(vector<int> &vec, int num) {
int left = 0, right=vec.size()-1;
while(left <= right) {
int mid = (left+right)/2;
if(vec[mid] == num) {
return
mid;
} else if(num < vec[mid])
{
right =
mid-1;
} else {
left =
mid+1;
}
}
return -1;
}
int main() {
srand(time(NULL));
vector<int> vec;
for(int i = 0; i < 5000; ++i) {
vec.push_back(rand()%1000);
}
cout << "Smallest number is " <<
smallest(vec) << endl;
cout << "Largest number is " <<
largest(vec) << endl;
cout << "Number of odd values is " <<
odd_count(vec) << endl;
cout << "Number of even values is " <<
even_count(vec) << endl;
cout << "Total of values is " <<
total(vec) << endl;
cout << "Average of values is " <<
average(vec) << endl;
shellSort(vec);
cout << "Enter a number to search for in vector:
";
int num;
cin >> num;
int ind = bin_search(vec, num);
if(ind == -1) {
cout << num << " is not
found in vector" << endl;
} else {
cout << num << " is
found at index " << ind << endl;
}
return 0;
}
Comment
Write a C++ program to fill a vector with 5000 random numbers. Then display: The smallest...
Must be written in C++ Display results and comments Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions: 1. void selSort (vector <string> & v): sorts the vector using selection sort 2. void display (const vector <string> & v): displays the vector contents 3. int binSearch (const vector <string> & v, string key): searches...
Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number to search (until ^D) and display the position of the number in the sorted vector. Try your program for the following user input: 1 15 18 40 30 50 ^D The output should be: -1 2 -1 7 5 -1 int binary_search(vector<int> v, int from, int to, int value) { if (from > to) return -1; int mid = (from + to) / 2;...
I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...
in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...
How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the linear doesn't. Here's my code. #include <iostream> using namespace std; void selectionSort(int[], int, int& ); void showSelection(int[], int); void sortArray(int[], int, int&); void showArray(const int[], int); int main() { int values[25] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24...
Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...
C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...
Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8, Part 3 * * Author: your name * z-ID: your z-ID * Date: due date of assignment * * This program builds, sorts and prints lists using the quicksort and * merge sort algorithms. */ #include <iostream> #include <iomanip> #include <vector> #include <string> #include "sorts.h" #include "quicksort.h" #include "mergesort.h" using std::cout; using std::fixed; using std::left; using std::setprecision; using std::string; using std::vector; // Data files...
Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel. The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge...
C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...