Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*C++* Part 1: The goal of this assignment is to gain practice with arrays: how to declare (and initialize) arrays, how to access array values,

*C++*

Part 1:

The goal of this assignment is to gain practice with arrays: how to declare (and initialize) arrays, how to access array values, and how to store values in arrays.

Collections of Things

To date, you've focused mostly on working with data one "piece" at a time. But in the real world, we're often concerned with collections of data: scores, hourly air temperatures, pressure readings over time, and colors of socks. In computer science, we call these collections "data structures" -- things that contain data. Perhaps the most simple data structure is the array: a container that holds multiple instances of only one type of thing. For example, you might have an array called scores that contains a bunch of ints , or an array called windSpeed that contains a bunch of doubles , or an array called favCharacters that contains a bunch ofchars .

There are a couple of "rules" you need to remember when dealing with arrays in C++.

First, arrays can only store values of one specific type, so you can't store both cats and dogs in the same array. Sometimes you can "bend" this rule, depending on type compatibility and coercion (e.g., you can store both int s and double s in an array of type double ), but typically you don't do this. Here's an example of an array of characters.

char lettersInAlphabet[26]; // rule1: can only store char values

Second, arrays have a "fixed size". This means that you must tell the computer how many elements you intend to store in an array when you declare said array, and you can never store more than that number of items in the array.

char lettersInAlphabet[26]; // rule2: can only store 26 values

Why does the machine need to know how many elements you intend to store in the array? In a nutshell, the machine needs to know in order to allocate enough memory for N pieces of data of a given type. In the example above, the machine needs to allocate enough memory to store 26 char values.

Third, you need to remember that arrays are "zero-indexed" meaning the first item in the array is "item 0" and the second is "item 1" and so on. This is important to remember, because it is easy to confuse the sizeof the array with the last index of the array. If you initialize an array like this:

int shotsOfWheatgrassDrunkThisWeek[7]; // rule3: what are the valid indices? 0 through 6.

Notice how the size of the array is 7, but the last index is 6.

C++ Array Syntax

You should have seen some examples of array syntax using [ and ] in class and in your zyBook activities. As such, we won't go over the syntex in detail here. However, here's a brief summary.

int x = 1; int y = 2; int z; int scores[10]; scores[1] = 27; scores[y] = 2; z = scores[x] + scores[y]; cout << scores[x + 1];

Please pause here until you totally understand what the syntax means in the example above. How many numbers can score hold? 10. What are their "names"? scores[0] through scores[9] . What is the ending value of z? 29. What is printed to the screen? 2. What does the array look like in memory?

27 2
0 1 2 3 4 5 6 7 8 9

We suggest you review your section's lecture and zyBook reading, and come see us if you do not understand the syntax in the previous example.

Arrays and Loops

Since you're aware of how to use loops and since you now know how to create arrays, you should recognize that there is a lot of hidden "power" in combining these two elements together. In fact, loops and arrays work so well together, it's rare you'll see one without the other -- they're best friends (but only friends, mind you). Let's take a look:

const int DAYS_IN_WEEK = 7; int shotsOfWheatgrass[DAYS_IN_WEEK] = {0, 1, 1, 1, 1, 7, 5}; for (int i = 0; i < DAYS_IN_WEEK; ++i) { cout << "On day " << i << " I drank " << shotsOfWheatgrass[i] << " shots." << endl; }

Or better yet,

const int DAYS_IN_WEEK = 7; int shotsOfWheatgrass[DAYS_IN_WEEK] = {0, 1, 1, 1, 1, 7, 5}; for (int i = 0; i < DAYS_IN_WEEK; ++i) { cout << "On day " << i << " I drank " << shotsOfWheatgrass[i]; if (shotsOfWheatgrass[i] == 1) { cout << " shot." << endl; } else { cout << " shots." << endl; } }

Do you see how inside the loop, the value for i is used to access each value in the array?

Instructions

Create a program that prompts the users for fifteen whole numbers, stores them in an array, and then prints out the contents of the array. An example interaction follows:

Hey! Witness my first array mojo! Enter 15 numbers and I will tell you what they are. Number 1: 8 Number 2: 6 Number 3: 7 Number 4: 5 Number 5: 3 Number 6: 2 Number 7: 4 Number 8: 1 Number 9: 3 Number 10: 13 Number 11: 32 Number 12: 14 Number 13: 25 Number 14: 51 Number 15: 9 So awesome! The numbers are: 8 6 7 5 3 2 4 1 3 13 32 14 25 51 9 Have a nice day!

If you are unsure how to get started, here are the suggested steps:

Step 1: declare an array of fifteen integers (which you access with 0 to 14).

Step 2: using a for loop, prompt the user for each number and then assign the entered number to the ith value in the array.

Step 3: using a 2nd for loop, output the ith value in the array.

Step 4: Profit.

-----------------------------------

-----------------------------------

*PART 2:*

Now, we are going to find the largest value in an array. After you have printed out the values within the array, iterate through the array searching for the largest value in the array. Then print this value. (There are hints below if you need a starting point.)

Then repeat the same process to find the minimum value in the array. An example interaction follows:

Hey! Witness my first array mojo! Enter 15 numbers and I will tell you what they are. Number 1: 8 Number 2: 6 Number 3: -7 Number 4: 5 Number 5: 3 Number 6: 2 Number 7: 4 Number 8: 1 Number 9: 3 Number 10: 13 Number 11: 32 Number 12: 14 Number 13: 25 Number 14: 51 Number 15: 9 So awesome! The numbers are: 8 6 -7 5 3 2 4 1 3 13 32 14 25 51 9 Have a nice day! The largest number is: 51 The smallest number is: -7 Gaze at my awesome.

If you are unsure how to get started, here are the suggested steps for your function:

Step 1: declare a variable for the largest number, and initialize it to be the 1st value in the array.

Step 2: using a for loop, read each array value and update the largest number variable (if appropriate). Think carefully about which two values you need to compare.

Step 3: print the largest number.

Functional Requirements

You must iterate through the array again after printing all the values. You should not keep track of the smallest/largest value as the user is entering them.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions