0

So I want to make this program that will show how many times a character appears in an array and the number of occurences, but I need to order that list of occurences from greatest to least. I've found tutorials to accomplish both separately: How to print how many times a value appears.(Which with help from a friend I simplified. and also how to order values from greatest to least. However, I'm having trouble combining the two to order the occurences from greatest to least. My code:

void keyResults(char buf[], char totalKeyboard[], int key_presses[]) {
//buf[i] is the array value, key_presses[] is how many times the value was found within the array.
int i = 0, j = 0;
char f;

for (i = 0; i < SIZE; i++) {
    for (j = 0; j > (i + 1); j++)
        if ( key_presses[buf[i]] < key_presses[buf[j]]) {

            f =  key_presses[buf[i]];
             key_presses[buf[i]] = key_presses[buf[j]];
            key_presses[buf[j]] = f;
        }
}
for (i = 0; i < SIZE; i++) {

    if (buf[i] == buf[i - 1]) {
        printf("%c: %d\n", buf[i], key_presses[buf[i]]);
    }
    else {
        printf("%c: %d\n", (char)buf[i], key_presses[buf[i]]);
    }

}

}

Full program this is from:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <conio.h>
#define PAUSE system("pause")
#define SIZE 128

void typeYourKeys(char buf[], char totalKeyboard[], int key_presses[]);
void keyResults(char buf[], char totalKeyboard[], int key_presses[]);
void neverUsedKeys(char buf[], char totalKeyboard[], int key_presses[]);
void mostUsedKey(char buf[], char totalKeyboard[], int key_presses[]);



int main() {
    int key_presses[SIZE] = { 0 };
    char totalKeyboard[SIZE] = { 0 };
    char yourTypingOrdered[SIZE] = { 0 };
    char buf[SIZE] = { 0 };
    int option = 0;
    while (option != 5) {
        printf("==========================================================\n");
        printf("=====================Keyboard Statistics Menu================\n");
        printf("============================================================\n\n");
        printf("Things you can do here.\n");
        printf("\n1. Type up to 100 characters. \n");
        printf("\n2. See the keys you typed from most to least.\n");
        printf("\n3. See what keys were never pressed. \n");
        printf("\n4. See what key was touched the most. \n");
        printf("\n5. Exit \n");
        scanf("%i", &option);

        switch (option) {
        case 1:
            typeYourKeys(buf, totalKeyboard, key_presses);
            break;
        case 2:
            keyResults(buf, totalKeyboard, key_presses);
            break;
        case 3:
            neverUsedKeys(buf, totalKeyboard, key_presses);
            break;
        case 4:
            mostUsedKey(buf, totalKeyboard, key_presses);
            break;
        case 5:
            break;
        default:
            printf("This is not a valid choice, please type 1-5. ");
            PAUSE;
        }
    }
    return option;
}

void typeYourKeys(char buf[], char totalKeyboard[], int key_presses[]) {
    int i = 0;

    printf("Hello! Please begin typing your characters. You may type up to 100. Type '~' when you are done. \n");


    for (i = 0; i < SIZE; i++) {
        scanf("%c", &(buf[i]));
        if ((buf[i]) == '~') {
            break;
        }
        else {

            key_presses[buf[i]]++;

        }
    }

}



void keyResults(char buf[], char totalKeyboard[], int key_presses[]) {
    int i = 0, j = 0;
    char f;

    for (i = 0; i < SIZE; i++) {
        for (j = 0; j > (i + 1); j++)
            if ( key_presses[buf[i]] < key_presses[buf[j]]) {

                f =  key_presses[buf[i]];
                 key_presses[buf[i]] = key_presses[buf[j]];
                key_presses[buf[j]] = f;
            }
    }
    for (i = 0; i < SIZE; i++) {

        if (buf[i] == buf[i - 1]) {
            printf("%c: %d\n", buf[i], key_presses[buf[i]]);
        }
        else {
            printf("%c: %d\n", (char)buf[i], key_presses[buf[i]]);
        }

    }
}

void neverUsedKeys(char buf[], char totalKeyboard[], int key_presses[]) {
    int i = 0;
    printf("Here are the keys that were never pressed: \n");
    for (i = 0; i < SIZE; i++) {
        if (key_presses[buf[i]] == 0) {
            printf("%c\n", buf[i]);
        }
    }
}



void mostUsedKey(char buf[], char totalKeyboard[], int key_presses[]) {
    int i = 0;
    char mostUsed = 0;
    for (i = 0; i < SIZE; i++) {
        if (key_presses[buf[i]] > mostUsed) {
            mostUsed = buf[i];
        }
    }
    printf("The character you typed most is: %c \n", mostUsed);
}

Right now it prints out the value and how many times it appears, but not in descending order, and it also repeats values. It also prints the total number of values in the array before each row which I don't want.. Any advice to fix this function is much appreciated.

Nyako
  • 45
  • 2
  • 9
  • Perfect time to learn how to use the debugger. Compile with symbols, run the code inside a debugger to trace through the program line by line inspecting the values of the relevant variables to learn what is *really* going on. If then a *specific* question arises feel free to come back here. – alk Jul 22 '17 at 11:36
  • 1
    To provide some help with @alk good recommendation: https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Yunnosch Jul 22 '17 at 11:54
  • Dang, you try to make a good specific question and you still get snide remarks here. I'm not demotivated by you, but others who are really getting started in programming probably are unmotivated to learn when they need to risk answers worded like this. There is helpful advice, but delivered with sarcasm. – Nyako Jul 22 '17 at 11:59
  • 4
    This doesn't sound like sarcasm to me - if you have a problem to debug, then it really is the perfect time to to learn how to use a debugger. – Oliver Charlesworth Jul 22 '17 at 12:01

0 Answers0