Saturday, July 14, 2018

Programming Challenges - Question : The Trip Problem C-Language Program

Problem : The Trip

www.matterhere.com - Nareddula Rajeev Reddy (NRR)

The Trip
A number of students are members of a club that travels annually to exotic locations. Their destinations in the past have included Indianapolis, Phoenix, Nashville, Philadelphia, San Jose, and Atlanta. This spring they are planning a trip to Eindhoven.
The group agrees in advance to share expenses equally, but it is not practical to have them share every expense as it occurs. So individuals in the group pay for particular things, like meals, hotels, taxi rides, plane tickets, etc. After the trip, each student's expenses are tallied and money is exchanged so that the net cost to each is the same, to within one cent. In the past, this money exchange has been tedious and time consuming. Your job is to compute, from a list of expenses, the minimum amount of money that must change hands in order to equalize (within a cent) all the students' costs.

The Input
Standard input will contain the information for several trips. The information for each trip consists of a line containing a positive integer, n, the number of students on the trip, followed by n lines of input, each containing the amount, in dollars and cents, spent by a student. There are no more than 1000 students and no student spent more than $10,000.00. A single line containing 0 follows the information for the last trip.

The Output
For each trip, output a line stating the total amount of money, in dollars and cents, that must be exchanged to equalize the students' costs.

Sample Input
3
10.00
20.00
30.00
4
15.00
15.01
3.00
3.01
0

Output for Sample Input
$10.00
$11.99

C-Language Program

#include <stdio.h>
#include <math.h>

int costs[1000];
int nCosts = 1000;

int sumCosts() {
    int sum = 0;
    
    int i = 0;
    for (i = 0; i < nCosts; i++) {
        sum += costs[i];
    }
    return sum;
}

int main() {
    for (;;) {
        scanf("%d", &nCosts);
        if (nCosts == 0) { 
            break;
        }
        
        int c = 0;
        for (c = 0; c < nCosts; c++) {
            int dollars;
            int cents;
            scanf("%d.%d", &dollars, &cents);
            costs[c] = dollars * 100 + cents;            
        }
        
        int sumCents = sumCosts();
        double avgCents = ((double) sumCents) / nCosts;
        double taken = 0;
        double given = 0;
        
        int i;
        for (i = 0; i < nCosts; i++) {
            double deltaCents = costs[i] - avgCents;
            if (deltaCents < 0) {
                taken += -((int) deltaCents) / 100.0;
            } else {
                given += ((int) deltaCents) / 100.0;
            }
        }
        
        printf("$%.2f\n", taken > given ? taken : given);
    }
    return 0;
}

Sources:
https://github.com/

Programming Challenges - Question: Minesweeper Problem C-Language Program

Problem : Minesweeper

www.matterhere.com - Nareddula Rajeev Reddy (NRR)

/*
Minesweeper
Have you ever played Minesweeper? This cute little game comes with a certain operating system whose name we can't remember. The goal of the game is to find where all the mines are located within a M x N field.

The game shows a number in a square which tells you how many mines there are adjacent to that square. Each square has at most eight adjacent squares. The 4 x 4 field on the left contains two mines, each represented by a ``*'' character. If we represent the same field by the hint numbers described above, we end up with the field on the right:

*...
....
.*..
....
*100
2210
1*10
1110

Input
The input will consist of an arbitrary number of fields. The first line of each field contains two integers n and m ( 0 < n, m100) which stand for the number of lines and columns of the field, respectively. Each of the next n lines contains exactly m characters, representing the field.

Safe squares are denoted by ``.'' and mine squares by ``*,'' both without the quotes. The first field line where n = m = 0 represents the end of input and should not be processed.

Output
For each field, print the message Field #x: on a line alone, where x stands for the number of the field starting from 1. The next n lines should contain the field with the ``.'' characters replaced by the number of mines adjacent to that square. There must be an empty line between field outputs.

Sample Input
 4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0

Sample Output
Field #1:
*100
2210
1*10
1110

Field #2:
**100
33200
1*100
*/

C-Language Program

#include <stdio.h>

int field[100][100]; //mine squares will be denoted by -1
//current bounds:
int nLines = 100;
int nColumns = 100;

void clearField() {
    int i;
    for (i = 0; i < nLines; i++) {
        int j;
        for (j = 0; j < nColumns; j++) {
            field[i][j] = 0;
        }
    }
}

void setBounds(int l, int c) {
    nLines = l;
    nColumns = c;
}

int isInsideBounds(int i, int j) {
    return i >= 0 && i < nLines && j >=0 && j < nColumns;
}

void increment(int i, int j) {
    if (isInsideBounds(i, j) && field[i][j] != -1) {
        ++field[i][j];
    }
}

void setMine(int i, int j) {
    field[i][j] = -1;
    
    //increment neighbours
    int m;
    for (m = -1; m <= 1; m++) {
        int n;
        for (n = -1; n <= 1; n++) {
            increment(i + m, j + n);
        }
    }
}

void printField(nField) {
    printf("Field #%d:\n", nField);
    
    int i;
    for (i = 0; i < nLines; i++) {
        int j;
        for (j = 0; j < nColumns; j++) {
            int x = field[i][j];
            
            if (x == -1) { //if is mine, print *
                printf("*");
            } else {
                printf("%d", x);
            }
        }
        printf("\n");
    }
}

int main() {
    int l, c; //input vars (field lines and columns)
    int nField = 1;
    
    for (;;) {
        scanf("%d %d", &l, &c);
        if (l == 0 && c == 0) { //if end of input, break
            break;
        }
        
        setBounds(l, c);
        clearField();
        
        int i;
        for (i = 0; i < l; i++) {
            char line[c + 1]; //remeber \0
            scanf("%s", line);
            
            int j;
            for (j = 0; j < c; j++) {
                char currentChar = line[j];
                if (currentChar == '*') {
                    setMine(i, j);
                }
            }
        }
        
        if (nField > 1) {
            printf("\n");
        }
        printField(nField);
        
        ++nField;
    }
    return 0;
}

Sources:
https://github.com/

Programming Challenges - Question: LCD Display Problem C-Language Program

Problem : LCD Display

www.matterhere.com - Nareddula Rajeev Reddy (NRR)
1.6.4 LCD Display
PC/UVa IDs: 110104/706, Popularity: A, Success rate: average Level: 1 A friend of yours has just bought a new computer. Before this, the most powerful machine he ever used was a pocket calculator. He is a little disappointed because he liked the LCD display of his calculator more than the screen on his new computer! To make him happy, write a program that prints numbers in LCD display style.

Input
The input file contains several lines, one for each number to be displayed. Each line contains integers s and n, where n is the number to be displayed (0 ≤ n ≤ 99, 999, 999) and s is the size in which it shall be displayed (1 ≤ s ≤ 10). The input will be terminated by a line containing two zeros, which should not be processed.

Output
Print the numbers specified in the input file in an LCD display-style using s “-” signs for the horizontal segments and s “|” signs for the vertical ones. Each digit occupies exactly s + 2 columns and 2s + 3 rows. Be sure to fill all the white space occupied by the digits with blanks, including the last digit. There must be exactly one column of blanks between two digits. Output a blank line after each number. You will find an example of each digit in the sample output below


C-Language Program


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

int nLines;
int nColumns;
char lcdNumber[23][12];
char lcdNumbers[8][23][12];

void setLcdSize(int size) {
    nLines = 2 * size + 3;
    nColumns = size + 2;
}

void clearLcd() {
    int i;
    for (i = 0; i < nLines; i++) {
        int j;
        for (j = 0; j < nColumns; j++) {
            lcdNumber[i][j] = ' ';
        }
    }    
}

void drawLine(int lineNumber) {
    int j;
    for (j = 1; j < nColumns - 1; j++) {
        lcdNumber[lineNumber][j] = '-';
    }
}

void drawFirstLine() {
    drawLine(0);
}

void drawMiddleLine() {
    drawLine(nLines / 2);
}

void drawLastLine() {
    drawLine(nLines - 1);
}

void drawFirstColumn(int columnNumber) {
    int i;
    for (i = 1; i < nLines / 2; i++) {
        lcdNumber[i][columnNumber] = '|';    
    }
}

void drawLastColumn(int columnNumber) {
    int i;
    for (i = (nLines / 2) + 1; i < nLines - 1; i++) {
        lcdNumber[i][columnNumber] = '|';    
    }
}

void drawFirstLeftColumn() {
    drawFirstColumn(0);
}

void drawLastLeftColumn() {
    drawLastColumn(0);
}

void drawFirstRightColumn() {
    drawFirstColumn(nColumns - 1);
}

void drawLastRightColumn() {
    drawLastColumn(nColumns - 1);
}

void printLcd(int position) {
    int i;
    for (i = 0; i < nLines; i++) {
        int j;
        for (j = 0; j < nColumns; j++) {
            lcdNumbers[position][i][j] = lcdNumber[i][j];
        }
    }    
}

void printZero(int position) {
    clearLcd();
    drawFirstLine();
    drawFirstLeftColumn();
    drawFirstRightColumn();
    drawLastLeftColumn();
    drawLastRightColumn();
    drawLastLine();
    printLcd(position);
}

void printOne(int position) {
    clearLcd();
    drawFirstRightColumn();
    drawLastRightColumn();
    printLcd(position);
}

void printTwo(int position) {
    clearLcd();
    drawFirstLine();
    drawFirstRightColumn();
    drawMiddleLine();
    drawLastLeftColumn();
    drawLastLine();
    printLcd(position);
}

void printThree(int position) {
    clearLcd();
    drawFirstLine();
    drawFirstRightColumn();
    drawMiddleLine();
    drawLastRightColumn();
    drawLastLine();
    printLcd(position);
}

void printFour(int position) {
    clearLcd();
    drawFirstLeftColumn();
    drawFirstRightColumn();
    drawMiddleLine();
    drawLastRightColumn();
    printLcd(position);
}

void printFive(int position) {
    clearLcd();
    drawFirstLine();
    drawFirstLeftColumn();
    drawMiddleLine();
    drawLastRightColumn();
    drawLastLine();
    printLcd(position);
}

void printSix(int position) {
    clearLcd();
    drawFirstLine();
    drawFirstLeftColumn();
    drawMiddleLine();
    drawLastLeftColumn();
    drawLastRightColumn();
    drawLastLine();
    printLcd(position);
}

void printSeven(int position) {
    clearLcd();
    drawFirstLine();
    drawFirstRightColumn();
    drawLastRightColumn();
    printLcd(position);
}

void printEight(int position) {
    clearLcd();
    drawFirstLine();
    drawFirstLeftColumn();
    drawFirstRightColumn();
    drawMiddleLine();
    drawLastLeftColumn();
    drawLastRightColumn();
    drawLastLine();
    printLcd(position);
}

void printNine(int position) {
    clearLcd();
    drawFirstLine();
    drawFirstLeftColumn();
    drawFirstRightColumn();
    drawMiddleLine();
    drawLastRightColumn();
    drawLastLine();
    printLcd(position);
}

void printNumber(int n, int position) {
    void (*functions[10]) (int position) = {
    printZero,
    printOne,
    printTwo,
    printThree,
    printFour,
    printFive,
    printSix,
    printSeven,
    printEight,
    printNine
    };

    functions[n](position);
}

int main() {
    int size = 0;
    char strNumber[9];
    scanf("%d %s", &size, strNumber);
    
    while (size != 0 || atoi(strNumber) != 0) {
        setLcdSize(size);
        int length = strlen(strNumber);
        
        int i;
        for (i = 0; i < length; i++) {
            int n = strNumber[i] - '0';
            printNumber(n, i);
        }
        
        for (i = 0; i < nLines; i++) {
            int n;
            for (n = 0; n < length; n++) {
                int j;
                for (j = 0; j < nColumns; j++) {
                    printf("%c", lcdNumbers[n][i][j]);
                }
                if (n < length - 1) {
                    printf(" "); //column of blanks
                }
            }
            printf("\n");
        }
        printf("\n");

        scanf("%d %s", &size, strNumber);
    }
    return 0;
}

Sources:
https://github.com/