Looking for program in C++ coding: Sample birthday files: bday1.txt ------------
ID: 3802404 • Letter: L
Question
Looking for program in C++ coding:
Sample birthday files:
bday1.txt
-------------------------------------------------------------------------------------------------------------------------------------------
bday2.txt
Problem A: Birthdays again. (20 points) Write a program that opens a file of the users choice that contains a list of birthdays. Extract from this file two things: (1) the date with the most common birthday (all of them) and (2) the month with the most people born. We will not test for a tie in either of these statistics. The file tested will always be in the format: mm/dd/yyyy Example 1 (user input is underlined): Enter file name: 1.txt. bday Most common birthday Most common birthday month Example 2 (user input is underlined): Enter file name: bday 2.txt Most common birthday 8/15 Most common birthday monthExplanation / Answer
PROGRAM CODE:
/*
* CommonBirthdays.cpp
*
* Created on: 22-Mar-2017
* Author: kasturi
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
using namespace std;
//Main program which would read the file and find the common birthday and month
int main()
{
string filename;
int date[32];
int month[13];
for(int i=1; i<32; i++)
{
date[i] = 0;
}
for(int i=1; i<13; i++)
{
month[i] = 0;
}
cout<<"Enter file name: ";
cin>>filename;
ifstream in(filename);
string line;
while(getline(in, line))
{
int f_date, f_month;
f_month = stoi(line.substr(0, line.find("/")));
line = line.substr(line.find("/")+1);
f_date = stoi(line.substr(0, line.find("/")));
date[f_date]++;
month[f_month]++;
}
in.close();
cout<<"Most common birthday: ";
int d_max = date[1], m_max = month[1], index_date = 1, index_month = 1;
for(int i=1; i<32; i++)
{
if(d_max < date[i])
{
d_max = date[i];
index_date = i;
}
}
for(int i=1; i<13; i++)
{
if(m_max < month[i])
{
m_max = month[i];
index_month = i;
}
}
cout<<index_month<<"/"<<index_date<<endl;
cout<<"Most common birthday month: ";
cout<<index_month;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.