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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:26:20+00:00 2026-06-11T06:26:20+00:00

I was working out this exercise and wanted to know if my answer is

  • 0

I was working out this exercise and wanted to know if my answer is correct;

Write a program to print a histogram of the frequencies of different
characters in the input.

I have seen a couple of other answers online but they differ quite a lot to mine. Also if there are any problems in the format of my code or any improvements to be made. Any suggestions are welcome. I do understad the question requires a histogram but it’s quite easy to build one once I have the data required.

#include <stdio.h>
int main(){

    int userInput;
    int arrayStuff[92];
    int i, j;

    for(i = 0; i < 92; ++i){
        arrayStuff[i] = 0;
    }

    while((userInput = getchar()) != '\n'){
        if(userInput >= 30 && userInput <= 122){
            if(userInput != '\n'){
                ++arrayStuff[(userInput-30)];
            }

            if(userInput == '\n'){
                break;
            }
        }
    }

    printf("Case\t|\tOccurances\n");

    for(i = 0; i < 92; ++i){
        printf("%c\t|\t%d\n", (i+30), arrayStuff[i]);
    }
}
  • 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-06-11T06:26:22+00:00Added an answer on June 11, 2026 at 6:26 am

    Improvements I’d make:

    • Declare and initialize int arrayStuff[92] = { 0 }; in one go and get rid of the for loop. This is guaranteed to set all elements to 0.
    • Don’t use the magic number 92 in various places, use (sizeof arrayStuff/sizeof arrayStuff[0]) instead to compute the number of elements in arrayStuff.
    • Parentheses in (i+30) are redundant
    • What happens if the user types the EOF character? It looks like it loops forever.
    • “Occurances” is not in my dictionary, but occurrence is. Maybe character frequency is a better term?
    • arrayStuff? I cannot overemphasize the importance of clear and concise identifier naming. Frequency comes to mind.
    • It must be int main (void) with a return 0;. This is not C++, where an empty parameter list is equivalent to void. This is C, where an empty parameter list means “I’m an old-style K&R parameter list for an unknown but fixed number of arguments.”
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't figure out why this isn't working, and I know it's because of
Hi I am having a little difficulty working out this mod rewrite rule /
I am tired of working this out myself , i cannot understand the way
I am tired to find out why this simple query not working. SELECT image_url
I can't quite figure out why this Linq Statement isn't working as i would
I've been working very long on trying to straighten this out, but I just
I have looked all over and cannot figure out why this code isn't working.
I'm working with SQL, and I can't seem to figure this out for the
i can't figure out about working with I/O. I am using this code to
I used this script to create a jQuery accordion. Check out the working jsFiddle

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.