質問

I am making a small ANSI C application that uses pointers to sort int values using the bubble sort algorithm.

My main file:

#include "bubbleSort.h"

char Ar[] = "All Gaul is divided into three parts, one of which the Belgae inhabit, the Aquitani another, those who in their own language are called Celts, in our Gauls, the third. All these differ from each other in language, customs and laws. The river Garonne separates the Gauls from the Aquitani; the Marne and the Seine separate them from the Belgae. Of all these, the Belgae are the bravest, because they are furthest from the civilization and refinement of [our] Province, and merchants least frequently resort to them, and import those things which tend to effeminate the mind; and they are the nearest to the Germans, who dwell beyond the Rhine , with whom they are continually waging war; for which reason the Helvetii also surpass the rest of the Gauls in valor, as they contend with the Germans in almost daily battles, when they either repel them from their own territories, or themselves wage war on their frontiers. One part of these, which it has been said that the Gauls occupy, takes its beginning at the river Rhone ; it is bounded by the river Garonne, the ocean, and the territories of the Belgae; it borders, too, on the side of the Sequani and the Helvetii, upon the river Rhine , and stretches toward the north. From 'Caesar's Conquest of Gaul', Translator. W. A. McDevitte. Translator. W. S. Bohn. 1st Edition. New York. Harper & Brothers. 1869. Harper's New Classical Library. Published under creative commons and available at http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:1999.02.0001";

int main(void) {
    int ABStats[ALPHABET_SIZE] = { 0 };
    char chAlphabet[ALPHABET_SIZE] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

    char *pAr = Ar;
    char *pAlphabet = chAlphabet;
    int *pABStats = ABStats;

    GetFrequency(pAr, pABStats);
    int i, j;
    for (i = ALPHABET_SIZE-1; i >= 0; i--) {
        for (j = 0; j <= i; j++) {
            if (*(pABStats+j) < *(pABStats+j+1)) {
                Swap(pABStats+j, pABStats+j-1);
            }
        }
    }

    DisplayVHist(pABStats, ALPHABET_SIZE);
    return EXIT_SUCCESS; 
}

My bubbleSort.c file:

#include "bubbleSort.h"

int GetFrequency(char *pAr, int *pABStats) {
    int chNum = 0;
    for (; *pAr != '\0'; pAr++) {
        char ch = *pAr;
        if (isalpha(ch))
            chNum = (toupper(ch) - 'A');
        pABStats[chNum]++;
    }
    return chNum;
}

void DisplayVHist(int *pABStats, int size) {
    int i, j;
    const float lengthAr = strlen(Ar);
    for (i = 0; i < size; i++) {
        float chPercent = 100 * (*pABStats / lengthAr);
        printf("'%c' --> %6.3f percent --> %3d occurances --> ", (i + 'A'), chPercent, *pABStats);
        for (j = 0; j < (*pABStats / 2); j++) {
            printf("%c",'*');
        }
        printf("\n");
        pABStats++;
    }
}

void Swap(int *pA, int *pB) {
    int temp;
    temp = *pA;
    *pA = *pB;
    *pB = temp;
}

My header file:

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

#define ALPHABET_SIZE 26

extern char Ar[];

int GetFrequency(char*, int*);
void DisplayVHist(int*, int);
void Swap(int*, int*);

I would like the application to iterate through the array ABStats using the pointer pABStats and use the Sort() function inside bubbleSort.c to sort the values inside ABStats from highest occurrence to lowest occurrence. Everything so far is functioning properly except for my Sort() algorithm.

I am required to use a Bubble Sort algorithm by using the function Sort() inside the bubbleSort.c file. I also have to use pointers to reference the array, as opposed to using ABStats[i].

Does anybody have any advice on how to get Sort() to work? I'm fairly new to C programming. Thanks!

役に立ちましたか?

解決

This code:

for (j = 0; j <= i; j++) 
    if (*(pABStats+j) < *(pABStats+j+1))
        Swap(pABStats+j, pABStats+j-1);

Should read:

for (j = 0; j < i; j++) 
    if (*(pABStats+j) < *(pABStats+j+1))
        Swap(pABStats+j, pABStats+j+1);

Your for loop led to out of bounds array access. And you must swap the items that you compared.

I suspect there are other problems with the code but I don't have the energy to debug it all.

Some general points:

  1. Sorting could should be de-coupled from the data it operates on. Your sort is inlined into your main. That's not ideal. Take a look at the standard library sorting function to see how it should be done.
  2. Whilst your professor seems to think that *(a+i) is better than a[i] he/she is in a tiny minority. Try not to be misled by this advice. Prefer using the array indexing operator.

他のヒント

int GetFrequency(char *pAr, int *pABStats) {
int chNum = 0;
for (; *pAr != '\0'; pAr++) { /*check if at the end of the array*/
    char ch = *pAr; /*store current letter as a char*/
    if isalpha(ch) /*if character is a letter*/
        chNum = (toupper(ch) - 'A'); /*return ascii code of specified letter*/
    pABStats[chNum]++; /*store ascii value in array and increment array*/
}
return chNum;

} this makes sense, but then you bubble sort the pABStats array, you are loosing all the information.

suppose there were 10A's and 5B's in your sentence so pABStats[0] = 10 and pABstats[1] = 5, when you bubble sort this from small to large, pABstats[0] = 5 and PabStats[1] = 10. So now your program thinks there were 5A's and 10B's ?

I assume the array subscript runs from 0-25, which translates to subscript 0 is the number of A's and subscript 1 number of B's and so on.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top