Question
I need help writing this code in C++
Proj11.cpp is provided as well as the randomdata.txt

thank you in advance!
Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL
For the second part of this assignment, you will implement the following functionalities: Create a vecInt, which will be std:
Back proj11.cpp #include <iostream> #include #include #include <fstream> <vector> vectorRecursion.h int main // Vector crea
Back RandomData.txt 凹 144 23 87 108 20 210 94 39 136 177 25 86 49 87 1;1 34 177 23 38 180 136 115 50 42 38 0 122 120 30 117 1
Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will work with STL std: :vectors. You are free to implement an y of the standard-provided functionalities with the libraries. For the first part of this you assignment, you will create three recursive functions: a) The vector resort Function (Recursive Sort) Parameters List Takes in a std: : vector by-Reference. Note that a std: :vector as part of the STL is a templated construct, hence the function will itself also need to be templated. Hint This is the base parameter requirement. Any parameters necessary for the function will depend on your specific implementation. Functionality: The function will have to perform Recursive Sorting of the vector elements. You may implement whichever sorting variant you wish (from the ones introduced in the Lab sections or any other established variant). Note: One of the most powerful naturally-recursive sorting Output: Nothing, since the std: :vector is passed by reference it should be sorted at the return of the vector resort function. b) The vector research Function Recursive Binary Search) Parameters List: Takes in a std: :vector by-Reference, which has to be already sorted. Note that a std: :vector as part of the STL is a templated construct, hence the function will itself also need to be templated. The function also takes in a const T& value, which is the one that is searched. Hint: These are the base parameter requirements, will depend on your specific implementation. Any other parameters necessary for the function Functionality: The function will have to perform Recursive Binary Search (ht for the provided value in the std: :vector. A Binary Search is performed as follows: Pick the value in the middle of the container (the pivot); if the search item is less than the pivot narrow the search to the bottom half of the container, otherwise search the top half of the container. This is done recursively until the item is found and the index (since std: :vectors can have index-based access) where it is found is returned. (Return -1 if the item is not found). inary search algorithm) as introduced during the class Lectures, Output: The index (since std: :vectors can have index-based access) where the value is found is returned. (Return -1 if the item is not found). Extra: You may also devise an implementation that returns std: :vector::iterator to the element in question (Return std: :vector: :end ) if the item is not found, not NULL -iterators are not generally pointers and should not be treated as such)
For the second part of this assignment, you will implement the following functionalities: Create a vecInt, which will be std: :vector of ints, and fill it with 100 random int numbers which will be read from the provided file RandomData.txt. User input is not mandatory, and hard-coding the file name is allowed for this Project. (Note: If you wish to test with your own randomly ordered numbers, you may use std: :rand (http://en.cppreference.com/w/cpp/numeric/random/rand) to do this). Create a vecIntCpy, which will be another std: :vector of ints, which should be a copy of the previously created one. Apply vector resort and vector research on vecInt. Print out (only to terminal) the resulting vecInt. Extras (not required for 100pt grade): Try std: :sort and std: :binary search http://en.cppreference.com/w/cpp the vecIntcpy container. Time the difference between your implementation and the STL to perform the same tasks on one; you may perform high-resolution time-difference counting with (http://en.cppreference.com/w/cpp/chrono/high resolution clock/now) std: :chrono: :time pointcatd: :chrono: :aystem clock> tiatd::chrono::system clock::now ) //tasks to time... std: :chrono: :time point t2 std: :chrono: :system clock: now) std::chrono: :duration double> difft2-tl std: :cout
0 0
Add a comment Improve this question Transcribed image text
Answer #1


Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)

//main.cpp

#include <iostream>
#include <fstream>

#include <vector>

#include "VectorRecursion.h"


int main(){

    std::vector<int> vecInt;

    std::ifstream fin("/Users/swapnil/CLionProjects/VectorRecursion/RandomData.txt");
    while(fin){
        int fin_in;
        fin >> fin_in;
        if (!fin)
            break;
        vecInt.push_back( fin_in );
    }

    for (size_t i=0; i<vecInt.size(); ++i){
        std::cout << vecInt[i] << " same as " << vecInt.at(i) << std::endl;
    }

    for (std::vector<int>::iterator it=vecInt.begin(); it!=vecInt.end(); ++it){
        std::cout << *it << std::endl;
    }

    return 0;
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//VectorRecursion.h

#ifndef VECTORRECURSION_VECTORRECURSION_H
#define VECTORRECURSION_VECTORRECURSION_H

#include <iostream>
#include <vector>

using namespace std;

template <typename T>
void swap(vector<T> &v, T m, T n) {
    T temp = v[m];
    v[m] = v[n];
    v[n] = temp;
}

template <typename T>
T divide(vector<T> &v, T l, T h) {
    T p = v[h];
    T r = l - 1;
    T s = l;
    while(s <= h - 1) {
        if(v[s] <= p) {
            ++r;
            swap(v, r, s);
        }
        ++s;
    }
    swap(v, r + 1, h);
    return (r + 1);
}

template <typename T>
void vector_resort(vector<T> &v, T l, T h) {
    if(l < h) {
        T p = divide(v, l, h);
        vector_resort(v, l, p - 1);
        vector_resort(v, p + 1, h);
    }
}

template <typename T>
T vector_research(vector<T> &v, T l, T h, const T theory) {
    if(l > h) {
        return -1;
    }
    T m = (l + h) / 2;
    if(v[m] == theory) {
        return m;
    }
    if(v[m] > theory) {
        return vector_research(v, l, m - 1, theory);
    }
    return vector_research(v, m + 1, h, theory);
}
#endif //VECTORRECURSION_VECTORRECURSION_H



Recursion] - ...main.cpp Project ▼ At ut..dk»奂:S main.cpP Rune /Users/swapnil/CLionProjects/VectorRecursion/cmake-build-debug Recursion] - ...main.cpp main.cpp *증 *-ㅿCMakeLists.txt 분.main.cpp aRandomData txt VectorRecursionh Project ▼ Run: VectorRecur

Add a comment
Know the answer?
Add Answer to:
I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • May i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #...

    may i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #include<iostream> #include<string> #include<sstream> #include<stack> using namespace std; int main() { string inputStr; stack <int> numberStack; cout<<"Enter your expression::"; getline(cin,inputStr); int len=inputStr.length(); stringstream inputStream(inputStr); string word; int val,num1,num2; while (inputStream >> word) { //cout << word << endl; if(word[0] != '+'&& word[0] != '-' && word[0] != '*') { val=stoi(word); numberStack.push(val); // cout<<"Val:"<<val<<endl; } else if(word[0]=='+') { num1=numberStack.top(); numberStack.pop(); num2=numberStack.top(); numberStack.pop();...

  • Can someone please help me with this code? I'm writing in C++. Thank you in advance....

    Can someone please help me with this code? I'm writing in C++. Thank you in advance. Complete a program that represents a Magic Eight Ball (a Magic Eight Ball allows you to ask questions and receive one of several random answers). In order to complete this, you will need a couple of new functions. First, in order to get a line of input that can contain spaces, you cannot use cin, but instead will use getline: string question; cout <<...

  • Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...

    Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...

  • In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits&gt...

    In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits> #include <iostream> #include <string> // atoi #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ const unsigned int DEFAULT_SIZE = 179; // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() {...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • C++. Need some help getting started. We will also have the following two functions: 1. A...

    C++. Need some help getting started. We will also have the following two functions: 1. A mutate function that randomly modifies a chromosome. 2. A crossover function that takes two chromosomes and splits each one at the same spot, then combines them together. Our genetic algorithm works by iterating over generations of chromosomes via the following process: 1. Generate random population. 2. Until we get an answer that is good enough, do the next steps in a loop: (a) Do...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • For this computer assignment, you are to write a C++ program to implement a class for...

    For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...

  • //Need help ASAP in c++ please. Appreciate it! Thank you!! //This is the current code I have. #include <iostream> #include <sstream> #include <iomanip> using namespace std; cl...

    //Need help ASAP in c++ please. Appreciate it! Thank you!! //This is the current code I have. #include <iostream> #include <sstream> #include <iomanip> using namespace std; class googlePlayApp { private: string name; double rating; int numInstalls; int numReviews; double price;    public: googlePlayApp(string, double, int, int, double); ~ googlePlayApp(); googlePlayApp();    string getName(); double getRating(); int getNumInstalls(); int getNumReviews(); string getPrice();    void setName(string); void setRating(double); void setNumInstalls(int); void setNumReviews(int); void setPrice(double); }; googlePlayApp::googlePlayApp(string n, double r, int ni, int nr, double pr)...

  • Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART...

    Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART A PART B Assignment 1 #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <stdio.h> #include <ctype.h> #include <string.h> #include <algorithm> using namespace std; /** This structure is to store the date and it has three integer fields **/ struct Date{    int day;    int month;    int year; }; /** This structure is to store the size of the box and it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT