Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2019-03-16 00:27:46

Zeeshan 01
Member
Registered: 2016-07-22
Posts: 746

Technical Coding

Different Type of Problem

Write a program that compose message in the same fashion as using mobile keypad. The program must receive integer input. When use enter 2 the program outputs letter ‘A’ and if 2 is pressed twice, the letter ‘A’ will disappear from screen and character ‘B’ will appear and if 2 is pressed thrice the letter ‘B’ will disappear and Letter C will appear. To enter ‘AA’ you have to enter number ‘2’ twice with delay. Delay must be as smaller as it is usually in the mobile.

To check a delay between two key press use the following library

#include <time.h>

To get the current time use following function

time(0)

This function will return numeric value of time. You can compute the time difference by getting time at both key press events and test for a fixed time delay to check whether the key press are consecutive or not.


If you want to clear screen use following statement

System(“CLS”);

Similarly find the associated letters with numbers

Requirement.
You cannot use string and character array. Only primitive data types are allowed. You are allowed to use header files that is mentioned or may use other libraries.


Malik

Offline

#2 2019-03-16 20:08:46

Bob
Administrator
Registered: 2010-06-20
Posts: 10,619

Re: Technical Coding

hi Zeeshan 01

You haven't said what you want from this post.  Also what coding language?

Bob


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

#3 2019-03-16 22:49:30

Zeeshan 01
Member
Registered: 2016-07-22
Posts: 746

Re: Technical Coding

C ++ language.


Malik

Offline

#4 2019-03-21 07:02:54

Zeeshan 01
Member
Registered: 2016-07-22
Posts: 746

Re: Technical Coding

Anyone there to solve this problem by program?.


Malik

Offline

#5 2022-10-15 16:58:44

imcute
Member
Registered: 2022-09-28
Posts: 176

Re: Technical Coding

Zeeshan 01 wrote:

Anyone there to solve this problem by program?.

Not if you disinclude hi-tech data structures cuz theres no reason to do that >:C

Offline

#6 2024-09-16 04:15:47

Adoynal
Novice
Registered: 2023-06-15
Posts: 4

Re: Technical Coding

Maybe this code will help you:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

// Function prototypes
void displayCharacter(int keyPressCount, int key);
void clearScreen();
char getCharacter(int key, int pressCount);

int main() {
    int key;
    int keyPressCount = 0;
    time_t lastPressTime = 0;
    time_t currentTime;
    const int delay = 2;  // Time delay in seconds to distinguish key presses

    while (1) {
        printf("Enter a key (2-9) or 0 to exit: ");
        key = getchar();
        while (getchar() != '\n'); // Clear input buffer

        if (key == '0') {
            break;
        }

        currentTime = time(NULL);

        if (key >= '2' && key <= '9') {
            if (key == lastPressTime) {
                if (difftime(currentTime, lastPressTime) < delay) {
                    keyPressCount++;
                } else {
                    keyPressCount = 1;
                }
            } else {
                keyPressCount = 1;
            }

            lastPressTime = key;
            clearScreen();
            displayCharacter(keyPressCount, key - '0');
        } else {
            printf("Invalid key. Press a number between 2 and 9.\n");
        }
    }

    return 0;
}

void displayCharacter(int keyPressCount, int key) {
    char character = getCharacter(key, keyPressCount);
    printf("%c\n", character);
}

char getCharacter(int key, int pressCount) {
    char result = '\0';

    switch (key) {
        case 2: result = "ABC"[pressCount - 1]; break;
        case 3: result = "DEF"[pressCount - 1]; break;
        case 4: result = "GHI"[pressCount - 1]; break;
        case 5: result = "JKL"[pressCount - 1]; break;
        case 6: result = "MNO"[pressCount - 1]; break;
        case 7: result = "PQRS"[pressCount - 1]; break;
        case 8: result = "TUV"[pressCount - 1]; break;
        case 9: result = "WXYZ"[pressCount - 1]; break;
    }

    return result;
}

void clearScreen() {
    system("CLS");
}

Offline

Board footer

Powered by FluxBB