Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays

Description:

An array is a structure which holds many variables, all of the same data type. ... Garfield Asterix Taz ... 79312444 99453552 79266262 ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 9
Provided by: willia442
Category:
Tags: arrays | taz

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
Foundation Studies Course
2
Introduction
  • An array is a structure which holds many
    variables, all of the same data type.
  • The array consists of so many elements, each
    element of the array capable of storing one piece
    of data (ie, a variable).

3
Definition
  • An array is defined as follows
  • array_name ARRAY lower..upper of data_type
  • Lower and Upper define the boundaries for the
    array.
  • Data_type is the type of variable which the array
    will store, eg, type int, char etc.

4
Declaring Arrays
  • A typical declaration follows
  • VAR
  • marks ARRAY 1..20 of integer
  • This creates a definition for an array of
    integers called marks, which has 20 separate
    locations numbered from 1 to 20. Each of these
    positions (called an element), holds a single
    integer.

5
Assigning Values
  • To assign a value to an element of an array, use
  • marks2 10
  • This assigns the integer value 10 to element 2 of
    the marks array. The value or element number
    (actually its called an index) is placed inside
    the square brackets.
  • To assign the value stored in an element of an
    array to a variable, use
  • Var
  • totalinteger
  • total marks1marks2 marks10
  • This takes the integer stored in element 1 to 10
    of the array marks, and makes the integer total
    equal to it.

6
Example
  • program Simple
  • var name string
  • maths,english,maltese,total integer
  • average real
  • begin
  • Write('Enter name of student ')
  • readln(names)
  • write('Enter marks for Maths --gt ')
  • readln(maths)
  • write('Enter marks for English --gt
    ')
  • readln(english)
  • write('Enter marks for Maltese --gt
    ')
  • readln(maltese)

7
Example
  • program Simple_Arrays
  • var index integer
  • names array 1..10 of string
  • maths,english,maltese,total array 1..10 of
    integer
  • average array 1..10 of real
  • begin
  • for index 1 to 10 do
  • begin
  • Write('Enter name of student
    ',Index,' ')
  • readln(namesindex)
  • write('Enter marks for Maths --gt ')
  • readln(mathsindex)
  • write('Enter marks for English --gt
    ')
  • readln(englishindex)

8
Lab
  • In order to store the telephone numbers of his
    friends, a programmer decided to make use of two
    1-dimensional arrays to store the names and the
    corresponding phone numbers.
  • Garfield Asterix Taz
  • 79312444 99453552 79266262
  • Write a short program to enter and store up to
    fifty names and telephone numbers. Ensure that
    the exact variable declarations are made as well
    as the correct program syntax in general is
    used.


Write a Comment
User Comments (0)
About PowerShow.com