## UVA Problem 12614 ( Earn For Future ) Solution

UVA Problem 12614 ( Earn For Future ) Solution:

Solving Technique:

Like many other programming problem this tries to tangle us with its complex language and useless data to through us off the right path. I have provided three working solutions with same logic but with some optimizations.

This program simply requires us to find the max number among given inputs.

At first glance it may seem like we need to bitwise & ( and ) all given value for the result. But trying that doesn’t yield the right answer.

If you need to know what this problem is about:

The problem says bitwise operation will be performed on card numbers. But we do not need to perform bitwise operations. It is not our concern. But the problem also says that he have picked N cards. Now he needs to choose more cards to maximize his gain. The problem say, ” Please tell him the maximum amount he can win from these set of cards”. This means we obviously need to pick the biggest number because we want to maximize the amount ( performing bitwise & ( and ) with the biggest number instead of a smaller gives us a bigger number ).

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.

Code Explanation:

The first line of input is the number of test cases ( n ). The next line of input the number of inputs (I used m in my code) to come. Following m lines inputs.

Here we do not need an array to store the variable. We can just calculate as the values are being given. For every test case I use the first card number as the max number, then I compare other inputs against it.

Input:

```6
2
0 1
2
3 5
3
0 1 2
4
0 0 9 1
5
0 99 1 9 5
6
8 0 7 5 8 5```

Output:

```Case 1: 1
Case 2: 5
Case 3: 2
Case 4: 9
Case 5: 99
Case 6: 8```

### Using some assumption:

```/*
* @author Quickgrid ( Asif Ahmed )
* Problem UVA 12614 ( Earn For Future )
*/

#include<stdio.h>

int main(){
register unsigned n, i;
/*
* Since we are told we won't get any negative numbers
*/
unsigned m, a, max, c = 1;

scanf("%u", &n);
while (n--){
/*
* Assuming m is at least 1, so i scan the first one as max
*/
scanf("%u%u", &m, &max);
/*
* Already one input taken, so decrease counter
*/
m -= 1;
while (m--){
scanf("%u", &a);
if (a > max)
max = a;
}
printf("Case %u: %u\n", c++, max);
}
return 0;
}
```

### Another one with custom scanning and some bit-wise tricks:

```/*
* Author: Quickgrid ( Asif Ahmed )
* Site: https://quickgrid.wordpress.com
* Problem: UVA 12614 ( Earn For Future )
*/

#include<stdio.h>

void Rfastscan(register unsigned &x){
register int c;
x = 0;
c = getchar();
for(;(c > 47 && c < 58); c = getchar())
x = (x << 1) + (x << 3) + c - 48;
}

void fastscan(int &x){
unsigned neg = 0;
register int c;
x = 0;
c = getchar();

if(c == '-'){
neg = 1;
c = getchar();
}

for(;(c > 47 && c < 58); c = getchar())
x = (x<<1) + (x<<3) +c -48;
if(neg)
x = ~x+1;   /* 2's complement */
}

int main(){
register unsigned n, m, i, c = 1;
int a, max;

Rfastscan(n);
while(n--){
Rfastscan(m);
fastscan(max);

for(i=1; i<m; ++i){
fastscan(a);
if(a > max)
max = a;
}
printf("Case %u: %u\n", c++, max);
}
return 0;
}
```

## UVA Problem 12541 ( Birthdates ) Solution

UVA Problem 12541 ( Birthdates ) Solution:

Solving Technique:

Rank 363, Run time 9 ms ( 0.009 s ).

This problem seems easy at first glance but requires some thinking. There will be only two output.

First one will be the name of the youngest person ( The person whose was born after everyone ). Meaning his/her birth year, month, date is greater than others. Ex, a person born on 1991 is younger than person born on 1990.

Second one will be the name of the oldest person ( The person whose was born before everyone ). Meaning his/her birth year, month, date is less than others. Ex, a person born on 1990 is older than person born on 1991.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.

Code Explanation:

Since the name doesn’t exceed 15 letter so an array of size 16 ( i use 1 more for safety ) will suffice. Next there are 4 inputs each time. One is name string, the rest are integers such as date, month and year respectively.

Since the inputs are all related I used a struct to keep all data in a structure named person.

Now I use two more struct variable named young and old. By default I set both of them to first value of our structure. Now I compare if year is greater I set young to that struct variable in that loop. If less than I set old to the struct variable in that loop. If both of these are not true then I follow the same logic for month and date.

In the and the young and old struct variable get set to youngest and oldest struct variable based on logic.

Since I only need to print the youngest and oldest person name so, I just access our young and old structure with name property using dot operator and print them each on a new line.

Input:

```10
Machel 31 12 4999
Amar 27 9 1991
Kara 18 9 5001
Tom 29 1 1991
Priyanka 1 12 5001
Melissa 25 10 1991
Ping 16 10 5001
Amidala 10 1 1991
Xena 17 10 5001```

Output:

```Priyanka
Amidala```

Code:

```/*
* Author: Quickgrid ( Asif Ahmed )
* Site: https://quickgrid.wordpress.com
* Problem: UVA 12541 ( Birth dates  )
*/

#include<stdio.h>

struct person{
char name;
unsigned int date, month, year;
};

int main(){
register unsigned int n, i = 0;

scanf("%u", &n);

struct person p[n], maxp, minp;

for(; i<n; ++i){
scanf("%s%u%u%u", &p[i].name, &p[i].date, &p[i].month, &p[i].year);
}

maxp = p;    /*younger person*/
minp = p;    /*older person*/

for(i=0; i<n; ++i){
if(p[i].year > maxp.year)
maxp = p[i];

else if(p[i].year < minp.year)
minp = p[i];

else{
if(p[i].month > maxp.month && p[i].year >= maxp.year)
maxp = p[i];

else if(p[i].month < minp.month && p[i].year <= minp.year)
minp = p[i];

else{
if(p[i].date > maxp.date && p[i].month >= maxp.month && p[i].year >= maxp.year)
maxp = p[i];
else if(p[i].date < minp.date && p[i].month <= minp.month && p[i].year <= minp.year)
minp = p[i];
}
}

}

printf("%s\n%s\n", maxp.name, minp.name);

return 0;
}
```

## UVA Problem 12289 ( One Two Three ) Solution

UVA Problem 12289 ( One Two Three ) Solution:

Solving Technique:

Very very easy problem. It requires us to only print 1 or 2 or 3 based on input.

There are only 3 types of string (one, two, three) input and among them only one character is changed for each input. Also Their length is always the same.

So the very basic idea that comes to mind is any input with length of five must be three ( since length doesn’t change ). Now if that is not true it can either be one or two.

Code Explanation:

I took a different approach for my code. Instead of including another header file and calling a function to get the string length, I simply checked if the third position in the array ( array starts from zero ) is not a NULL character. If there is no NULL character in that position then I simply outputted 3. Why? because the string three does not contain a NULL character in position 3.

Now above is not true then I check if the string is one or two. If i only check for sub-strings ( part of strings ) in one and find any two characters of ‘o’, ‘n’, ‘e’ then it is definitely one. Then print 1. Else simply print 2.

Input:

```3
owe
too
theee```

Output:

```1
2
3```

Code:

```/**
* @author  Quickgrid ( Asif Ahmed )
*/

#include<stdio.h>

#define O 111
#define N 110
#define E 101

static char s;

int main(){
register unsigned n;
scanf("%u", &n);

while (n--){
scanf("%s", s);
if (s){
printf("3\n");
}else{
unsigned s0 = s;
unsigned s1 = s;
unsigned s2 = s;
if ((s0 == O && s1 == N) || (s1 == N && s2 == E) || (s0 == O && s2 == E))
printf("1\n");
else
printf("2\n");
}
}
return 0;
}
```

## UVA Problem 591 ( Box of Bricks ) Solution

UVA Problem 591 ( Box of Bricks ) Solution:

Solving Technique:

If we read this problem carefully then there are a some very important clues,

1. make all stacks the same height (line 2)
2. he sets out to rearrange the bricks, one by one (line 3)
3. The total number of bricks will be divisible by the number of stacks (line 7)

From the above points and problems description it is clear that Little Bob can only move the blocks one by one. Also the first line of input will be less or equal to 50 and the block heights will be less than or equal to 100. So we can use integer ( also unsigned integer ) for them.

Another thing it is said that dividing sum of stack heights with stack count is always possible. Meaning that gives us the height of the wall ( total stacks height sum, h[i] / stack count, n ).

The technique is there can be three types of box stack. That is equal to, less than or greater than height of the wall( total stacks height sum, h[i] / stack count, n ).

Since Bob can move boxes one by one it doesn’t matter which stack to start from. We can find the number of blocks to move from by only subtracting those stack that are greater in height than the max wall height. No need to perform this action on stacks that are equal or less high than max wall height.

So the result is the sum of subtraction of each stacks that are greater than from ( all stack sum / stack count ). Also since blocks are moved one by one this is the final result.

Important:   We normally print a new line after each output line but for this problem asks us to Output a blank line after each set. Meaning with our normal line break we need to add another line break. Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.

Code Explanation:

1. First I take the stack count and loop that many times. [ also reset moves, and c ( count ) variable each time ]
2. Now for each loop I take the height of stack in an array and sum it.
3. Next I find the wall height by dividing the sum with stack count.
4. Now I use another loop to loop through the array. In it subtract elements greater than stack height and also sum up this value to moves ( number of box movement needed ).
5. Lastly I print in the exact format the problem asked.
6. One more thing I printed an extra newline because the problem asked for a blank line after each set.

Input:

```6
5 2 4 1 7 5
0```

Output:

```Set #1
The minimum number of moves is 5.```

### Code:

```/*
* @author  Quickgrid ( Asif Ahmed )
*/

#include<stdio.h>

static int a;

int main(){
register int i, c = 1;
int n, sum, maxHeight, moves, temp;

while (scanf("%d", &n) && n){
moves = sum = 0;

for (i = 0; i < n; ++i){
scanf("%d", &a[i]);
sum += a[i];
}

maxHeight = sum / n;

for (i = 0; i < n; ++i){
if(a[i] > maxHeight)
moves += a[i] - maxHeight;
}

printf("Set #%d\nThe minimum number of moves is %d.\n\n", c++, moves);
}
return 0;
}
```

## UVA Problem 10324 ( Zeros and Ones ) Solution

UVA Problem 10324 ( Zeros and Ones ) Solution:

Solving Technique:

Here I have provided naive solution for this problem which is very slow. This code can be solved much much faster. I’ll update to it later.

This One was painful. Got Runtime Error a few times before solving this. Still my best time for this so far is 1.899 sec. My main problem was that it said the problem could end with both an EOF or a new line character. The main problem was new line. So I used getchar() ( found it here one of the best sites for contest programming ) and checked for EOF and string length in the string input while condition.

The problem is quite descriptive. It says our string or character array size can be 1000000The string will contain only ‘0’ and ‘1’ characters. Basically we are to find if the characters between the given interval min(i,j)  and max(i,j)Meaning check for the characters from the minimum number between i or j to the maximum number of i or j.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.

Code Explanation:

Here the input format is first take a string or character array ( of size 1000000 ) and next take an integer which is the number if inputs to come. Now loop that many times and take two integers ( i and j ) every loop.

I used a flag named works which is set to 1 by default and use this to check condition if the characters match or not.

Now i loop thorough the given interval ( i and jto check if the character in the given interval are same.

In the loop i check every character of the interval string with the first character of that given interval. Since all character need to be same to output is this code is valid.

Now in the loop if any character doesn’t match I set the flag ( works ) to 0, print No since all characters don’t match and break from loop immediately.

Next thing I check if flag is 1 then I print Yes.

Lastly the getchar()

Input:

```0000011111
3
0 5
4 2
5 9
01010101010101010101010101111111111111111111111111111111111110000000000000000
5
4 4
25 60
1 3
62 76
24 62
1
1
0 0```

Output:

```Case 1:
No
Yes
Yes
Case 2:
Yes
Yes
No
Yes
No
Case 3:
Yes```

Code Naive Solution ( Slow ):

```/**
* Author:  Quickgrid ( Asif Ahmed )
* Site:    https://quickgrid.wordpress.com
* Problem: UVA 10324 - Zeros and Ones
*/

#include<stdio.h>
#include<string.h>

#define N 1000000

static char s[N];

int main(){

int i;
int works, c = 1;
int n, a, b;

while(gets(s)){

scanf("%d", &n);
printf("Case %d:\n", c++);

while( n-- ){

works = 1;

scanf("%d%d",&a,&b);

if(a == b){
printf("Yes\n");
continue;
}

if(a > b){
for(i = b; i <= a; ++i){
if(s[i] != s[a]){
works = 0;
printf("No\n");
break;
}
}
}else{
for(i = a; i <= b; ++i){
if(s[i] != s[a]){
works = 0;
printf("No\n");
break;
}
}
}

if(works)
printf("Yes\n");

}
getchar();
}
return 0;
}
```

## UVA Problem 11192 ( Group Reverse ) Solution

UVA Problem 11192 ( Group Reverse ) Solution:

Solving Technique:

This one is a bit tricky. Also it seems to be closely related to this ( 483 word scramble ) uva problem. Basically for this problem we are to take an integer ( which is the number of groups ) and a string until we are given a 0. Now our input is given like this,

3 ABCEHSHSH

5 FA0ETASINAHGRI0NATWON0QA0NARI0

0

So, the first problem for beginner is taking input for this. From the input we can easily see that there are two parts and the first part is an integer and second part is a string. Also we need to keep taking input until we find 0. What if we divide our scanf or input taking into two parts. First take the integer and check if its 0 or not then if it’s not zero then take the string and continue like this.

Our first input is an integer which is group number or number of parts the string can be divided to. So, from the given problem statement,

TOBENUMBERONEWEMEETAGAINANDAGAINUNDERBLUEICPCSKY

This string has length 48. We have divided into 8 groups of equal length and so the length of each group is 6. Meaning each group or each part of sentence will consist of 6 characters.

Our task is to reverse print/output each and every group. We can easily find how many members are in each group by dividing the length of the string with input ( number of groups ) we are given.

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.

Code Explanation:

Here I have used stack data structure ( see this and this ) to solve this problem. First I find the length of the string then find how many characters in each group by dividing string length with group number. I also keep a counter ” x “ and keep pushing each character of the string in stack until I reach the last character or until our counter is not equal to the group member count. Also increment the counter each time i push into the stack. Also I keep a condition to check if  I have reach the last character or until our counter is equal to the group member count then pop until our loop counter has reached zero. Since stack is a LIFO data structures the last character i pushed into the stack are the first one to come out ( meaning they are outputted in reverse order ). This process keeps running until we reach the end of the sentence.

Input:

```3 ABCEHSHSH
5 FA0ETASINAHGRI0NATWON0QA0NARI0
0```

Output:

```CBASHEHSH
ATE0AFGHANISTAN0IRAQ0NOW0IRAN0```

#### Commented Code ( See the next Code for easier understanding of stack ):

```/**
* @author Quickgrid ( Asif Ahmed )
* @url https://quickgrid.wordpress.com
*/

#include<stdio.h>

/**
* Input string buffer
*/
static char s;

/**
* Global index for accessing input array, may be declared inside main
*/
int top = -1;

int main(){
register unsigned j,u,n,i,x;

/**
* Input group count and Check if count is Not Zero
*/
while (scanf("%u", &n) && n) {
scanf("%s", s);

/**
* Find out input string size
*/
for (j = 0; s[j]; ++j);

/**
* Find out length of each group
*/
n = j / n;

/**
* Group Reverse loop
*/
for (x = i = 0; i < j; ++i) {
/**
* For checking is the item after is NUL terminator
*/
u = s[i + 1];

/**
* @var u
* If the character after current is NUL then NOT u, is True
*
* @var n
* Check to see if characters in group count is NOT full, then push more to stack
*/
if ( !u || x != n ) {
/**
* Push current character on top of stack
*/
s[++top] = s[i];
/**
* Keep a count of characters pushed on stack
*/
++x;
}

/**
* If character limit per group is reached then Condition is True
*/
if ( !u || x == n ) {
/**
* Empty the the stack for every pushed character
*/
while (x--){
/**
* Output character by character in reversed form
*/
printf("%c", s[top]);
--top;
}
x = 0;
}

}
printf("\n");
}
return 0;
}
```

#### Code:

```/*
* Author: Quickgrid ( Asif Ahmed )
* Site: https://quickgrid.wordpress.com
*/

#include<stdio.h>

char s;
int top = -1;

void push(char c){
if(top + 1 >= 101)
return;

s[++top] = c;
}

void pop(){
if (top == -1)
return;

printf("%c", s[top]);
s[top--] = '\0';
}

int main(){
int n,j,i,x;
while (scanf("%d", &n) == 1 && n != 0){
x = 0;
scanf("%s", &s);
for (i = 0; s[i]; ++i);
n = i / n;

for (i = 0; s[i]; ++i){
if (s[i+1] == '\0' || x != n){
push(s[i]);
++x;
}

if (s[i+1] == '\0' || x == n){
while (x--)
pop();

x = 0;
}
}

printf("\n");
}
return 0;
}
```

## BFS Sequence for Unirected Graph in C++

Code Explanation:

Coming soon maybe…… Until then please look at the commented code.

Example Graph:

Code:

```/**
* @author  Quickgrid ( Asif Ahmed )
*/

#include<stdio.h>

#define size 100

/**
* Here nodes are represented with characters
*/
char vertex[size], visited_sequence[size], q[size];

/**
* The graph which is a 2D matrix
*/
int graph[size][size];

/**
* Counters for visited sequence, queue etc
*/
int count_visited_seq = 0, f = 0, r = 0, n;

/**
* Update the visited sequence array
*/
void visit(char v)
{
visited_sequence[count_visited_seq++] = v;
}

/**
* Check the visited sequence array against a node
* to see if it is already visited
*/
int isVisited(char v)
{
for(int i = 0; i != count_visited_seq; ++i)
{
if(visited_sequence[i] == v)
{
return 1; // means already visited
}
}
return 0; // not visited
}

/**
* Display the BFS visited sequence
*/
void displayVisitedSequence()
{
for(int i = 0; i < count_visited_seq; ++i)
{
printf("%c ", visited_sequence[i]);
}
printf("\n");
}

void enqueue(char v)
{
int s = (r + 1) % (size + 1);
if(s == f)
{
return;
}
q[s] = v;
r = s;
}

char dequeue()
{
if(f == r)
{
return '\0';
}
f = (f + 1) % (size + 1);
char v = q[f];
q[f] = '\0';
return v;
}

/**
* Check the adjacent nodes of given node to see if there is an edge
* Also check if it is not already visited then enter it to queue
*/
{
for(int i = 0; i != n; ++i)
{
if(vertex[i] == v)
{
for(int j = 0; j != n; ++j)
{
if(graph[i][j] == 1 && !isVisited(vertex[j]))
{
enqueue(vertex[j]);
}
}
}
}
}

/**
* Create undirected graph, map characters as nodes identifiers
* Reset the graph meaning set 0, to point out no edge exist
*/
void createGraph()
{
int i, j;
for(i = 0; i < 26; ++i)
{
vertex[i] = i + 'A';
}

printf("How many vertex:\n");
scanf("%d", &n);
for(i = 0; i != n; ++i)
{
for(j = 0; j != n; ++j)
{
graph[i][j] = 0;
}
}

for(i = 0; i != n; ++i)
{
for(j = i + 1; j != n; ++j)
{
printf("%c-%c exist:\n", vertex[i], vertex[j]);
scanf("%d", &graph[i][j]);
if(graph[i][j] == 1)
{
graph[j][i] = graph[i][j];
}
}
}
}

/**
* Start processing the queue
*/
void BFS()
{
char v;
int exit = 0;
while(1)
{
fflush(stdout);
printf("Enter a node to Enqueue:\n");
fflush(stdin);
scanf("%c", &v);
for(int i = 0; i != n; ++i)
{
if(vertex[i] == v)
{
enqueue(vertex[i]);
exit = 1;
}
}
if(exit)
{
break;
}
}

while(f != r)
{
char v = dequeue();
if(!isVisited(v))
{
visit(v);
}
}
}

int main()
{
createGraph();
BFS();
displayVisitedSequence();
return 0;
}
```

## Stack Queue Array Hybrid (Data Structure)

Description: Here i combined my knowledge of stack and queue array into a single code. Firstly we are working with Queue. We can enqueue items in a queue. But when we dequeue the dequeued item is pushed to the stack ( We push the dequeued item in stack ) . The items pushed in stack are in the same order as they were in queue  since Queue is FIFO ( First In First Out ) data structure. We can also pop items from that stack. Now when we pop them they are enqueued back to the queue. The interesting thing is when they are popped they fill up the queue in reverse order since Stack is LIFO ( Last In First Out ) data structure.

Stack Functions: pop(), push(), displayStack(), searchStack()

Queue Functions: enqueue(), dequeue(), displayQueue(), searchQueue()

Code:

```/**
* @author  Quickgrid ( Asif Ahmed )
*/

#include<stdio.h>

#define size 100

int q[size+1], f = 0, r = 0;
int s[size], top = -1;

{
printf("1.Enqueue\n");
printf("2.Dequeue\n");
printf("3.Show Queue\n");
printf("4.Search Queue\n");
printf("5.Show Stack\n");
printf("6.Search Stack\n");
printf("7.Pop stack\n");
printf("8.Exit\n");
}

void push(int item)
{
if(top+1 >= size)
{
printf("stack Overflow.\n");
return;
}
s[++top] = item;
}

void displayQueue()
{
int i = (f+1) % (size+1);
for(int j=i; j!=r+1; ++j)
{
if(j>size)
{
j = 0;
}
if(f != r)
{
printf("%d ", q[j]);
}
}
printf("\n");
}

void enqueue(int num)
{
int s = (r+1) % (size+1);
if(s == f)
{
printf("Queue Full.\n");
return;
}
q[s] = num;
r = s;
}

void dequeue()
{
if(f == r)
{
printf("Queue empty.\n");
return;
}
f = (f+1) % (size+1);
printf("%d\n", q[f]);
push(q[f]);
q[f] = 0;
}

void searchQueue(int item)
{
int i = (f+1) % (size+1);
for(int j=i; j!=r+1; ++j)
{
if(j > size)
{
j = 0;
}
if(f!=r)
{
if(q[j] == item)
{
printf("%d found in queue\n", item);
}
}
}
}

void searchStack(int item)
{
for(int i=0; i<=top; ++i)
{
if(s[i] == item)
{
printf("%d found at index: %d\n", item, i);
}
}
}

void displayStack()
{
for(int i=0; i<=top; ++i)
{
printf("%d ", s[i]);
}
printf("\n");
}

void pop()
{
if(top == -1)
{
printf("Stack Underflow.\n");
return;
}
printf("%d\n", s[top]);
enqueue(s[top]);
s[top--] = 0;
}

int main()
{
int choice, exitcode = 1, item;

do
{
printf("Enter choice:\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter item to enqueue:\n");
scanf("%d", &item);
enqueue(item);
printf("Stack: ");
displayStack();
printf("Queue: ");
displayQueue();
break;
case 2:
dequeue();
printf("Stack: ");
displayStack();
printf("Queue: ");
displayQueue();
break;
case 3:
displayQueue();
break;
case 4:
printf("Enter item to search:\n");
scanf("%d", &item);
searchQueue(item);
printf("Stack: ");
displayStack();
printf("Queue: ");
displayQueue();
break;
case 5:
displayStack();
break;
case 6:
printf("Enter item to search:\n");
scanf("%d", &item);
searchStack(item);
printf("Stack: ");
displayStack();
printf("Queue: ");
displayQueue();
break;
case 7:
pop();
printf("Stack: ");
displayStack();
printf("Queue: ");
displayQueue();
break;
case 8:
exitcode = 0;
break;
}
}
while(exitcode == 1);

return 0;
}
```

## Console based Dice Game Code C

A Console based turn based dice rolling game. The player with highest number on dice wins. If both have same dice then the game draws.

CODE:

```#include<stdio.h>
#include<time.h>
#include<stdbool.h>

#define TOTAL_PLAYERS 2

int rollDices(int sides);
int tossCoin();
void exitMessage();
void invalidOp();
void playerDiceOptions(int tc, int sides);

struct player
{
char name;
bool played;
int dice;
} p[TOTAL_PLAYERS];

int main()
{
int sides = 6;
char tcAgree = '\0';

for(int i=0; i<TOTAL_PLAYERS; i++)
{
printf("Enter Player %d name:\n", i+1);
scanf("%s", &p[i].name);
}

int tc = tossCoin();
printf("Toss COIN: (Y/N)?\n");
getchar();
tcAgree = getchar();

printf("\n");
if(tcAgree - 'y'==0|| tcAgree - 'Y'==0)
{
printf("Player %d \"%s\" will play first\n", tc+1, p[tc].name);
p[tc].played = true;
}
else if(tcAgree - 'n'==0 || tcAgree - 'N'==0)
{
exitMessage();
return 0;
}
else
{
invalidOp();
return 0;
}

playerDiceOptions(tc, sides);

//invert the player
if(tc==0)
tc=1;
else
tc=0;

printf("\n");
for(int i=0; i<TOTAL_PLAYERS; i++)
{
if(!p[i].played)
{
printf("Player %d \"%s\" will play\n", i+1, p[i].name);
playerDiceOptions(tc, sides);
}
}

printf("\n");
Sleep(2000);

int k=0;
if(p[k].dice>p[k+1].dice)
printf("Player %d \"%s\" wins\n", k+1, p[k].name);
else if(p[k].dice<p[k+1].dice)
printf("Player %d \"%s\" wins\n", (k+1)+1, p[k+1].name);
else
printf("the game is a draw\n");

return 0;
}

int rollDices(int sides)
{
int dice1 = rand() % sides + 1;
return dice1;
}

int tossCoin()
{
srand(time(NULL));
return rand() % 2;
}

void exitMessage()
{
printf("Exiting.....");
Sleep(2000);
}

void invalidOp()
{
printf("Invalid Operation\n");
}

void playerDiceOptions(int tc, int sides)
{
char rollAgree = '\0';

printf("Roll dice: (Y/N)\n");
getchar();
rollAgree = getchar();
if(rollAgree - 'y'==0|| rollAgree - 'Y'==0)
{
p[tc].dice = rollDices(sides);
printf("Player %d \"%s\" got on dice: %d\n", tc+1, p[tc].name, p[tc].dice);
}
else if(rollAgree - 'n'==0 || rollAgree - 'N'==0)
{
printf("I get it you don't want to play\n");
exitMessage();
return 0;
}
else
{
printf("Invalid operation\n");
return 0;
}
}
```

## Console based Static Clock with Structure C

Create a 24 hour clock with struct that takes hour, minutes, seconds as input. First check if inputs are valid. The output is 1 second plus the input. Input format is (H M S) hour, minute then second.

Input:

`2 3 9`
`4 2 59`
`8 59 59`
`23 59 59`

Output:

`2 : 3 : 10`
`4 : 3 : 0`
`9 : 0 : 0`
`0 : 0 : 0`

CODE:

```#include<stdio.h>

struct time{
short _h, _m, _s;
} t;

int main(){
while(1){
printf("Enter Hour, Minute, Second:\n");
scanf("%d %d %d", &t._h, &t._m, &t._s);
if(t._h<0 || t._h>23 || t._m<0 || t._m>60 || t._s<0 || t._s>60)
printf("\nWrong input try again!\n\n");
else break;
printf("\n");

if(++t._s == 60)
t._s = 0;
if(++t._m == 60)
t._m = 0;
if(++t._h == 24)
t._h = 0;
}
printf("%d : %d : %d\n", t._h, t._m, t._s);
return 0;
}
```