Programing C+ Lab 06

Submitted by: Submitted by

Views: 57

Words: 683

Pages: 3

Category: Science and Technology

Date Submitted: 02/11/2015 11:55 AM

Report This Essay

TECH104 Assignment 06 Answer – F14

Overview

This assignment looks at arrays. Arrays are groups of the same type of data. For instance you may want to deal with a collection of integers in a more convenient way than declaring many separate variables, especially if you are doing common manipulations of the data. In C arrays are given a name like any other variable and the various members of the group are accessed by an index which has an integer value.

Arrays are setup as follows:

float mydata[50];

In this example mydata is the name given to a group of 50 memory contiguous memory locations that can hold decimal numbers (float). To access the first data location in the array and set it to a value you would:

mydata[0] = 56.78;

So mydata is the name of the array, which is followed by an integer index value (or and expression which evaluates to an integer value) inside square brackets. In this example the index is 0, which points to the first float in the group. Notice that:

• Square brackets are used in C to indicate and array.

• When you declare or setup the array, the value in the square brackets indicates the number of elements in the array. In this case there are 50 floats.

• Array indexes start a 0. When you use the array, the first element is at indiex 0, and for this array the last or 50th element is at index 49.

The most common errors seen when using arrays are:

• Making the array size one element too small during declarations.

• Not starting the indexing at 0.

• Not ending the indexing at the declaration size -1.

• Indexing past the endpoint in the array and potentially altering (corrupting) memory that is not reserved for your use. C does not check if you have a valid index value.

When manipulating series of numbers, sometimes it is convenient to re-shuffle the position of data in an array. Ordering information in such groups of data, can have important effects on the speed of accessing data...