Question

Topics: Arrays in C. For this assignment, you will write a C program that uses its...

Topics: Arrays in C.

For this assignment, you will write a C program that uses its first command line parameter to compute and display a histogram of characters that occur in it.

Requirements:

Your program must compile and run correctly using the gcc compiler on ale.

You must write the corresponding function definitions for the following function prototypes:

// set all elements of the histogram to zero

void init_histogram(int histo[]);

// construct the histogram from string

void cons_histogram(char string[], int histo[]);

// display the histogram to the screen in a “nice” format

void display_histogram(int histo[]);

Your functions must work with the following program:

#include

#include

#define MAX_CHAR 255 // largest ASCII(Extended) value for characters

typedef unsigned char byte; // may be useful for casting(s)

void init_histogram(int histo[]);

void cons_histogram(char string[], int histo[]);

void display_histogram(int histo[]);

int main(int args, char *argv[])

{

int histo[256];

if (args == 2)

{

init_histogram(histo);

cons_histogram(argv[1], histo);

display_histogram(histo);

}

else

exit(1);

return 0;

}

void init_histogram(int histo[])

{

// your code here

}

void cons_histogram(char string[], int histo[])

{

// your code here

}

void display_histogram(int histo[])

{

// your code here

}

Outline:

Create / open a .c file using pico, in your UNIX account

Write the necessary C statementss to meet the game specification given above

Make sure to test your “program” when it is done

You really need to run your program a number of time to do this thoroughly

Notes(s):

Only those characters that occurred at least once are reported.

The minimal occurrence may very well not be unique.

The value of a char variable is an integer value and hence can be used as an index into the histogram – once casted as unsigned to be safe.

Sample Run(s):

% ./a.out hgfjkddjkrui3

3 appeared 1 time

d appeared 2 times

f appeared 1 time

g appeared 1 time

h appeared 1 time

i appeared 1 time

j appeared 2 times

k appeared 2 times

r appeared 1 time

u appeared 1 time

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_CHARS 255

typedef unsigned char byte;

void init_histogram(int histo[]);
void cons_histogram(char string[], int histo[]);
void display_histogram(int histo[]);

int main(int argc, char const *argv[])
{
   if(argc < 2){
       printf("Please pass commanf line argument\n");
       return 1;
   }
  
   char input[40];
   strcpy(input, argv[1]);
   int histo[MAX_CHARS];

   init_histogram(histo);

   cons_histogram(input, histo);

   display_histogram(histo);

   return 0;
}

void init_histogram(int histo[]){

   int i;

   for(i=0; i<MAX_CHARS; i++)
       histo[i] = 0;
}

void cons_histogram(char string[], int histo[]){
   int i;
   for(i=0; i<strlen(string); i++){

       int index = (int)string[i];
       histo[index]++; // increasign count
   }
}

void display_histogram(int histo[]){

   int i;
   //printf("Char\t\tCount\n");
   for(int i=0; i<MAX_CHARS; i++){
       if(histo[i] > 0){
       char c = (byte)i;
       printf("%c appeared %d time\n", c, histo[i]);
       //printf("%c\t\t%d\n", c, histo[i]);
   }
   }
}

-flrstweek firstweek firstweek gcc histogram.c firstweek ./a.out hgfjkddjkrui3 3 appeared 1 time d appeared 2 time f appeared

Add a comment
Know the answer?
Add Answer to:
Topics: Arrays in C. For this assignment, you will write a C program that uses its...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • Write a C++ program to display workers schedule for a company. To accomplish this (1) Write...

    Write a C++ program to display workers schedule for a company. To accomplish this (1) Write a struct Time with attributes (at least) : hours, minutes and seconds. Add other functions or attributes you need to get the job done (2) Write a class Schedule with attributes : Day of week, start_time, end_time and activity. Schedule should have at least (a) Display to display the schedule (b) Length to return the duration of the schedule (ie end_time - start_time), (c)...

  • Background: For this assignment, you will write a small encryption utility that implements a simple encryption...

    Background: For this assignment, you will write a small encryption utility that implements a simple encryption algorithm described below. The program will take one command line argument as an input; this will represent the word which is to be encrypted. As an output, your program will print the encrypted version of the word to the console using a simple printf() statement. This is the only output your program needs to produce. There is an important catch, however: your program is...

  • ANSWER ASAP PLEASE IN C Duplicate the program below to a new Program 2, and modify...

    ANSWER ASAP PLEASE IN C Duplicate the program below to a new Program 2, and modify Program 2 such that it generates n random virtual addresses between 0 and 232-1 and computes the page number and offset for each address. Here, do not write to the console. Instead, run your program with n = 1000000 random virtual addresses and compute the total CPU time of the task. Report your findings, as well as how you ran your program. Provide commentary...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

  • 6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you...

    6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered. Test cases are available in this document. Remember, in addition...

  • MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random...

    MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random words from a training set as explained in the lectures on graphs. Do not hard-code the training set! Read it from a file. A suggested format for the input file: 6 a e m r s t 10 ate eat mate meet rate seat stream tame team tear Here are some suggestions for constants, array declarations, and helper functions #include <iostream> #include <fstream> #include...

  • In C++ write a program that will read three strings from the user, a phrase, a...

    In C++ write a program that will read three strings from the user, a phrase, a letter to replace, and a replacement letter. The program will change the phrase by replacing each occurrence of a letter with a replacement letter. For example, if the phrase is Mississippi and the letter to replace is 's' the new phrase will be "miizzizzippi". in the function, replace the first parameter is a modifiable string, the second parameter is the occurrence of a letter...

  • // Write a program that determines how many of each type of vowel are in an...

    // Write a program that determines how many of each type of vowel are in an entered string of 50 characters or less. // The program should prompt the user for a string. // The program should then sequence through the string character by character (till it gets to the NULL character) and count how many of each type of vowel are in the string. // Vowels: a, e, i, o, u. // Output the entered string, how many of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT