Introduction to Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Arrays

Description:

Introduction to Arrays Presenter: Mrs. McCallum-Rodney – PowerPoint PPT presentation

Number of Views:113
Avg rating:3.0/5.0
Slides: 20
Provided by: Care173
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Arrays


1
Introduction to Arrays
  • Presenter Mrs. McCallum-Rodney

2
WHAT ARE ARRAYS?
  • An array is a set of data of the same type
    grouped together and referred to by one name.
  • Any collection of homogeneous data (data of the
    same type) is well-suited for storage in arrays.

3
WHY USE ARRAYS?
  • Arrays provide a mechanism for declaring and
    accessing several data items with only one
    identifier, thereby simplifying the task of data
    management.
  • As programs become larger and more complicated,
    it becomes increasingly difficult to manage the
    data. Variable names typically become longer to
    ensure their uniqueness. And, the number of
    variable names makes it difficult for the
    programmer to concentrate on the more important
    task of correct coding.

4
STRUCTURE OF ONE DIMENSIONAL ARRAYS?
  • If you will need to store 10 integer numbers and
    still have access to all numbers entered, then
    entering the numbers into an array will be
    appropriate.
  • Consider the following array
  • Number

11 2 51 1 19 71 -11 87 43 16
5
STRUCTURE OF ONE DIMENSIONAL ARRAYS?
Number
Subscript 0 1 2 3 4 5 6 7 8 9
Values 11 2 51 1 19 71 -11 87 43 16
  • How many elements are in the array?

10
Each element has a number assigned to it this
is known as the SUBSCRIPT or INDEX. The
subscript identifies each item in the array.
If you need to access values from the array,
you will need to state the name of the array and
the index (subscript) number. The same goes for
putting data into the array.
6
UNDERSTANDING ARRAYS?
Number
Subscript 0 1 2 3 4 5 6 7 8 9
Values 11 28 51 1 19 71 -11 87 43 16
  • What is the value in the 4th element of the
    array?
  • What is in Number7?
  • What is in Number1?
  • Where is the value -11 stored?
  • Where is the value 16 stored?

1 87 28 Number6 Number9
7
UNDERSTANDING ARRAYS?
STUDENT_NAME
Subscript 0 1 2 3 4 5
Values ALTEVETA JEWEL-ANN ALECIA ROXANNE RENEE SUZAN
  • What is the name of the array?
  • How many elements are in the array?
  • What is the data type for STUDENT_NAME
  • What is the value in the 5th element of the
    array?
  • What is in STUDENT_NAME1?
  • Where is the value SUZAN stored?

STUDENT_NAME 6 string RENEE JEWEL-ANN STUDENT
_NAME5
8
DECLARING ARRAYS
  • In every algorithm and program you will need to
    state each variable that you will be using and
    its data type. This is known as DECLARING
    variables.
  • To declare an integer array called Number, to
    hold 10 values, you will do the following

PSEUDOCODE DECLARE Number AS integer array of
size 10
C int number 10
9
DECLARING ARRAYS
Question 1
Declare an array called teachaddr, to hold addresses of 50 teachers.
PSEUDOCODE DECLARE teachaddr AS string array of size 50
C char address 5030 //50 addresses with max string length of 30
10
DECLARING ARRAYS
Question 2
Declare an array called costprice, to hold the price of 1000 items in store
PSEUDOCODE DECLARE costprice AS real array of size 1000
C float costprice1000
11
DECLARING ARRAYS
Question 3
Declare an array called answer, to hold the answer of Y or N for 25 questions in a questionnaire.
PSEUDOCODE DECLARE answer AS character array of size 25
C char answer25
12
ASSIGNING VALUES TO ARRAY ELEMENTS
STUDENT_NAME
Subscript 0 1 2 3 4 5
Values Kerri-Ann Allyandra
  • Place the value Kerri-Ann into third element of
    STUDENT_NAME.
  • PSEUDOCODE STUDENT_NAME2 ? Kerri-Ann
  • C strcpy(STUDENT_NAME2 ,Kerri-Ann)
  • Place the value Allyandra into last element of
    STUDENT_NAME.
  • PSEUDOCODE STUDENT_NAME5 ? Allyandra
  • C strcpy(STUDENT_NAME5 , Allyandra)

13
FILLING AN ARRAY using a FOR LOOP
Grades
Subscript 1 2 3 4 5
Values 76 89 100 67 90
Pseudocode segment . . FOR count ? 0 TO 4 PRINT
Enter grade for student READ
gradescount ENDFOR . .
C segment . . for (count 0 count lt 5
count) printf(Enter grade for
student) scanf(d, gradescount)
. .
14
FILLING AN ARRAY using a FOR LOOP
Question Write an algorithm pseudocode to accept
the names and ages of 100 students.
START DECLARE count AS integer DECLARE sname AS
string array of size 100 DECLARE sage AS integer
array of size 100 FOR count ? 0 TO 99 PRINT
Enter name of student READ snamecount PRINT
Enter the age of student READ
sagecount ENDFOR STOP
15
FILLING AN ARRAY using a FOR LOOP
Question Write C to accept the names and ages of
100 students.
/ This program accepts the names and ages of 100
student Created by Careene McCallum-Rodney Date
March 1, 2010/ includeltstdio.hgt int
main() int count char sname
100 50 int sage 100
for(count0 count lt 100 count)
printf(Please enter name and age)
gets(snamecount)
scanf(d, sagecount)
return 0
16
PRINTING VALUES from ARRAY ELEMENTS
STUDENT_NAME
Subscript 0 1 2 3 4 5
Values KRYSSAL SHANICE JODY-GAYE MARSHA SARAN AMELIA
  • PRINT the value in first element of STUDENT_NAME.
  • PSEUDO PRINT STUDENT_NAME0
  • C printf(Name is
    s,STUDENT_NAME0)
  • Place the value in the fourth element of
    STUDENT_NAME.
  • PSEUDO PRINT STUDENT_NAME3
  • C printf(Name is
    s,STUDENT_NAME3)

KRYSSAL
MARSHA
17
PRINTING ALL VALUES in an ARRAY
STUDENT_NAME
Subscript 0 1 2 3 4 5
Values KRYSSAL SHANICE JODY-GAYE MARSHA SARAN AMELIA
Pseudocode segment . . FOR count ? 0 TO 5
PRINT STUDENT_NAMEcount ENDFOR . .
KRYSSAL
SHANICE
JODY-GAYE
MARSHA
SARAN
AMELIA
18
PRINTING AN ARRAY using a FOR LOOP
Question Write a C program to accept the names
and ages of 100 students. After which, print the
names of all students and their age.
/ This program accepts the names and ages of 100
student Created by Careene McCallum-Rodney Date
March 1, 2010/ includeltstdio.hgt int
main() int count char sname
100 50 int sage 100
for(count0 count lt 100 count)
printf(Please enter name and age)
gets(snamecount)
scanf(d, sagecount)
for(count0 count lt 100 count)
printf(s\td,
snamecount, sagecount) return
0
19
The End of Todays Lesson
  • Hope you have learnt a lot today.
Write a Comment
User Comments (0)
About PowerShow.com