Question
C++
5. (6 points) Write a function split_vector that receives a source vector of Date (from Project 2) values and a target Date v
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Copy below code in online compiler

"https://www.onlinegdb.com/online_c++_compiler "

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

#include <string>
#include <vector>
#include <iostream>
#include <cstring>

using namespace std;

int comp_dates(string s1, string s2)
{
int cnt1, cnt2;
cnt2 = 0;
string d1,m1,y1,d2,m2,y2;
for(int i=0;i<s1.size();i++){
if(s1[i]=='/' && cnt2==0){
cnt1 = i;
++cnt2;
}
else if(s1[i]=='/'){
cnt2 = i;
}
}
m1 = s1.substr(0, cnt1);
d1 = s1.substr(cnt1+1, cnt2-cnt1-1);
y1 = s1.substr(cnt2+1,s1.size()-1);

cnt2=0;
for(int j=0;j<s2.size();j++){
if(s2[j]=='/' && cnt2==0){
cnt1 = j;
++cnt2;
}
else if(s2[j]=='/'){
cnt2 = j;
}
}
m2 = s2.substr(0, cnt1);
d2 = s2.substr(cnt1+1, cnt2-cnt1-1);
y2 = s2.substr(cnt2+1,s1.size()-1);
if(y2>y1){
return 1;
}
else if(y1==y2 && m2>m1){
return 1;
}
else if(y1==y2 && m2==m1 && d2>d1){
return 1;
}
else{
return 0;
}

}

int main()
{
vector <string> hold;
vector <string> first_vec;
vector <string> sec_vec;
string trgt,in,temp;
int n,comp;
cout<<"enter target date in the form mm/dd/yyyy"<<endl;
cin>>trgt;
cout<<"enter number of dates in source vector"<<endl;
cin>>n;
for(int k=0;k<n;k++){
cout<<"enter source vector date number "<<k+1<<endl;
cin>>in;
hold.push_back(in);
comp = comp_dates(in, trgt);
if(comp==1){
sec_vec.push_back(in);
}
else{
first_vec.push_back(in);
}
  
}
cout << "Source vector of dates is ";
for (int p = 0; p < hold.size(); p++)
{
temp = hold[p];
cout << "\t" << temp;
}
cout <<endl<< "First vector of dates that is before target date is ";
for (int q = 0; q < first_vec.size(); q++)
{
temp = hold[q];
cout << "\t"<< temp ;
}
cout << endl<< "Second vector of dates that is after target date is ";
for (int r = 0; r < sec_vec.size(); r++)
{
temp = hold[r];
cout << "\t" << temp ;
}
return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ 5. (6 points) Write a function split_vector that receives a source vector of Date (from...
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
  • Write a C function named date() that receives an integer number of the long date, date...

    Write a C function named date() that receives an integer number of the long date, date form is yyyymmdd, such as 20070412; and the addresses of three variables named month, day, and year. determines the corresponding month, day, and year; and returns these three values to the calling function. For example, if date() is called using the statement     longdate=20200411; date(longdate, &month, &day, &year); the number 4 should be returned in month, the number 11 in day, and the number...

  • Write a function template that receives a priority queue and an output stream as parameters. Lab 48 Due Date: See Blackboard Source File: /2336/48/1ab48.cpp Input: Output:under control of main functio...

    Write a function template that receives a priority queue and an output stream as parameters. Lab 48 Due Date: See Blackboard Source File: /2336/48/1ab48.cpp Input: Output:under control of main function Waol under control of main function Write a function template that receives a priority queue and an output stream as parameters. The function determines the distribution of the elements in the priority queue; that is, the function counts the number of occurrences of each element. The format of the output...

  • C++ 4. (5 points) Identify the following items in the programming code shown below: a. Function...

    C++ 4. (5 points) Identify the following items in the programming code shown below: a. Function prototype, function heading, function body, and function definitions. b. Function call statements, formal parameters, and actual parameters. C. Value parameters and reference parameters. d. Local variables and global variables. 6. Named constants. #include <iostream> //Line 1 using namespace std; //Line 2 const double NUM = 3.5; //Line 3 int temp; //Line 4 void func(int, doubles, char); //Line 5 int main() int num; double one;...

  • c++ programming please. thx. Define a function template named "merge" to merge two lists of items...

    c++ programming please. thx. Define a function template named "merge" to merge two lists of items into one list including all the items of the first list followed by the items of the second list. Even though, the data type of the items of the lists is a generic type, both input lists include items of the same type. To merge the two lists, the function first dynamically allocates enough memory to accommodate the items of both lists. The allocated...

  • programing C,already write part of code (a) Write a function print.array that receives an array of...

    programing C,already write part of code (a) Write a function print.array that receives an array of real numbers and the number of el stored in the array. This function must display the elements of the array in order with each one on a line by itself in a field width of 7 with two decimal places. (See sample output. Recall the format specifier would be "%7.21f"). (b) Sometimes we want to treat an array as a mathematical vector and compute...

  • Write in C. Write a function that takes an array of integers and its size as...

    Write in C. Write a function that takes an array of integers and its size as parameters and prints the two largest values in the array. In this question, the largest value and the second largest value cannot be the same, even if the largest value occurs multiple times. In the first sample, the two largest values are 9 and 8 (even though the value 9 appears twice). In the second sample, all the values are equal and the program...

  • c++ HW 5b- STL vector 1.) Declare a vectorobjoct named: cities 2.) Push three cities into...

    c++ HW 5b- STL vector 1.) Declare a vectorobjoct named: cities 2.) Push three cities into the vector, First Venice, then Paris, and finally Dublin Noto: Functions are not required for this assignment (but you can use them it you want). 3.) Declare an iterator to iterate through the cities vector, and name it: it 4.) Declare a reverse iterator to iterato through the citios vector in reverse direction (back to front) 5.) First use a for loop to display...

  • Write a C program (not C++) that calls a function to multiply a matrix (two dimensional...

    Write a C program (not C++) that calls a function to multiply a matrix (two dimensional array). The main program should ask the user to enter an integer that will be used as the matrix “adder” and then call the function. The function should perform a matrix increment (every element of the array is incremented by the same value) and assign the result to another array. The function should be flexible enough to work with any array size and “adder”,...

  • Using C++ to write .cpp and .h file. Main function and sample output are given. The...

    Using C++ to write .cpp and .h file. Main function and sample output are given. The task is to write a vector class for dynamic allocation.declare the the class named vector with the required attributes. The task are defined in the main function. #include <iostream> #include "vector.h". #define LOG(x,y) std::cout << x << y « std::endl; #define INFO(x) std::cout << "[INFO]: #define WARNING(x) std::cout <« "[WARNING]: "<< x <« std: :endl; << x << std::endl; " « x < std::endl;...

  • PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Hint:...

    PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Hint: the number array in task 2 is work for task 3 in figure 1.   Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count = 0; for (int i...

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