Question
Run C in Linux Computing a histogram version 2.0 For this, you will be creating a modified version of your histogram program from the last
Run C in Linux
Computing a histogram version 2.0
For this, you will be creating a modified version of your histogram program from the last lab code. Make sure you start by making a copy of your lab1 code .: code link: https://www.chegg.com/homework-help/questions-and-answers/c-language-linux-computing-histogram-lab-computing-histogram-specifically-loading-set-quiz-q43838184?trackid=NpDyOmbb
Using a typedef
The first change you should make is to replace the use of "struct freq" in your code with a typedef alias of "Histogram".
Mod of readScores
The old version of this function took two parameters. One was an array that was already created (in the main) which was to be filled in by the function and the other was the count that was to be "returned" through the parameter. You are to change this so that the array is now created on the heap in the function and then passed back as the return value. The count should still be handled the same way.
Mod of calcHistogram
The old version of this function took in a pre-created array and its count (it also took in the scores information). You are to change this so that the count information is passed back through the return value rather than a parameter. And the array should be created on the heap from within the function and passed back through a parameter. This is the opposite of what I wanted for readScores - just to give you practice with doing it both ways.
Freeing memory
Once done using the heap memory you should make sure you free it since you do not have a garbage collector in C. You can use the program valgrind to help determine if you have any memory leaks. Valgrind is installed on linux*.cs.du.edu. You can install it on WSL. If you are using a Mac, it doesn't look like you can installed it on anything past HighSierra at the moment. So if you are coding locally on a Mac without valgrind, you should make sure you make a copy of your code to the department servers and compile and test there with valgrind. To copy to the servers use the secure copy command :
scp lab2.c yourLoginName@linux1.cs.du.edu:lab2.c
Reading in words
Lastly you should modify your code so that it handles words instead of numbers. First make a copy of your working number code in case I wish to look at the integer version. This modification will involve changes to your code in many different places. The first thing you should note is that to read or write a "string" you use the %s argument in scanf/printf. Second, for the "string" type you should simply use a char*. That is an array of chars is a "string", often referred to as a CString. Note that just like in the int case, scanf will need a place to put the string it is reading. It might also prove useful to use the function strcmp(str1, str2) to compare two strings together. This function is defined in the string.h header file.
Make sure you allocate memory for your strings. You didn't have to do this for numbers since they were just primitives, but you will for strings. In particular, for your array of words, I would like you to allocate the exact amount of memory necessary for each word. That is, if the word is 'test' you should allocate 5 bytes exactly for it. You can still allocate space for up to 100 words in advance.
A good test file to use is:
this is a test this is only a test the preceding test was also a test
Array notation
To further get used to the dual nature of [ ] array notion and the pointer counterpart, you should go through your code and change all array [ ] access to pointer based. For example, arr[5] should be changed to *(arr + 5). You should still use [ ] if you need to create any arrays, but only in creation.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started