I'm having troubles with a coding problem. Yes, I need to write a C++ program which sorts dates. I've tried several methods. Using 2D arrays and now parallel arrays.
The problem I have with my parallel array to keeping tract of those which haven't been sorted yet. That is the YYYY
has been sorted. I need to keep track of the MM
. Then DD
.
Please make some suggestions as to how I should approach this. Or did I approach this question entirely in the wrong way?
#include <iostream>
#include <string>
using namespace std;
/*
Write a program in C++ to ask the user to enter dates in dd/mm/yyyy format.
The number of dates that the user can enter should also be specified by the user.
Sort the dates in ascending order using an appropriate sorting algorithm.
Also write a small description as to why you have chosen this particular sorting technique.
Example output screen:
Enter no of dates to enter: 5
Enter the first date: 12/05/1945
Enter the second date: 01/01/2001
Enter the third date: 05/12/1999
Enter the fourth date: 11/03/1972
Enter the fifth date: 15/12/1999
After sorting the dates are:
12/05/1945
11/03/1972
05/12/1999
15/12/1999
01/01/2001
*/
string ConvertToWord(unsigned int input) {
string output = "";
int ones_place = input % 10;
int tens_place = (input - ones_place) % 100;
if (input >= 10 && input < 20) {
switch (ones_place) {
case 0:
output += "Tenth";
break;
case 1:
output += "Eleventh";
break;
case 2:
output += "Twelfth";
break;
case 3:
output += "Thirteenth";
break;
case 4:
output += "Fourteenth";
break;
case 5:
output += "Fiftheenth";
break;
case 6:
output += "Sixteenth";
break;
case 7:
output += "Seventeeth";
break;
case 8:
output += "Eighteeth";
break;
case 9:
output += "Nineteeth";
break;
}
}
else if ((input >= 20 && input < 99) || (input > 0 && input < 20)) {
switch (tens_place) {
case 20:
output += "Twenty";
break;
case 30:
output += "Thirty";
break;
case 40:
output += "Forty";
break;
case 50:
output += "Fifty";
break;
case 60:
output += "Sixty";
break;
case 70:
output += "Seventy";
break;
case 80:
output += "Eighty";
break;
case 90:
output += "Ninety";
break;
}
if (tens_place > 0 && ones_place > 0) {
output += "-";
}
switch (ones_place) {
case 1:
output += "First";
break;
case 2:
output += "Second";
break;
case 3:
output += "Third";
break;
case 4:
output += "Fourth";
break;
case 5:
output += "Fifth";
break;
case 6:
output += "Sixth";
break;
case 7:
output += "Seventh";
break;
case 8:
output += "Eighth";
break;
case 9:
output += "Nineth";
break;
case 10:
output += "Twentth";
break;
}
}
return output;
}
int* ToArray(string word) {
word += "/";
int* result = new int[3];
string current_word = "";
int num_words = 0;
for (int i = 0; i < word.length(); i++) {
char c = word.at(i);
if (c != '/') {
current_word += c;
}
else {
result[num_words] = stoi(current_word);
current_word = "";
num_words++;
}
}
return result;
}
void ToArrays(int* days, int* months, int* years, int index, string input) {
input += "/";
string current_input = "";
int num_input = 0;
for (int i = 0; i < input.length(); i++) {
char c = input.at(i);
if (c != '/') {
current_input += c;
}
else {
if (num_input == 0) {
days[index-1] = stoi(current_input);
}
else if (num_input == 1) {
months[index-1] = stoi(current_input);
}
else if (num_input == 2) {
years[index-1] = stoi(current_input);
}
current_input = "";
num_input++;
}
}
}
int** AddDatesInto2DArray(int** result, int _num_dates, string word, int index) {
int* sorted_date = ToArray(word);
for (int i = 0; i < 3; i++) {
result[index-1][i] = sorted_date[i];
}
return result;
}
void InsertionSort(int* array, int length) {
int j, temp;
for (int i = 1; i < length; i++) {
j = i;
while (j > 0 && array[j] < array[j - 1]) {
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
j--;
}
}
}
void InsertionSort(int* sorting, int* a, int* b, int length) {
bool all_sorted = true;
int* unsorted_index = new int[length];
int* unsorted_sorting = new int[length];
int* unsorted_a = new int[length];
int* unsorted_b = new int[length];
int j, temp, temp_a, temp_b;
for (int i = 1; i < length; i++) {
j = i;
if (sorting[i] == sorting[j-1]) {
}
while (j > 0 && sorting[j] < sorting[j - 1]) {
temp_a = a[j];
a[j] = a[j - 1];
a[j - 1] = temp_a;
temp_b = b[j];
b[j] = b[j - 1];
b[j - 1] = temp_b;
temp = sorting[j];
sorting[j] = sorting[j - 1];
sorting[j - 1] = temp;
j--;
}
}
}
int main() {
cout << "Enter the number of dates to enter [Three Digit] : ";
int num_dates = 0;
cin >> num_dates;
int* int_days = new int[num_dates];
int* int_months = new int[num_dates];
int* int_years = new int[num_dates];
for (int i = 1; i < num_dates+1; i++) {
string num_word = ConvertToWord(i);
cout << endl;
cout << "Enter the " << num_word << " date : ";
string date_input = "";
cin >> date_input;
ToArrays(int_days, int_months, int_years, i, date_input);
}
InsertionSort(int_years, int_days, int_months, num_dates);
for (int i = 0; i < num_dates; i++) {
cout << int_days[i] << "/" << int_months[i] << "/" << int_years[i] << endl;
}
int i_temp = 0;
cin >> i_temp;
return (0);
}
n
in the last three teens. \$\endgroup\$struct date
and use that. Also, remove all those plain arrays and use vectors. Your code leaks like there's no tomorrow. \$\endgroup\$ones_place
will never be 10; the word"Twentth"
won't be generated. It isn't clear that you handle zero; it may not matter for dates. Your naming code doesn't handle negatives or numbers larger than 99 fully. Have you heard of 'arrays'? Are you sure you can't use some in your number-to-name code? \$\endgroup\$