Tuesday, October 20, 2009

[DSA] stun question Wk1


I came across this "game" when our department have a staff retreat at the Singapore flyer.
The objective of the game is to arrange staffs into 3 "E" characters.

Since this week our learning topics revolves around functions(), arithmetic evaluations, use of variables and proper programming methodology. I think it would be interesting to write a program to solve it. Anyway, during that period of time, access to a computer is a luxury. All equations have to be done on the paper. I manage to save this data in my memory and it is fresh still.

Attached is the screenshot of the piece of paper that illustrate the requirements.

Assume that the top and bottom horizontal bar is X, mid horizontal bar is (X-1) and the vertical bar is (X+1). Instinctively, this is a classic case where we can solve for X to the total number of staff and use it to form the "E". There is a possibility that there will be some staff remained, because we would want to maintain the uniformity of the 3 "E" characters.

3[(x-1)+2x+(x+1)] = total staff available.
which can further deduce to
12x = total staff available. => that is for forming 3 "E" characters.

Hence forming of 1 "E" character would be
4x = (1/3) staff available

The diagram below are not drawn to scale.

(x+1)
======== (x)
+
+
====== (x-1)
+
+
======== (x)

But how to translate the problem specs to code????????

we can use a divide and conquer approach to subdivide the problem into smaller portion.
Solving the algebra problem in the program might be redundant, since we have solve it on paper.

Some questions to ponder before start writing the code
a. what are the variables will be used.
b. do we need to use some form of data structure to store the temporary data?
c. what are the algorithms to calculate the X's, to display on the screen.

below are the sub-problem that need to be solve. drill down to further detail is not required.
1. Read in inputs from user for the number of staff available
2. Calculate how many will be left out from the E formation
3. calculate how many staff will form the top, mid and bottom horizontal bar
4. calculate how many staff will form the vertical bar
5. output to a screen

let the code and comments flow in!

2 comments:

Rajendran s/o Raja Rajan said...

#include &ltiostream&gt

using namespace std;

int main()
{
int num, x, remainder, midrow, column, used, halfcolumn, secondhalf;

cout &lt&lt "Enter number of students: "; //get number of students
cin &gt&gt num;

if(num % 4 == 0) //if number of students is a multiple of 4
{
x = num / 4; //solve for x

cout &lt&lt "Number of students used is " &lt&lt num &lt&lt endl;
cout &lt&lt "Number of balance students is 0\n\n"; //All students are used to form the E in this case
}
else //if number of students is not a multiple of 4
{
remainder = num % 4; //finds number of remaining students after forming the E
x = num / 4;
used = 4*x; //finds number of students used to form the E

cout &lt&lt "Number of students used is " &lt&lt used &lt&lt endl;
cout &lt&lt "Number of balance students is " &lt&lt remainder &lt&lt endl &lt&lt endl;
}

midrow = x-1; //number of students for middle row of E
column = x+1; //number of students for column of E

if(column%2 == 1)//gets half the number of students used for the column of E when there's an odd number of students there
{
halfcolumn = (column/2) + 1;
secondhalf = column/2;
}
if(column%2 == 0)//gets half the number of students used for the column of E when there's an even number of students there
{
halfcolumn = (column/2);
secondhalf = column/2;
}

for(int i=0; i&ltx; i++) //Outputs first row of E
{
cout &lt&lt "*";
}
for (int i=0; i&lthalfcolumn; i++) //Outputs first half of the column of E
{
cout &lt&lt "\n*";
}
cout &lt&lt endl;
for(int i=0; i&ltmidrow; i++) //Outputs the middle row if E
{
cout &lt&lt "*";
}
for (int i=0; i&ltsecondhalf; i++) //Outputs the second half of the column of E
{
cout &lt&lt "\n*";
}
cout &lt&lt endl;
for(int i=0; i&ltx; i++) //Outputs the bottom row of E
{
cout &lt&lt "*";
}
cout &lt&lt endl &lt&lt endl;
return 0;
}

sjteo said...

good job rajendran. Have you read the email i sent to you.

Have better insights on explaining/writing code?

Would you summarize and share with the rest?

Sharing is Caring.