Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7435675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:06:52+00:00 2026-05-29T10:06:52+00:00

I’m actually writing about the same program as before, but I feel like I’ve

  • 0

I’m actually writing about the same program as before, but I feel like I’ve made significant progress since the last time. I have a new question however; I have a function designed to store the frequencies of letters contained within the message inside an array so I can do some comparison checks later. When I ran a test segment through the function by outputting all of my array entries to see what their values are, it seems to be storing some absurd numbers. Here’s the function of issue:

void calcFreq ( float found[] ) 
{
    char infname[15], alpha[27];
    char ch;
    float count = 0;
    FILE *fin;

    int i = 0;
    while (i < 26) {
        alpha[i] = 'A' + i++;
    }

    printf("Please input the name of the file you wish to scan:\n");
    scanf("%s", infname);

    fin = fopen ( infname, "r");
    while ( !feof(fin) ) {
        fscanf(fin, "%c", &ch);
        if ( isalpha(ch) ) {
            count += 1;
            i = 0;
            if ( islower(ch) ) { ch = toupper(ch); }
            while ( i < 26 ) {
                if ( ch == alpha[i] ) {
                    found[i]++;
                    i = 30;
                }
                i++;
            }
        }
    }
    fclose(fin);
    i = 0;
    while ( i < 26 ) {
        found[i] = found[i] / count;
    printf("%f\n", found[i]);
        i++;
    }
}

At like… found[5], I get this hugely absurd number stored in there. Is there anything you can see that I’m just overlooking? Also, some array values are 0 and I’m pretty certain that every character of the alphabet is being used at least once in the text files I’m using.

I feel like a moron – this program should be easy, but I keep overlooking simple mistakes that cost me a lot of time >.> Thank you so much for your help.

EDIT So… I set the entries to 0 of the frequency array and it seems to turn out okay – in a Linux environment. When I try to use an IDE from a Windows environment, the program does nothing and Windows crashes. What the heck?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:06:53+00:00Added an answer on May 29, 2026 at 10:06 am

    Here are a few pointers besides the most important one of initializing found[], which was mentioned in other comments.

    the alpha[] array complicates things, and you don’t need it. See below for a modified file-read-loop that doesn’t need the alpha[] array to count the letters in the file.

    And strictly speaking, the expression you’re using to initialize the alpha[] array:

    alpha[i] = 'A' + i++;
    

    has undefined behavior because you modify i as well as use it as an index in two different parts of the expression. The good news is that since you don’t need alpha[] you can get rid of its initialization entirely.

    The way you’re checking for EOF is incorrect – it’ll result in you acting on the last character in the file twice (since the fscanf() call that results in an EOF will not change the value of ch). feof() won’t return true until after the read that occurs at the end of the file. Change your ch variable to an int type, and modify the loop that reads the file to something like:

    // assumes that `ch` is declared as `int`
    
    while ( (ch = fgetc(fin)) != EOF ) {
        if ( isalpha(ch) ) {
            count += 1;
            ch = toupper(ch);
    
            // the following line is technically non-portable, 
            //  but works for ASCII targets.
            // I assume this will work for you because the way you
            //  initialized the `alpha[]` array assumed that `A`..`Z`
            //  were consecutive.
    
            int index = ch - 'A';
    
            found[index] += 1;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.