[ad_1]

4.2 Guided PracticeIntroduction to Arrays

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).

If you haven’t already, please read the following in your textbook, Deitel, P. &Deitel, H. (2015). C how to program, 8th ed., before beginning:

Instructions

Follow these instructions to complete your assignment:

  1. Use Flowgorithm to create the flowchart in the tab below.
  2. Select the Code tab and enter the code into your compiler.
  3. Compile your code and run. Your output should match the image on the Output tab.
  4. Paste your flowchart and a screenshot of your output (including the Title Bar showing the path and name of your code) into a Word document (.docx).
  5. Submit the “.c” code file, which should include a comment line with your name and the date.
  6. Submit the Flowgorithm (.fprg) file.
  7. Save your files and upload them using the instructions below.

[ad_2]

Testimonials

Programming
We have updated our contact contact information. Text Us Or WhatsApp Us+1-(309) 295-6991