Create a 2D array of size m x n where m is the number of employees working and n is the
number of weekdays (mon – fri). Populate the array with the hours the employees have worked
for 1 week (random values between 5 and 10).
#include <iostream>
#include <ctime>
#include <string>
#include <iomanip>
std::string getDayOfWeek(int val);
int main()
{
std::srand(std::time(nullptr));
int employees{0}, weekdays{0};
std::cout << "Enter the number of employees: ";
std::cin >> employees;
std::cout << "Enter the number of weekdays: ";
std::cin >> weekdays;
int* arr = new int[employees * weekdays];
for (int employee = 0; employee <= employees; ++employee )
{
if (employee == 0)
std::cout << std::setw(10) << ' ';
else
std::cout << "e" << employee - 1 << " ";
}
std::cout << std::endl;
for (int day = 0; day < weekdays; ++day)
{
for (int empl = -1; empl < employees; ++empl)
{
if (empl == -1)
std::cout << std::setw(9) << getDayOfWeek(day) << ' ';
else{
*(arr + day * weekdays + empl) = rand() % 6 + 5;
std::cout << std::setw(2) <<*(arr + day * weekdays + empl) << ' ';
}
}
std::cout << std::endl;
}
delete[] arr;
return 0;
}
std::string getDayOfWeek(int val)
{
switch (val % 5)
{
case 0: return "Monday";
case 1: return "Tuesday";
case 2: return "Wednesday";
case 3: return "Thursday";
case 4: return "Friday";
default:
return "";
}
}
Comments
Leave a comment