Question

In: Computer Science

C++ Write a function called gen_dates() that generates random dates. It takes two arrays of integers...

C++ Write a function called gen_dates() that generates random dates. It takes two arrays of integers called months and days to store the month and day of each date generated, a constant array of 12 integers called num_of_days that specify the number of days of each of the 12 months and an integer called size that specifies how many dates to generate and randomly generates size dates, storing the generated months in months array and generated days in days array. The function must generate only valid dates. To do that, it sets the upper limit of the days to generate to the values given in num_of_days array that is passed as a parameter. The number of days for the 12 months stored in num_of_days array are as follows: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31.

Write another function called search_date() that takes two arrays of integers called months and days, an integer for size (same as above) and two more integers for the month and day to search for within the two arrays of months and days. The function returns the index of the found date if found and -1 if not found.

In main, declare two arrays of integers of maximum size 50, called months and days, to hold the generated months and days. Also declare an array of integers called num_of_days and initiaize it to the number of days of each of the 12 months. Because February can have either 28 or 29 days, depending on if the year is a leap year or not, the program must ask the user to enter the current year (in main). If it's a leap year, the number of days for month 2 (February) must be set to 29, otherwise, it's set to 28. A year is a leap year if it's either divisible by 400 or if it's divisible by 4, but not by 100.

The program must also ask the user (in main) how many random dates to generate (size). Then, it must pass the months and days arrays, the num_of_days array and size to gen_dates() function and print the generated dates (in main).

Then, read a single date such as 9/21 and pass the two arrays, the size and the month and day to search for to search_date() function. If the specified date was found, print the index of the first date found in main or print in main that the date could not be found.

All user input must be read in main and generated dates printed in main. Do not use any global variables. The two specified functions must receive only specified parameters. The gen_date() function returns nothing and search_date() function returns the index of found date or -1. ?

Solutions

Expert Solution

Thanks for the question.


Here is the completed code for this problem.

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution

, please rate the answer. Thanks


===========================================================================

#include<iostream>
#include<cstdlib>
using namespace std;


void gen_dates(int months[], int days[], int num_of_days [12], int size){
  
   int month;
   for(int i=0; i<size; i++){
       month = 1+rand()%12; // generate month
       months[i] = month;
       days[i] = 1+ rand()%num_of_days[month-1];  
   }
}

int search_date(int months[], int days[], int size, int month, int day){
  
   for(int i=0; i<size;i++){
      
       if(months[i]==month && days[i]==day)return i;
   }
   return -1;
}

int main(){
  
   int size;
   // Also declare an array of integers called num_of_days
   // and initiaize it to the number of days of each of the 12 months.
   int num_of_days[]={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   // In main, declare two arrays of integers of maximum size 50,
   // called months and days, to hold the generated months and days
   int months[50];
   int days[50];
   int year;
   cout<<"Enter current year: "; cin>>year;
   if((year%4==0 && year%100!=0) || (year%400==0)){
       cout<<year<<" is a leap year."<<endl;
       num_of_days[1]=29;
   }
  
   cout<<"How many random days to generate (<=50)? "; cin>>size;
  
   gen_dates(months,days,num_of_days,size);
  
   // print dates in main()
  
   for(int i=0; i<size;i++){
       cout<<months[i]<<"/"<<days[i]<<endl;
   }
  
   //Then, read a single date such as 9/21
   int search_month, search_day;
   char separator;
   cout<<"Enter month day (format 12/31): ";
   cin>>search_month>>separator>>search_day;
   // print the index of the first date found in main or print in main that the date could not be found.
   int index = search_date(months,days,size,search_month,search_day);
   if(index!=-1){
       cout<<"Date found at index "<<index<<endl;
   }else{
       cout<<"Date not found."<<endl;
   }
  
  
  
}


Related Solutions

C++ write a function called divideBy that takes two integers as its input and returns the...
C++ write a function called divideBy that takes two integers as its input and returns the remainder. If the divisor is 0, the function should return -1, else it should return the remainder to the calling function.
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and...
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and returns the number of consecutive values that match between the two arrays starting at index 0. Suppose the two arrays are {3, 2, 5, 6, 1, 3} and {3, 2, 5, 2, 6, 1, 3} then the function should return 3 since the consecutive matches are for values 3, 2, and 5. in C++ with explanations
Write a function called alternate that takes two positive integers, n and m, as input arguments...
Write a function called alternate that takes two positive integers, n and m, as input arguments (the function does not have to check the format of the input) and returns one matrix as an output argument. Each element of the n-by-m output matrix for which the sum of its indices is even is 1. All other elements are zero. For example, here is an example run: >> alternate(4,5) ans = 1 0 1 0 1 0 1 0 1 0...
Write a C function hwkOneA, that takes a long int x as well as two integers...
Write a C function hwkOneA, that takes a long int x as well as two integers n and m as arguments, and returns a long int. Here is the function declaration: long int hwkOneA (long int x, int n, int m); The function should swap nibble n and m of a long int x (64-bit integer). A nibble is a four-bit aggregation. For this problem, the index of the most significant nibble is 0, and the index of the least...
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
Write a function called randFill that fills the entries of an array with random integers in...
Write a function called randFill that fills the entries of an array with random integers in the range from 10 to 99 (inclusive). (You should use a standard Java method to generate the values. Your solution should use no more than 6 lines of code.) For example, a program that uses the function randFill follows. public class P4 { public static void main(String args[]) { int x[]; x = randFill(5); for (int i = 0; i < 5; i++) System.out.print(x[i]...
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Write a function called is_equal(). This function will take as an argument two integers. Call the...
Write a function called is_equal(). This function will take as an argument two integers. Call the is_equal function and supply two integers. You do not need to get the integers from the keyboard. Compare the two integers. If the two integers are equal, then print "equal". If the two integers are not equal, print "not equal". Call the function from your main code to execute the function. Sample output: equal Sample output: not equal
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT