How to get a percentage ?

Hi,

I had to write a program that determines the frequency of a letter specified by the user in an input file. I figured out how to do all that, however, we have to include the percentage of the total letters the user input letter was. I have in my code " percent = frequency/counter" however this doesn't work. Do I have to make a loop to find this out? Any help is appreciated !
Thanks

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


void totalLetters(FILE *filePtr, int *count, int *allCount, char letter);

int main ()

{

   char letter;
   int counter;
   int frequency;
   float percent;

   FILE *inFilePtr ;
   inFilePtr = fopen("file1.txt", "r");

   printf("Enter a letter:\n");
   scanf("%c", &letter);


totalLetters(inFilePtr, &frequency, &counter, letter);

printf("The letter %c appeared %d times out of a total of %d letters. \n", letter, frequency, counter);
percent = frequency/counter;
printf("%f  of the letters", percent);

fclose(inFilePtr);


return 0;
} /* end main */

/* count is a pointer to the count of the letter specified by the user
allCount is a pointer to the count of the total letters in the file
letter is the letter specified by the user */


void totalLetters(FILE *filePtr, int *count, int *allCount, char letter)
{                                        
     char c;
     int counter=0;
     int frequency=0;
     float percent;

if (filePtr == 0)
   printf("Could not open the file\n");
else {
        fscanf(filePtr, "%c", &c);

   while(!feof(filePtr)) {
       
     if (tolower(c) == letter)   {

        frequency++;       }
     if ((c >= 65 && c <91) || (c >= 97 && c < 123)) {   /* end if statement*/
     counter++;     }
  fscanf(filePtr,"%c", &c);
                }       /* end while loop*/

}                                  /* end if-else statement*/
   
*count = frequency;
*allCount = counter;


} /* end function totalLetters */
well what is a percentage? It's a value out of 100. So if counter is the total number of letters entered and frequency is the amount of times one particular letter appears,the percentage for each letter would be

100.0f / counter * frequency
Topic archived. No new replies allowed.