Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A one-dimensional array stores multiple pieces of data of the same data type. A one-dimensional array is also known as a list. Let's say you

A one-dimensional array stores multiple pieces of data of the same data type. A one-dimensional array is also known as a list. Let's say you wanted the user to enter five integer numbers to perform a calculation, instead of declaring five separate variables to store those values, you can create an array that has a size of five elements to store the data. Let's look at a coding example.

Below is an example of declaring an array of size 3, which will store integer values, initializing it with three values. Notice that the size is enclosed in square brackets and the values in curly brackets.

int values[3] = {4, 5, 6};

Another way to store values into an array structure is to use assignment. statements. After you declare your array, you then can assign a value to each element or index. Let's look at a coding example.

int values[3];

values[0] = 2;

values[1] = 5;

values[2] = 8;

Notice the subscripts are "zero based," that is, the first one is subscript zero. This means that the last element in an array structure is always the size - 1. Please note that if you know the values that need to be stored in an array when creating flowcharts in Flowgorithm, you will need to use assignment statements. You cannot declare and initialize an array as seen in the first example.

The last way we can store values into an array is by using a looping structure. This is commonly used when we ask the user to enter the values (the values are unknown). Let's look at a coding example.

int values[3];

int counter; //control variable used to step through the loop

for(counter=0; counter<=2; counter++)

{

printf("Enter a number: "):

scanf("%d", &values[counter]);

}

Notice in we start the loop at 0 since the first element or index in the array is the zeroth element and stop at 2 (size - 1). Each time the loop iterates the user will store a value into an array element in sequential order. Also, notice the syntax in the scanf() statement as well. The variable 'counter' is inside of the square brackets so that each time the loop runs, user input will be stored into an element sequentially (0,1,2).

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

Understanding Energy And Energy Policy An Introduction

Authors: Timothy Braun, Lisa Glidden, Aloka Kumara

1st Edition

1780329369, 9781780329369

More Books

Students also viewed these General Management questions

Question

2. Information that comes most readily to mind (availability).

Answered: 1 week ago

Question

3. An initial value (anchoring).

Answered: 1 week ago