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

This is the prompt... This problem consists of writing a function that computes

ID: 3628264 • Letter: T

Question

This is the prompt...

This problem consists of writing a function that computes a histogram. The function has two arrays as a parameter. Each array is passed to the function along with the “length” of that array. The first array is the data array. This array is the input to your function, and contains the values for which you should compute a histogram. The second array is for your output, i.e., where you should put the result of your histogram. You can assume that the largest value stored in the data array is less than the length of the histogram array. For example, if the histogram array has a length provided of 100, then the largest value in the data array will be less than 100 (i.e., 99 or smaller). To complete this, write the function that computes the histogram of the input array and stores the result in the output array.


This is the code I've written so far....

#include <stdio.h>

void computeHistogram(int data[], int data_size, int histo[], int histo_size) {


//Variables
int OccurrenceCounter = 0; //Will track the number of times a number is found in the arrayint MatchingValue = (data_size - 1); //The variable that each number in the array will be compared to on each run through
int OriginalSize = data_size; //Used to reset counters at the end
int TotalCounter = data_size; //Used to count the number of total scans through the data array

while(TotalCounter != 0){ //Will hit zero when the data array has been completely scanned X number of times
//Data Array logic
while(data_size != 0){ //Hits zero after going through all of the data points in the array
data_size = (data_size - 1);
if(MatchingValue == data[data_size]){ //Increment the OccurrenceCounter for each match OccurrenceCounter = (OccurrenceCounter + 1);
}
}

//Histo Array logic
histo[histo_size] = OccurrenceCounter; //Store the occurence of the value being searched for into the histogram
histo_size = (histo_size - 1); //Make room for the next piece of data
TotalCounter = (TotalCounter - 1);
MatchingValue = (MatchingValue - 1);
OccurrenceCounter = 0; //Reset the counter to wipe old data
data_size = OriginalSize;
}
}

Sorry if the formatting is a little weird, it was a pain pasting this into the question box.

Anyways, this is my first time working with C, and my TA told me I've been going about writing this histogram the wrong way, and I can do it with fewer variables and fewer code, I've been trying to figure out how to write it a more efficient way but I'm coming up blank. Could somebody please help? Will give an A+ rating

Explanation / Answer

All you really need to do is to loop through the data array and use the value in that element of the array to index into the histogram array. At the histogram index, increment the value. void computeHistogram(int data[], int data_size, int histo[], int histo_size) { for( int dataIndex = 0; dataIndex
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