In: Computer Science
One can find out date of the month when someone was born by asking five questions. Each question asks whether the day is in one of the five sets of numbers below
1 3 5 7 2 3 6 7 4 5 6 7 8 9 10 11 16 17 18 19
9 11 13 15 10 11 14 15 12 13 14 15 12 13 14 15 20 21 22 23
17 19 21 23 18 19 22 23 20 21 22 23 24 25 26 27 24 25 26 27
25 27 29 31 26 27 30 31 28 29 30 31 28 29 30 31 28 29 30 31
Set1 Set 2 Set 3 Set 4 Set 5
The birthday is the sum of the first numbers in the set where the day appears. For example, if the birthday is 19, it appears in Set 1, Set 2, and Set 5.
The first numbers in these three sets are 1, 2, and 16 whose sum is 19.
Write a C++ program using arrays that prompts the user to answer whether the day is in Sets 1‐5. If the number is in the particular set, the program adds the first number in the sets to calculate the day of the month. An array must be used in the problem. Refer to the sample output below.
Sample Run:
Is your birthday in Set 1?
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31
Enter 0 for No and 1 for Yes: 1
Is your birthday in Set 2?
2 3 6 7
10 11 14 15
18 19 22 23
26 27 30 31
Enter 0 for No and 1 for Yes: 1
Is your birthday in Set 3?
4 5 6 7
12 13 14 15
20 21 22 23
28 29 30 31
Enter 0 for No and 1 for Yes: 1
Is your birthday in Set 4?
8 9 10 11
12 13 14 15
24 25 26 27
28 29 30 31
Enter 0 for No and 1 for Yes: 0
Is your birthday in Set 5?
16 17 18 19
20 21 22 23
24 25 26 27
28 29 30 31
Enter 0 for No and 1 for Yes: 0
Your birthday is on day: 7
//C++ program using arrays
#include<iostream>
using namespace std;
const int n = 4; // n is the dimension of all 2D
array
int sum = 0;
// function for printing all set values
void print_set(int arr[n][n]){
int i,j;
// static variable is used counting set number
static int x = 1;
cout<<"Is your birthday in
Set"<<x++<<"?"<<endl;
// printing set values of sets
for(i=0;i<n;i++){
for(j=0;j<n;j++)
cout<<arr[i][j]<<" ";
cout<<endl;
}
int no;
cout<<"Enter 0 for No and 1 for Yes: ";
cin>>no;
// if Birthday is present in the set then adds the
first number sets to calculate the day of the month
if(no==1) sum = sum+arr[0][0];
cout<<endl;
}
int main(){
int set1[n][n] =
{{1,3,5,7},{9,11,13,15},{17,19,21,23},{25,27,29,31}};
// set1
int set2[n][n] =
{{2,3,6,7},{10,11,14,15},{18,19,22,23},{26,27,30,31}};
// set2
int set3[n][n] =
{{4,5,6,7},{12,13,14,15},{20,21,22,23},{28,29,30,31}}; //
set3
int set4[n][n] =
{{8,9,10,11},{12,13,14,15},{24,25,26,27},{28,29,30,31}}; //
set4
int set5[n][n] =
{{16,17,18,19},{20,21,22,23},{24,25,26,27},{28,29,30,31}}; //
set5
print_set(set1); // calling print_set()
function for set1
print_set(set2); //
calling print_set() function for set2
print_set(set3); //
calling print_set() function for set3
print_set(set4); //
calling print_set() function for set4
print_set(set5); // calling print_set()
function for set5
cout<<"Your birthday is on day:
"<<sum<<endl;
return 0;
}