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 6807963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:53:48+00:00 2026-05-26T19:53:48+00:00

The below code reads in a file and then counts the number of ‘a’,

  • 0

The below code reads in a file and then counts the number of ‘a’, ‘b’, etc… characters, it counts both ‘A’ and ‘a’ as the same letter.

The code below works; however if I change the declaration of the int array count[26] = {0} to count[25] = {0} (and ONLY change this), the program will not properly store the number of ‘z’ characters, which is stored in the 25th element of the array. It will store -1 in the 25th element. I don’t understand why? I never access count[26], only count[0] through count[25], but why must I add one more element to the array for the code to work? When count is 26 elements long the correct number of ‘z’ characters is stored in count[25].

int main (int argc, char *argv[], char **env)
{
// Declare variables

int c, big ;
int count[26] = {0} ;
int bar[25] = {0} ;
ifstream infile("histo.data") ;

while (!infile.eof())
{

        c = infile.get() ;
        c = tolower(c) ;

      if ( c >= 97 && c <= 122)
      {
         count[c - 'a']++ ;
      }

}
infile.close() ;

for (int i = 0; i <= 25; i++)
{
    printf("%c: %d\n",(97+i),count[i]) ;
}


// find largest letter count
big = count[0] ;

for (int i = 1; i <= 25; i++)
{

    if(count[i] > big)
    {
      big = count[i] ;
    }
}

// bar lengths
int cols  = atoi(getenv("COLUMNS")) ;
for (int i = 0; i <= 25; i++)
    {
        bar[i] = (cols-3)*count[i]/big ;
    }

// print bars

for (int i = 0; i <= 25; i++)
{
    printf("\n%c: ",(97+i));
    for (int j = 1; j <= bar[i]; j++)
    {
        cout << "=" ;
    }
}

cout << "\n" << endl ;
}
  • 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-26T19:53:49+00:00Added an answer on May 26, 2026 at 7:53 pm

    Arrays are zero indexed, however the number you declare them with is the size of the array. Therefore, if you want 26 items, you need to declare an array of 26. The index range is 0…25 though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code given below reads file contents to buffer, and then does something with
I have my code below. It correctly reads my sqlite database file(that i have
I am testing this simple code below and it works... $(document).ready( function() { $('.show-modal').click(
I have the below code to run a java program from a file. import
I am using the code below to upload a file. The files upload without
Below is the code I am using. It reads links from a textarea, and
I have an XML file that is pretty long. Below is the code I
I am running below code via eclipse in windows and then in Unix as
I am using jquery datepicker, please see below code $(document).ready(function() { $(.txtDate).datepicker({ showOn: 'button',
I use the below code to create and read my database in the app

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.