C++ program to print a specific star pattern - c++

*****
***
*
***
*****
For odd no. of rows only. The no. of rows is variable.
We have to print this star pattern in c++ using nested loops.
#include <iostream>
using namespace std;
int main()
{
int trows, rowno = 1;
cout << "Enter total no. of rows";
cin >> trows;
while (rowno <= (trows + 1) / 2) {
int space = 1;
while (space <= (rowno - 1)) {
cout << ' ';
space = space + 1;
}
int star = 1;
while (star <= (2 * (trows - rowno) - (trows + 1) / 2)) {
cout << '*';
star = star + 1;
}
cout << endl;
rowno = rowno + 1;
}
rowno = ((trows+1)/2)+1;
while (rowno <= trows) {
int space = 1;
while (space <= trows - rowno) {
cout << ' ';
space = space + 1;
}
int star = 1;
while (star <= 2 * rowno - trows) {
cout << '*';
star = star + 1;
}
cout << endl;
rowno = rowno + 1;
}
return 0;
}
The program ran correctly for the total no. of rows 5 or 7. But it gives a distorted shape for total no. of rows greater than 7.

Here's a simplified version that works for any odd sized number of rows:
void printstar(const int totalRows)
{
for (int col = 0; col < totalRows; ++col)
{
const int totalStarsThisRow = abs((totalRows / 2) - col)*2+1;
// Write needed spaces
for (int row = 0; row < (totalRows - totalStarsThisRow)/2; ++row)
{
cout << ' ';
}
// Write Stars
for (int row = 0; row < totalStarsThisRow; ++row)
{
cout << '*';
}
cout << '\n';
}
}

Generally, your code is far more complicated than it needs to be, and
you're making it even more complicated by ignoring how the language works.
Just count from zero. This code also shows that you're attempting to brute
force the solution. There are patterns to be taken advantage of that you
don't. Sit and think about the problem before writing any code. The coding
quickly becomes a technical detail and not the primary challenge.
Let's examine your shape. You state that input will always be an odd number, n. You need to print n rows. The first row contains n stars. You decrease the number of stars by 2, or drop down to the next odd number. The shape is symmetric.
Break the problem down. Can you print a square?
Here's the main() that we'll be using for testing purposes:
int main() {
print_shape(5);
std::cout << "\n\n";
print_shape(7);
std::cout << "\n\n";
print_shape(21);
}
As we progress through this answer, all three shapes need to always be correct. Testing on just one case is asking for a bad time when you're expected to pass multiple cases.
And here's the function for printing the squares:
void print_shape(int rows) {
for (int row = 0; row < rows; ++row) {
for (int column = 0; column < rows; ++column) {
std::cout << '*';
}
std::cout << '\n';
}
}
Pretty straightforward so far. Now, can we modify print_shape() to print the correct number of stars per row?
Our inner for loop controls how many stars get printed, so that's where we should start. We need to consider a pattern between our row number and how many stars are required. We'll use the case where n = 5 as an example.
Row 0: 5
Row 1: 3
Row 2: 1
Row 3: 3
Row 4: 5
Well, for positive numbers, the expression 5 - 2*rowNumber works. But it falls apart when we get to the bottom half. However, note the symmetry of the number of stars. What if we number our rows symmetrically?
Row -2: 5
Row -1: 3
Row 0: 1
Row 1: 3
Row 2: 5
Now, a new pattern that holds for all rows should be sticking out. abs(rowNumber) * 2 + 1 is how many stars we need to print. The next question, how we do know where to start our row numbering? Integer division and symmetry come to our rescue again. With small changes to both loop conditions, we are now printing the correct number of stars on every row for every case.
void print_shape(int rows) {
int startingNum = rows / 2;
for (int row = -startingNum; row <= startingNum; ++row) {
for (int column = 0; column < std::abs(row) * 2 + 1; ++column) {
std::cout << '*';
}
std::cout << '\n';
}
}
So now all that's left is the spaces. And again, we just need to think about it for a bit. First, what spaces do we actually need to print? Do we need to bother printing trailing spaces? (Hint: No)
Let's put a table together for the number of spaces that are required at the beginning each row.
Row -2: 0
Row -1: 1
Row 0: 2
Row 1: 1
Row 2: 0
A very similar pattern almost emerges. What if we make the table a bit fancier?
Spaces Stars
Row -2: 0 5
Row -1: 1 3
Row 0: 2 1
Row 1: 1 3
Row 2: 0 5
It takes some thinking to see this one, and we have to remember that we have a total number of rows that we can consider as well. But putting all of our information together, (totalRows - numStars) / 2 is the pattern that we want.
And because we only need to print the spaces before the stars, we add a separate loop to our function. I'm also going to pull some calculations out and change some names becuase they no longer make sense.
void print_shape(int rows) {
int startingNum = rows / 2;
for (int row = -startingNum; row <= startingNum; ++row) {
int numStars = std::abs(row) * 2 + 1;
int numSpaces = (rows - numStars) / 2;
for (int spaces = 0; spaces < numSpaces; ++spaces) {
std::cout << ' ';
}
for (int star = 0; star < numStars; ++star) {
std::cout << '*';
}
std::cout << '\n';
}
}
And that will print your shape, if and only if you pass an odd number. So we should guarantee that we do just that.
int main() {
int input;
bool oddNumberWasEntered = false;
do {
std::cout << "How many rows? ";
std::cin >> input;
if (is_odd(input)) {
oddNumberWasEntered = true;
} else {
std::cerr << "Must enter an odd number!\n";
}
} while (!oddNumberWasEntered);
print_shape(input);
}
is_odd() is a one-liner function I wrote.
An example:
❯ ./a.out
How many rows? 4
Must enter an odd number!
How many rows? 20
Must enter an odd number!
How many rows? 21
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
From the comments, I really like the idea of building correct sized strings and just printing those at each row. I consider it a more elegant solution as it shortens the code but doesn't lose any readability. I won't add that code to this answer, but I want to say that solution is only possible if you took the time to examine the problem, and find the same patterns.
There is also further input validation that I'm not bothering with, like ensuring the number is positive, or that a number was even entered. The goal of this homework was less about writing code, and more about sitting and thinking about a problem.

Related

Working on solving an Nqueens problem with backtracking and as a CSP specifically most constrained value

So I am currently working on a project to place N queens on an NxN board and prevent them from attacking each other. This project is for an intro level AI course. It has a few specific criteria to get full points which are, finding up to 3 solutions for any board size up to N = 100 in 5 seconds or less. I'm currently trying to make this a constraint satisfaction problem by choosing the most constrained row which if I understand it correctly will prevent rows that are closer to fully attacked from getting there.
Initially the user will input a column number and a queen will be placed in that column on the first row of the board. From there the attack board will be updated using that row column combination by increasing the value of all diagonals and the row and column a small example of the former and the latter below
void main()
{
int size, row, col;
row = 1;
cout << "Enter the board size: ";
cin >> size;
cout << "Enter column of first queen: ";
cin >> col;
cols[row] = col; // cols store the column value of each queen in that particular row.
updateAttack(row, col, +1, size);
findNextQueen(size);
// return here if we found all the solution
//cout << solutionCount << " solutions found. see NQueen.out.\n";
cout << solutionCount << " solutions found. see NQueen.out.\n";
fout.close();
system("pause");
}
void updateAttack(int r, int c, int change, int size) // Updates the attack board given the location a queen being placed
{
int r1, c1;
// update diagnals
for (r1 = r - 1, c1 = c - 1; r1 >= 1 && c1 >= 1; r1--, c1--)
attack[r1][c1] += change;
for (r1 = r + 1, c1 = c + 1; r1 <= size && c1 <= size; r1++, c1++)
attack[r1][c1] += change;
for (r1 = r - 1, c1 = c + 1; r1 >= 1 && c1 <= size; r1--, c1++)
attack[r1][c1] += change;
for (r1 = r + 1, c1 = c - 1; r1 <= size && c1 >= 1; r1++, c1--)
attack[r1][c1] += change;
// update columns
for (r1 = 1, c1 = c; r1 <= size; r1++) // k goes to each row
attack[r1][c1] += change;
}
The main issue with this program is choosing which row to place the queen in. In a simple backtracking method with recursive calls of the queen placing you increment down the rows and place the queen in the first space in that row that isn't currently under attack and then doing the same for the next row and the next queen until the queen cannot be placed, in which case you backtrack and attempt to fix the previous queen by moving it to the next spot. An example of this being done with backtracking and no CSP implemented below.
void findNextQueen(int r, int size)
{
for (int c=1;c<=size;c++)
{
if (attack[r][c]==0) // not under attack
{
cols[r]=c; // assign another queen
if (r<size)
{
updateAttack(r,c,+1, size);
findNextQueen(r+1, size);
updateAttack(r,c, -1, size);
}
else
{
print1solution(size);
if (solutionCount >= 3)
{
cout << solutionCount << " solutions found. see NQueen.out.\n";
system("pause");
exit(0);
}
}
}
}
return;
}
The constraint satisfaction attempts to solve a problem caused during this backtracking where you might later on completely fill rows below with attack values which will cause alot of backtracking to be required increasing the time it takes by alot of time. It does this by attempting to choose rows that have more spaces being attacked first in order to prevent them from being lost and requiring the late backtracking. My example of this that is causing the issues, currently that it always seems to come to 0 solutions possible below.
void findNextQueen(int size)
{
int bestRowCount = 0;
int bestRow = 2;
for (int r = 2; r <= size; r++) // Meant to find the most constrained row and use that as my r value for attack array
{
int aRowCount = 0; // Count of attacks in current row
for (int c = 1; c <= size; c++)
{
if (attack[r][c] >= 1)
{
aRowCount++;
}
}
if ((aRowCount > bestRowCount) && (aRowCount != size))
{
bestRowCount = aRowCount;
bestRow = r;
}
}
for (int c = 1; c <= size; c++)
{
if (attack[bestRow][c] == 0) // not under attack
{
cols[bestRow] = c; // assign another queen
if (queensLeft(size) == 1) // returns true if there are rows that still lack a queen
{
updateAttack(bestRow, c, +1, size);
findNextQueen(size);
cols[bestRow] = 0;
updateAttack(bestRow, c, -1, size);
}
else
{
print1solution(size);
if (solutionCount >= 3)
{
cout << solutionCount << " solutions found. see NQueen.out.\n";
system("pause");
exit(0);
}
}
}
}
return;
}
The very similar problem was introduced in LeetCode:
https://leetcode.com/problems/n-queens-ii
You can go to discussions and find explanation with code solutions.
You will need to modify code to return possible results when you reach your limit.

Finding all possible combinations with 1 element in each row of a 2D array

Recently I have been trying to do a problem that requires me to find all the different combinations with selecting only 1 element from each row. For example, I'm inputting n rows with 2 strings per row. However, I only want to find all the different combinations where I choose 1 string from each row.
Example:
Input:
3
alex bob
straw mat
eat drink
Example combination:
alex straw drink
This results in 2^n combinations, which in this case would be 2^3 = 8 combinations. However, if I was to use n for loops to find the combinations
e.g.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int n;
int main(int argc, char ** argv) {
cin >> n; //rows of words
string words[n][2]; //the words with 2 words per row
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2; j++) {
cin >> words[i][j]; //input of words
}
}
//finding all possible combinations
for (int i =0; i<n; i++){
for (int j=0; j<2; j++){
for (int x=0; x<2; x++){
//and so on per n
}
}
}
return 0;
}
this would take n for loops to find out all the combinations of the array with only taking one item from each row. What would be the best and simplest approach to finding all different combinations with size n as I would take 1 string out of the two in each row? Thanks.
You can do recursion.
Assuming C++11, something like this maybe (didn't try to compile this though):
// finding all possible combinations
std::vector<std::vector<std::string>> combinations;
const auto processLine = [&](const std::vector<std::string>& currentCombination, int line) {
std::vector<std::string> combination0 = currentCombination;
std::vector<std::string> combination1 = currentCombination;
combination0.push_back(words[line][0]);
combination1.push_back(words[line][1]);
if (line + 1 < n) {
// process next line
processLine(combination0, line + 1);
processLine(combination1, line + 1);
}
else {
// last line - keep the result
combinations.push_back(combination0);
combinations.push_back(combination1);
}
};
std::vector<std::string> empty;
processLine(empty, 0);
// print results
for (const auto& combination : combinations) {
for (const auto& word : combination) {
std::cout << word << " ";
}
std::cout << std::endl;
}
A very simple solution for a setting where you have always 2 elements per row would be to use datatype integer and interpret each bit as a decision for the first or the second column in the respective row; then simply count from 0 to 2^n - 1 in order to get all combinations.
Applied to your example this would look as follows:
int bits meaning
0 000 alex,straw,eat
1 001 alex,straw,drink
2 010 alex,mat,eat
3 011 alex,mat,dring
4 100 bob,straw,eat
5 101 bob,straw,drink
6 110 bob,mat,eat
7 111 bob,mat,drink
For any of the given integer values 0..7, use bit shift operators or &-bitmask to map each bit to a column index:
void getCombinationRepresentedByIntValue(vector<string>& combination, int value) {
int mask = 1;
for (int i=n-1; i>=0; i--) {
if (value & mask)
combination.push_back(words[i][1]);
else
combination.push_back(words[i][0]);
mask = mask << 1;
}
}
That seems to answer your question :
int ct[n]; // count of the number of pass
int current = 0; // index of the current word (n)
/* while not all combinaison have been exploited */
while (current >= 0)
{
cout << words[current][ct[current]]; /* <<<<< can be used another way*/
/* down to the next word */
current ++; // to get the next word
if (current >=n) { // at the end of the list
cout << " ";
current--; // restore last
ct[current]++; // increment number of time we passed
/* find the previous not completely exploited */
while (current >= 0 && ct[current]> 1) /* <<< change 1 to any number of words per line */
{
ct[current] = 0;
current--;
if (current >= 0) ct[current]++;
}
if (current > 0 ) current = 0;
}
}
With your example :
Input :
3
alex bob
straw mat
eat drink
output :
alexstraweat
alexstrawdrink
alexmateat
alexmatdrink
bobstraweat
bobstrawdrink
bobmateat
bobmatdrink
hope it helps !

Is that possible by only using while loop?

My tutor gave me an assignment where I have to write a code that only contains while loop and prints:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
I tried it 100 times and failed 100 times. Due to my limited knowledge, I began to think that my tutor is just messing with my brain. If it's possible, please introduce me a code that prints the numbers in that order.
Thanks...
int i = 1;
int LIMIT = 5;
while (i <= LIMIT)
{
int j = 1;
while (j <= LIMIT -i) //Loop to print the desired space.
{
cout << " ";
j++;
}
int k = i;
while(k)
{
cout<<k; //Printing the digits
k--;
}
cout << endl; //Adding new line character at the end.
i++;
}
Say hello to your tutor :)
int main(void)
{
char str[11] = " ";// Filled with 10 blank spaces
int i=0;
while(i < 5)
{
str[9 - 2*i] = (i+1) + 48;// +48 will give equivalent ASCII code
printf("%s\n",str);
i++;
}
return 0;
}
Try some code like below. i've also create a sample working code here http://goo.gl/gJqias
#include <iostream>
using namespace std;
int main()
{
int start_Index=1, maximum_Index=5;
while(start_Index<=maximum_Index)
{
//below loop prints leading whitespaces
//note that there are two whitespaces per number
int temp_var=start_Index;
while (maximum_Index-temp_var>0)
{
cout <<" ";
temp_var++;//note the post increment operator.
}
//below whiel loop prints lagging numbers with single whitespace before them
temp_var=start_Index;
while(temp_var>0)
{
cout<<" "<<temp_var--;//note the post decrement operator.
}
//Now we start over to next line
cout<<endl;
//Increment the start_index by 1
start_Index++;
}
return 0;
}

Using char arrays to add together large numbers

I have an assignment in which I am supposed to
1.) Read in 2 char arrays of numbers
2.) add together the arrays, performing carry addition, carrying the tens place
3.) print out the newly added array.
I am also supposed to print an error if I need to carry on the last digit of the array (overflow)
so something like 99999999999999999999 +
1 =
________________________
ERROR
That's the part I'm having trouble with.
The above outputs something like "99999999999999999:0" so I have no idea what's going wrong.
I'll post my code, but please be nice :( I know it certainly isn't the most efficient, but I'm just trying to lay things out in a way that is easy for my brain to understand right now.
And yes, I HAVE to use char arrays. I guess it's to help us understand the ascii table.
#include <iostream>
using namespace std;
void InitNumber(char[]);
int AddArrays(char first[], char second[], char combined[]);
void OutputNumber (char[]);
const int LENGTH = 20; // global variable
int main()
{
char set1[LENGTH];
char set2[LENGTH];
char sum[LENGTH];
InitNumber (set1);
InitNumber (set2);
if(AddArrays (set1, set2, sum)) {
cout << "overflow!" << endl;
}
OutputNumber(sum);
}
void InitNumber (char list[])
{
int numberOfDigits;
cout << "Please enter the number of digits in the number: ";
cin >> numberOfDigits;
cout << "Please enter the digits in the number with the LEAST significant first: ";
for (int i = 0; i < numberOfDigits; i++) {
cin >> list [i];
}
for (int l=(numberOfDigits); l < LENGTH; l++) {
list [l] = '0';
}
}
int AddArrays(char first[], char second[], char combined[])
{
for (int h = 0; h < LENGTH; h++)
combined[h]= '0';
int overflow = 0;
for (int i = 0; i < LENGTH; i++) {
int currentSum = (first[i]-'0' + second[i]-'0');
cout << "currentSum = " << currentSum << endl;
if(currentSum / 10 == 0 )
combined[i] += currentSum;
else if (currentSum/10 !=0) {
if (i == LENGTH-1 && currentSum/10 !=0){
overflow = 1;
}
else{
combined [i] += currentSum%10;
cout << "current i: " << combined[i] << endl;
combined [i+1] += currentSum/10;
cout << "current i+1: " << combined[i+1] << endl;
}
}
}
return overflow;
}
void OutputNumber(char arrayOut[])
{
for (int l=LENGTH - 1; l >= 0; l--)
cout << arrayOut[l];
}
working input
input
6
1 2 3 4 5 6
7
1 2 3 4 5 6 7
output
00000000000008308642
Not-working output
input
20
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
1
1
output
999999999999999999:0
I'm going to reproduce small parts of your inner loop where you are carrying out the addition, in order to explain why your overflow detection is broken.
for (int i = 0; i < LENGTH; i++) {
int currentSum = (first[i]-'0' + second[i]-'0');
if(currentSum / 10 == 0 )
combined[i] += currentSum;
Here you're adding the corresponding pair of digits from the two numbers you're adding, and checking (in your particular style) if they overflowed. Try to remember this part, it's important. In order to check for overflow, you're only checking the result of adding the pair of digits from the two large numbers you're adding.
else if (currentSum/10 !=0) {
This is a completely useless check. You can only get here if the division is known to produce a non-zero result. This if() can be removed completely. Now, the relevant part of your code is
combined [i] += currentSum%10;
combined [i+1] += currentSum/10;
Do you see the problem yet?
Your approach is, once overflow is detected, is to increment the next higher order digit in the result.
Unfortunately, on the next loop iteration, in order to check for carry over, you're just checking the sum of the next corresponding digit pair, from the two large numbers you're adding. The carry-over you're saving here is going to get completely ignored.
Say your numbers are two digits long max, rather than 20, and you entered the numbers 99 and 1.
On the first iteration, you'll add 9 and 1, save 0 as the first digit, and add 1 to the second digit in the sum.
On the second iteration, you'll add 9 and 0, and, given your logic above, conclude that nothing overflowed.

Vector cannot be overwritten

I'm trying to write a program for university. The goal of the program is to make a nurse schedule for a hospital. However, i'm really stuck for the moment. Below you can find one function of the program.
The input for the function is a roster which consists of the shift each nurse has to perform on each day. In this example, we have 32 rows (32 nurses) and 28 columns (representing 28 days). Each cell contains a number from 0 to 6, indicating a day off (0) or a certain shift (1 to 6).
The function should calculate for each day, how many nurses are scheduled for a certain shift. For example, on the first day, there are 8 nurses which perform shift 2, 6 shift 3 and so forth. The output of the function is a double vector.
I think the function is mostly correct but when I call it for different rosters the program always gives the first roster gave.
void calculate_nbr_nurses_per_shift(vector<vector<int>> roster1)
{
for (int i = 0; i < get_nbr_days(); i++)
{
vector<int> nurses_per_shift;
int nbr_nurses_free = 0;
int nbr_nurses_shift1 = 0;
int nbr_nurses_shift2 = 0;
int nbr_nurses_shift3 = 0;
int nbr_nurses_shift4 = 0;
int nbr_nurses_shift5 = 0;
int nbr_nurses_shift6 = 0;
for (int j = 0; j < get_nbr_nurses(); j++)
{
if (roster1[j][i] == 0)
nbr_nurses_free += 1;
if (roster1[j][i] == 1)
nbr_nurses_shift1 += 1;
if (roster1[j][i] == 2)
nbr_nurses_shift2 += 1;
if (roster1[j][i] == 3)
nbr_nurses_shift3 += 1;
if (roster1[j][i] == 4)
nbr_nurses_shift4 += 1;
if (roster1[j][i] == 5)
nbr_nurses_shift5 += 1;
if (roster1[j][i] == 6)
nbr_nurses_shift6 += 1;
}
nurses_per_shift.push_back(nbr_nurses_shift1);
nurses_per_shift.push_back(nbr_nurses_shift2);
nurses_per_shift.push_back(nbr_nurses_shift3);
nurses_per_shift.push_back(nbr_nurses_shift4);
nurses_per_shift.push_back(nbr_nurses_shift5);
nurses_per_shift.push_back(nbr_nurses_shift6);
nurses_per_shift.push_back(nbr_nurses_free);
nbr_nurses_per_shift_per_day.push_back(nurses_per_shift);
}
}
Here you can see the program:
Get_shift_assignment() and schedule_LD are other rosters.
void test_schedule_function()
{
calculate_nbr_nurses_per_shift(schedule_LD);
calculate_nbr_nurses_per_shift(get_shift_assignment());
calculate_coverage_deficit();
}
One more function you need to fully understand the problem is this one:
void calculate_coverage_deficit()
{
int deficit = 0;
for (int i = 0; i < get_nbr_days(); i++)
{
vector<int> deficit_day;
for (int j = 0; j < get_nbr_shifts(); j++)
{
deficit = get_staffing_requirements()[j] - nbr_nurses_per_shift_per_day[i][j];
deficit_day.push_back(deficit);
}
nurses_deficit.push_back(deficit_day);
}
cout << "Day 1, shift 1: there is a deficit of " << nurses_deficit[0][0] << " nurses." << endl;
cout << "Day 1, shift 2: there is a deficit of " << nurses_deficit[0][1] << " nurses." << endl;
cout << "Day 1, shift 3: there is a deficit of " << nurses_deficit[0][2] << " nurses." << endl;
cout << "Day 1, shift 4: there is a deficit of " << nurses_deficit[0][3] << " nurses." << endl;
}
So the problem is that each time I run this program it always gives me the deficits of the first roster. In this case, this is Schedule_LD. When I first run the function with input roster get_shift_assignment() than he gives me the deficits for that roster.
Apparently the nbr_nurses_per_shift_per_day[][] vector is not overwritten the second time I run the function and I don't know how to fix this... Any help would be greatly appreciated.
Let me try to summarize the comments:
By using global variables to return values from your functions it is very likely, that you forgot to remove older results from one or more of your global variables before calling functions again.
To get around this, return your results from the function instead.
Ex:
vector<vector<int>> calculate_nbr_nurses_per_shift(vector<vector<int>> roster1)
{
vector<int> nbr_nurses_per_shift_per_day; // Create the result vector
... // Do your calculations
return nbr_nurses_per_shift_per_day;
}
or if you do not want to return a vector:
void calculate_nbr_nurses_per_shift(vector<vector<int>> roster1, vector<vector<int>> nbr_nurses_per_shift_per_day)
{
... // Do your calculations
}
But clearly, the first variant is a lot less error-prone (in the second example you can forget to clear nbr_of_nurses again) and most compilers will optimize the return nbr_nurses_per_shift_per_day so the whole vector does not get copied.
The second possible issue is that ´get_nbr_days()´ might return numbers that are larger or smaller than the actual size of your vector. To work around this, use either the size() method of vector or use iterators instead.
Your first function would then look like this:
vector<vector<int>> calculate_nbr_nurses_per_shift(vector<vector<int>> roster1)
{
vector<vector<int>> nbr_nurses_per_shift_per_day;
for (vector<vector<int>>::iterator shiftsOnDay = roster1.begin(); shiftsOnDay != roster1.end(); ++shiftsOnDay)
{
vector<int> nurses_per_shift(6, 0); // Create vector with 6 elements initialized to 0
for (vector<int>::iterator shift = shiftsOnDay->begin(); shift != shiftsOnDay->end(); ++shift)
{
if (*shift == 0)
nurses_per_shift[5]++;
else
nurses_per_shift[*shift - 1]++; // This code relies on shift only containing meaningful values
}
nbr_nurses_per_shift_per_day.push_back(nurses_per_shift);
}
return nbr_nurses_per_shift_per_day;
}