-2

the goal of this code is to display the frequency or how much each letter are being use in the input but as I click compile there are an erroe show that

main.c:50:19: error: invalid operands to binary * (have 'float *' and 'int')"
    avg = freq*100/count;    

Code

#include <stdio.h>
#include <ctype.h>
#include <float.h>

int main()
{
    char alp;
    float count, i, avg;
    float freq[26];


    printf("Enter the alphabet : ");
    scanf("%c", &alp);
    do{
        printf("Enter the alphabet : ");
        scanf("%c", &alp);

        switch(alp) {
            case 'a' :freq[0]++; break;
            case 'b' :freq[1]++; break;
            case 'c' :freq[2]++; break;
            case 'd' :freq[3]++; break;
            case 'e' :freq[4]++; break;
            case 'f' :freq[5]++; break;
            case 'g' :freq[6]++; break;
            case 'h' :freq[7]++; break;
            case 'i' :freq[8]++; break;
            case 'j' :freq[9]++; break;
            case 'k' :freq[10]++; break;
            case 'l' :freq[11]++; break;
            case 'm' :freq[12]++; break;
            case 'n' :freq[13]++; break;
            case 'o' :freq[14]++; break;
            case 'p' :freq[15]++; break;
            case 'q' :freq[16]++; break;
            case 'r' :freq[17]++; break;
            case 's' :freq[18]++; break;
            case 't' :freq[19]++; break;
            case 'u' :freq[20]++; break;
            case 'v' :freq[21]++; break;
            case 'w' :freq[22]++; break;
            case 'x' :freq[23]++; break;
            case 'y' :freq[24]++; break;
            case 'z' :freq[25]++; break;
            default :;}
        count++;
    }while (alp==-1);

    for( i = 0; i < 26; i = i + 1 ){
        avg = freq*100/count;
        printf("%c : %f", i+65, avg);

    }


    return 0;
}

by the way I'm not a computer science student so if I make a stupid mistake please don't judge me.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Mizery 26
  • 1
  • 1

4 Answers4

1

You have this code...

avg = freq*100/count;

...freq is an array so it should be

avg = freq[i]*100/count;
Chris Turner
  • 8,082
  • 1
  • 14
  • 18
1

You are missing the array index in the following line:

avg = freq*100/count;

It should be:

avg = freq[i]*100/count;

Array pointer cannot be multiplied :)

Liran Funaro
  • 2,750
  • 2
  • 22
  • 33
1

here freq is array of float numbers. array name is a pointer to memory location of first position i.e. freq points to freq[0].

therefore when you use "avg = freq*100/count;" the freq is (float*) and it expects float. so use freq[i]

mangupt
  • 369
  • 2
  • 11
0

There are multiple errors in this code: 1) you use unitializated vars:

float count=0, avg=0;

2) avg is an array

 avg = freq[i]*100/count;

3)i is a float, but you want a integer (or just a char)

char i=0;

4) while(apl==-1) is not correct, the do while will always terminated(excep when there will be an error. use

while(apl!=-1)
Riccardo Bonafede
  • 610
  • 1
  • 9
  • 17
  • the code can be run but the number that shows the frequency are display strangely Ex: enter the alphabeth:werfgyyjjjjkkklpo but the number still display as zero – Mizery 26 May 12 '17 at 16:35
  • there is another error: while (alp==-1); i think you would like to have while(alp != -1) – Riccardo Bonafede May 12 '17 at 16:40