Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Loops and Arrays Objectives: Arrays are a series of elements consisting of the same type (For ex: all integers), in a contiguous chunk of memory.

Loops and Arrays

Objectives:

Arrays are a series of elements consisting of the same type (For ex: all integers), in a contiguous chunk of memory. The elements can be individually referenced by indexing the array.

You must specify the type of data stored in an array, and each element in any array must be of the same type.

An array has a static size -- they cannot grow any larger than the size you tell it to be when you create it.

To access the value at that index of the array: for (int i = 0; i < 10; i++){

cout<< intArray[i]<< endl; }

To access the memory address of the array element: for (int i = 0; i < 10; i++){

cout << intArray + i << endl; }

Creating an Array and Initializing

When you declare an array, you will get an uninitialized section of memory. If you want to create an array of numbers, you would do:

int intNumbers [10];

double dblNumbers[10];

Then, putting element in the array can be set up by using the index of the array element you want to change:

intNumbers[0] = 3; intNumbers[1] = 5; intNumbers[2] = 6; intNumbers[3] = 8;

1. Write a program that uses a for loop. 2. Declare and initialize and array. 3. Practice the array operations print and modify.

When you create an array, you do so to capture a group of items that all need to be treated separately, but also need to grouped together for some purpose. For example, when you think about a song, each note in the song is a separate sound, but they all need to be included together in a certain order to make up the song.

To initialize the array: int intNum [5] = {16, 3, 20, 4, 21}; (Hint: notice the syntax difference initializing this array

versus the one above)

Retrieving/Adding/Changing elements

To access or retrieve individual elements in an array, the syntax is square brackets and the element index (starting from 0):

intNumbers[0]; will be the first item in the intNumbers array. intNumbers[1]; will be the second item in the intNumbers array.

Below is an example of a basic 2D array. It shows how we access certain elements shown in the arrayName[row][column] format. This will be helpful for a future assignment.

We can change the values in the array using the index of the individual items, such as

Example 1: int intNumbers[2];

intNumbers[0] = 5; intNumbers[1] = 7; intNumbers[1] = intNumbers[1] * 2 cout << intNumbers[1] << endl;

Example 2:

//this will print the value 14

intNumber[1] = 10;

Example 3: for (int i = 0; i < 10; i++){

intArray[i] = i; cout << intArray[i] << endl;

//this will update the value to 10 at that index

//will set what the values are in that position //will print out the value of i at each index

}

TO DO: Create an array in your main function

1) Create an array of 20 doubles, using a variable name of your choosing. Assign values to your array using a for loop:

for (int i = 0; i < 20; i++) myDoubles[i] = i;

where myDoubles is the name of the variable created.

2) Print out the values in another for loop using: cout << Stuff to print << endl;

3) Now, using your array and another loop, calculate the average of the values in the myDoubles array and print out the average using the statement below:

The average value of myDoubles is: X

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_2

Step: 3

blur-text-image_3

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

Upgrading Oracle Databases Oracle Database New Features

Authors: Charles Kim, Gary Gordhamer, Sean Scott

1st Edition

B0BL12WFP6, 979-8359657501

More Books

Students also viewed these Databases questions

Question

c. How is trust demonstrated?

Answered: 1 week ago

Question

c. Will leaders rotate periodically?

Answered: 1 week ago

Question

b. Will there be one assigned leader?

Answered: 1 week ago