I have an assignment to write a program that calculates how many of each character an imput contains and list the characters according to frequency greater to lesser. I thought I might have figured it out by modifying a piece of code from the textbook decribing array arguments, but when I attempted to run the program it aborted. Would someone please tell me what I need to do differently?
Code:
#include <stdio.h>
#include <stdlib.h>
# define N 52
int find_largest(int a[], int n);
int find_largest(int a[], int n)
{
int i, max;
max = a[1];
for (i = 1; i < n; i++)
if (a[i] > max)
max = a[i];
return max;
}
main()
{
int a[N];
int count = 0;
int i = 0;
int largest;
int *b;
float percent;
char ch;
for (i = 0; i < 53; i++)
a[i] = 0;
printf ("Enter a line of text: ");
ch = getchar();
while (ch != '\n')
{
count++;
if (ch == ' ')
a[0]++;
if (ch >= 'A' && ch <= 'Z')
{
i = ch;
a[i - 64]++;
}
if (ch >= 'a' && ch <= 'z')
a[ch - 70]++;
ch = getchar();
}
printf ("\n\nFREQUENCY TABLE\n---------------\nChar Count %% of Total \n");
printf ("---- ----- ---------- \nALL %d 100.00%%\n", count);
largest = find_largest(b, N);
for (i = 0; i < 53; i++)
{
if (a[i] != 0)
{
if (i == 0)
ch = ' ';
else if (i >= 1 && i <= 26)
ch = i + 64;
else
ch = i + 70;
percent = (float)(a[i]) / (float)(count) * 100;
printf ("\"%c\" %d %.2f%%\n", ch, b, percent);
}
}
system("pause");
return 0;
}