Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Topics: Multiple file programs, Makefiles, and Arrays in C. For this assignment,

ID: 3889888 • Letter: T

Question

Topics: Multiple file programs, Makefiles, and 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 only the digit [ '0' – '9' ] characters that occur in it.

Requirements:

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

You must write the corresponding histo.c file for the following histo.h file:

#define NUM_DIGITS 10 // number of unique “digit” characters

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

void init_histogram(int histo[]); // set all elements of the histogram to zero

void cons_histogram(const char string[], int histo[]); // construct the histogram from string

void most_frequent(const int histo[], char* ret_val); // set *ret_val to a most occurring digit        

                                                            character – hence returning it

void display_histogram(int* const histo); // display the histogram sparsely

Your histo.c file must also work with the following main.c file:

#include <stdlib.h>

#include <stdio.h>

#include "histo.h"

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

{

int histo[NUM_DIGITS];

if (args == 2)

{

    init_histogram(histo);

    cons_histogram(argv[1], histo);

    display_histogram(histo);

}

else

    exit(1);

return 0;

}

You must write an appropriate Makefile

Once complete and working, your program must produce output “very” similar to:

Sample Runs:

% ./main h3j5j7k8fjksk8

3 appeared 1 time

5 appeared 1 time

7 appeared 1 time

8 appeared 2 times

8 occurred most often

Notes:

Only those characters that occurred at least once are reported, and only those that occurred more than one are reported as plural.

The maximal 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 to a byte to be safe.

Explanation / Answer

main.c
------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include "histo.h"

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;

}


--------------------------------------------------------------
histo.c
-------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include "histo.h"
#include <string.h>

// set all elements of the histogram to zero
void init_histogram(int histo[]){
    int i;
    for (i = 0; i<256; i++) {
        histo[i] = 0;
    }
}

// construct the histogram from string
void cons_histogram(const char string[], int histo[]){
    int length_of_string = strlen(string);
    int i;
    for (i = 0; i < length_of_string; i++) {
        histo[i] = (int)string[i];
    }
}

// report a most occurring character
void most_frequent(const int histo[], char* ret_val){
    int counter[256] = {0};
    int i;
    for (i = 0; i < 256; i++) {
        counter[histo[i]]++;
    }

    int max = 0;
    char character = 0;
    for (i = 1; i < 256; i++) {
        if (max < counter[i]) {
            max = counter[i];
            character = (char)i;
        }
    }

    *ret_val = character;

}

// display the histogram sparsely
void display_histogram(const int histo[]){
    char most_common;

    most_frequent(histo, &most_common);

    int counter[256] = {0};
    int i;
    for (i = 0; i < 256; i++) {
        counter[histo[i]]++;
    }

    for (i = 1; i < 256; i++) {
        if (counter[i] > 0) {
            printf("%c appeared %d times ", (char)i, counter[i]);
        }
    }

    printf("Most common char is %c ", most_common);

}
----------------------------------------------------------------------------------
histo.h
-------------------------------------
#ifndef hw2_histo_h
#define hw2_histo_h

#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[]); // set all elements of the histogram to zero

void cons_histogram(const char string[], int histo[]); // construct the histogram from string

void most_frequent(const int histo[], char* ret_val); // report a most occurring character

void display_histogram(const int histo[]); // display the histogram sparsely

#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote