Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Memory and Structs (C Language) Learning Outcomes and Introduction In this lab assignment you will learn about: reserving memory using malloc and/or calloc managing resizable arrays using realloc releasing memory using free handling input with no pre-determined size constraints using structs to organize data Task 1: Longest Words Create a program that finds the longest words in the input

Memory and Structs (C Language) Learning Outcomes and Introduction In this lab assignment you will learn about: reserving memory using malloc and/or calloc managing resizable arrays using realloc releasing memory using free handling input with no pre-determined size constraints using structs to organize data Task 1: Longest Words Create a program that finds the longest words in the input

Computer Science

Memory and Structs (C Language)

Learning Outcomes and Introduction

In this lab assignment you will learn about:

  • reserving memory using malloc and/or calloc
  • managing resizable arrays using realloc
  • releasing memory using free
  • handling input with no pre-determined size constraints
  • using structs to organize data

Task 1: Longest Words

Create a program that finds the longest words in the input.

Your program should accept a filename as a command-line argument. If provided, input will be read from the named file. Otherwise, input will be read from the standard input stream.

The input will consist of a mix of letters, numbers, whitespace, punctuation, and other symbols. Your program should consider only consecutive sequences of one or more latin letters (A-Z, a-z) as words, and treat everything else as word separators. For example, "goodbye2you forevè" would be interpretted as three words: "goodbye", "you", and "forev".

The end goal is to collect the list of unique words of the same length that are longer than all other words in the input. For example, if the longest word in the input is eight letters long, collect the list of every eight-letter word, ignoring duplicates. Words that differ only in letter case should be considered equivalent. Print the longest words found (in any order) to standard output.

Make use of malloc, calloc, and realloc to allow your program to handle inputs of any size. Specifically, do not make any assumptions about the maximum length of any one word, the number of unique words that will be found, the maximum line length, or maximum number of input lines. Do not attempt to store the entire file's content in memory at once. Make use of free to minimize the amount of memory used by your program as it is running. Do not retain words in memory that are known to not be among the longest in the input.

If your program is unable to complete processing its input because it cannot reserve more memory (eg. malloc returns NULL), report the error and then print the list of longest words found so far.

Suggested approach

There are many ways to manage a dynamicly changing list of data. For simplicity, I recommend storing each word in an appropriately-sized chunk of memory allocated with malloc, and keep track of the list of words with an array of pointers (also allocated with malloc). The array should start relatively small, but may need to be resized with realloc as the program progresses. Keep track of how large the array currently is in one variable, and how much space is used so far in another. When you run out of room, resize the array to provide twice as much space.

Task 2: Math with Units

Create a calculator program that is able to add, subtract, multiply, and divide two measurements with units.

  1. Start by creating a structure type to represent a measurement, and a function that creates a value of this type from an input string.

Valid input values will consist of a real decimal number in fixed-point notation, followed by one or more spaces, followed by a unit name. Unit names are composed of an optional SI prefix letter, followed by one or more letters identifying the base unit.

Example meansurement strings

    • 3.2 g
    • 1000 km
    • 4.3333 in
    • -3.0 hours
    • 5 GPa
  1. Note: base units must always be at least one letter long. Thus 3 m is already normalized despite "m" looking like an SI prefix.

Create a function that accepts a measurement value and returns a "normalized" version that does not use SI prefixes in its unit name but has the same value. For example, given a measurement representing 3 kg, the function should produce the measurement 3000 g.

SI prefix values

Your code should support at least the following SI prefix values:

Symbol

 Factor

T

1012

G

109

M

106

k

103

c

10-2

Symbol

 Factor

m

10-3

u

10-6

n

10-9

  1. Create addition, subtraction, multiplication, and division functions that each take in two measurement values and calculate the resulting measurement.

These functions should use the normalization function described above on their input values before performing the operation.

The addition and subtraction functions should report an error if the normalized inputs are different units.

The multiplication and division functions should accept measurements of any unit and produce a measurement with a composite unit. For example, multiplying 3 km by 4 g should produce 12000 m*g, and dividing 4 km by 2 ks should produce 2 m/s. Your code does not need to handle these composite units as inputs.

Division by zero should report an error.

  1. Lastly, add code to your main function to read in two measurements and an operator (one of +, -, *, or /) as input, and print the result as output. Provide helpful error messages in response to invalid inputs.

Option 1

Low Cost Option
Download this past answer in few clicks

9.99 USD

PURCHASE SOLUTION

Already member?


Option 2

Custom new solution created by our subject matter experts

GET A QUOTE