Lab 03: intro to arrays and structsWork to complete these exercises in lab today. Whatever you do not finish in the lab, try to complete by the start of lecture on Wednesday. This repository contains a few sample C++ files that you will modify for this lab assignment. The work will give you practice with two primitive data structures in C++, arrays and structs. These each can be used as the low-level building blocks of other data structures. They are the low-level components of the library object classes that come with C++, and of ones ones that we will invent ourselves. ArraysArrays allow you to construct a fixed-size sequence of data values,
all of the same type. Each item of an array can be accessed by its
index, numbered from 0 up to, but excluding, the size of that array. For
example, the statement below builds an array of 5 items, each of type
The contents of the array are allocated, but the actual values of
each are indeterminant. So we need to then explicitly give each array
item a value. The
The array built above is a stack-based array. That means that its storage lives alongside the local variables of the function that uses it. That also means that the array’s lifetime is the lifetime of that particular function’s call. Because it is allocated on the stack, we require that the size of the array be an integer constant. Though C++ allows arbitary-sized stack-allocated arrays, most expert programmers discurage such use of them. There is an alternate syntax to the above that allocates the array, initializes its contents, and names its variable in a single statement. Below is an example
The info on the right hand side of It is possible to pass arrays as parameters to functions and procedures, but we need to limit that idea for this lab. In full generality, it requires an understanding of pointers, something we’ll learn in Wednesday’s lecture. We’ll also talk about more complex allocation of arrays, including using the heap memory instead of the stack memory. This will give us longer lifetimes for arrays, but it also requires us to understand pointers. So here we’ll mostly work on arrays within StructsStructs are like primitive object classes. They are just a collection
of named data items—the struct’s fields. Since different
structs have different collections of field names and field types, then
each struct has to have its own type, describing those. For example,
below is a definition of a new
The above definition tells C++ that a Once defined, you can then have variables of type
This allocates space for a string, followed by an integer, followed by a boolean, onto that function’s stack frame. The contents of each are indeterminate since there is no initializer, and so the code might then set each of the fields with assignment statements, like so:
It is also possible to use initializer lists for structs, too. Below
we do that for a new struct variable
Structs can be passed as values to functions, and returned as values from functions. When a struct is passed to a function, C++ copies each field of the struct into another struct, the parameter struct of the called function. This means that a function like
called with
called with Array ExercisesExercise 1. the mean value Modify the code So, if the user enters the three items 5, 12, and 4, then the program should report
because that is the average (5+12+4)/3. Since the size of the data is an
This yields a value of type Exercise 2. the minimum value Now add a section of the code that computes the minimum value of the data in the array and reports it, like so:
Exercise 3. mean and minimum functions Restructure the code that you just wrote so that it instead computes
the mean value of an array of data using a function named
For now, don’t worry about the type Now modify
Do the same for the minimum value calculation: Write a similar
function named Struct ExercisesExercise 4. car In the source file named
Write a C++ function
In the end, you should have a working Exercise 5. complex product In lab we just demonstrated a program Add a function and its use in Note that the product of a + bi with c + di (because of “FOIL”) has a real part equal to ac - bd and an imaginary part equal to ad + bc. |