-2

I need to convert the integer to a string. The integer is a most_occurred_number and I cannot figure out which integer to plug into the code. I have put most_occurred_number as the integer but nothing is changing in the cout. I tried putting month1[i] but it is coming out with an error of an undeclared identifier even though it is in the code.

#include <string>
#include <algorithm>
#include <map>
#include <array>
#include <iomanip>

using namespace std;

int dayofweek(int d, int m, int y)
{
    static int t[] = { 0, 3, 2, 5, 0, 3,
                       5, 1, 4, 6, 2, 4 };
    y -= m < 3;
    return ( y + y / 4 - y / 100 +
             y / 400 + t[m - 1] + d) % 7;
}

std::string to_date_string1(int most_occurred_number1)
{
    switch(most_occurred_number1)
    {
    case 1:
        return "Monday";
    case 2:
        return "Tuesday";
    case 3:
        return "Wednesday";
    case 4:
        return "Thursday";
    case 5:
        return "Friday";
    case 6:
        return "Saturday";
    case 7:
        return "Sunday";
    }
}

std::string to_date_string2(int most_occurred_number2)
{
    switch(most_occurred_number2)
    {
    case 1:
        return "Monday";
    case 2:
        return "Tuesday";
    case 3:
        return "Wednesday";
    case 4:
        return "Thursday";
    case 5:
        return "Friday";
    case 6:
        return "Saturday";
    case 7:
        return "Sunday";
    }
}

void most_occurred_number1(int month1[], int size1)
{
  int max_count1 = 0;
  cout << "\nMost occurred number: ";
  for (int i=0; i<size1; i++)
  {
   int count1=1;
   for (int j=i+1;j<size1;j++)
       if (month1[i]==month1[j])
           count1++;
   if (count1>max_count1)
      max_count1 = count1;
  }

  for (int i=0;i<size1;i++)
  {
   int count1=1;
   for (int j=i+1;j<size1;j++)
       if (month1[i]==month1[j])
           count1++;
   if (count1==max_count1)
       cout << month1[i] << endl;
  }
 }

void most_occurred_number2(int month2[], int size2)
{
  int max_count2 = 0;
  cout << "\nMost occurred number: ";
  for (int i=0; i<size2; i++)
  {
   int count2=1;
   for (int j=i+1;j<size2;j++)
       if (month2[i]==month2[j])
           count2++;
   if (count2>max_count2)
      max_count2 = count2;
  }

  for (int i=0;i<size2;i++)
  {
   int count2=1;
   for (int j=i+1;j<size2;j++)
       if (month2[i]==month2[j])
           count2++;
   if (count2==max_count2)
       cout << month2[i] << endl;
  }
 }

int main()
{
    int day1 = dayofweek(03, 02, 2020); //month 1 days
    int day2 = dayofweek(04, 02, 2020);
    int day3 = dayofweek(04, 02, 2020);
    int day4 = dayofweek(05, 02, 2020);
    int day5 = dayofweek(11, 02, 2020);
    int day6 = dayofweek(12, 02, 2020);
    int day7 = dayofweek(13, 02, 2020);
    int day8 = dayofweek(20, 02, 2020);
    int day9 = dayofweek(21, 02, 2020);
    int day10 = dayofweek(27, 02, 2020);

    int day11 = dayofweek(02, 03, 2020); //month 2 days
    int day12 = dayofweek(02, 03, 2020);
    int day13 = dayofweek(05, 03, 2020);
    int day14 = dayofweek(10, 03, 2020);
    int day15 = dayofweek(11, 03, 2020);
    int day16 = dayofweek(11, 03, 2020);
    int day17 = dayofweek(17, 03, 2020);
    int day18 = dayofweek(19, 03, 2020);
    int day19 = dayofweek(24, 03, 2020);
    int day20 = dayofweek(25, 03, 2020);

    int month1 [] = {day1, day2, day3, day4, day5, day6, day7, day8, day9, day10};
    int n1 = sizeof(month1)/sizeof(month1[0]);
    cout << "Original array: ";
    for (int i=0; i < n1; i++)
    cout << month1[i] <<" ";
    most_occurred_number1(month1, n1);

    int month2 [] = {day11, day12, day13, day14, day15, day16, day17, day18, day19, day20};
    int n2 = sizeof(month2)/sizeof(month2[0]);
    cout << "Original array: ";
    for (int i=0; i < n2; i++)
    cout << month2[i] <<" ";
    most_occurred_number2(month2, n2);

}```
  • 3
    Note that `day_of_week` returns a value in the range [0,6] but `to_date_string` operates on values in the range [1,7]. I don't see any reason to use a `switch`. It just creates bloated code. Store the day-of-week names in a static array and then just return them: `static const std::string dayofweek[7] = { "Monday", "Tuesday", ... "Sunday" }; return dayofweek[x];` – paddy Sep 04 '20 at 05:05
  • 1
    Beyond these observations, it's unclear precisely what you're asking. Please edit your question to show the output you _expect_ and the output you _actually get_. – paddy Sep 04 '20 at 05:07
  • 2
    Can you tell why you need 2 functions `to_date_string1` and `to_date_string2` which do exactly the same? Why can't you just have one, and call it as often you want / need to? – πάντα ῥεῖ Sep 04 '20 at 05:32
  • 1
    I think you are fighting several battles at once and losing in most of them. May I recommend to https://ericlippert.com/2014/03/21/find-a-simpler-problem/ and/or https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ Those are two generally helpful concepts. – Yunnosch Sep 04 '20 at 06:35
  • Don’t use `02` with leading 0 for February. While it works, you’ll get a surprise when `08` and `09` don’t work for August and September. Numbers with a leading zero are taken to be octal numbers. See for example [Integer literal](https://en.cppreference.com/w/cpp/language/integer_literal). – Ole V.V. Sep 05 '20 at 07:13

1 Answers1

1

I need to convert the integer to a string.

then use std::to_string https://en.cppreference.com/w/cpp/string/basic_string/to_string

The integer is a most_occurred_number

There is no such variable of type int in this code. I can see most_occurred_number1 and most_occurred_number2, which are function parameters - and they seem to be (almost) properly handled in switch statements. Almost - because your to_date_string1 and to_date_string2 are not always returning a value - which is Undefined Behaviour in c++ - you may add return "?" or throw exception in case function is not called with value in proper range.

I have put most_occurred_number as the integer but nothing is changing in the cout

You should show in the code where you have tried to put most_occurred_number

I tried putting month1[i] but it is coming out with an error of an undeclared identifier even though it is in the code.

Array month1 is a parameter of most_occurred_number1 and also a variable in main, and those are the only places where it is visible.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • All of that is true. But I think this does not answer OPs question, not only because OPs question is unclear. E.g. it is quite clear that OP does not want to convert int to string as described in the link you provide, the result should be weekday names, not strings of digits. So, your valuable contribution would seem more appropriate as comments to me. – Yunnosch Sep 04 '20 at 06:37
  • @Yunnosch the answer is too long to fit in the comment, it answers asked questions, it does not try to change the code or give hints or try to guess answer. "the result should be weekday names, not strings of digits" - thats not what OP asked for, why you think this is the question? – marcinj Sep 04 '20 at 08:14
  • Two of the shown functions (though identical) attempt to convert a number to a weekday string. None of the shown functions attempts to convert a number to a string of digits. I consider the shown code part of the question. (Though I do not accept only code as a question.) That is what makes me think that OP wants to convert to weekdays. – Yunnosch Sep 04 '20 at 08:22
  • :-) I prefer not to guess what OP really wants. But if that is the real question then the answer is to change cout to : `cout << to_date_string1(month2[i]) <<" ";` and the same with the other cout – marcinj Sep 04 '20 at 08:27
  • Yet you do guess that "convert to string" means the special kind of string which is a sequence of digits, without anything to support that guess. – Yunnosch Sep 04 '20 at 08:35