0% found this document useful (0 votes)
15 views14 pages

Topic8 Array Part1

Uploaded by

panha7016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

Topic8 Array Part1

Uploaded by

panha7016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Data structure and

programming

Topic 8- Array
int marks[5]; char animals[4][20];

1
Outline
❑ An overview of the lessons

▪Introduction
▪ Problem when not using array

▪Array
▪ What is array?
▪ How to use array?
▪ More on array
2
Introduction
❑ Problem
▪ Problem #1: Suppose we want to get 100 students’ names then display theirs
names in a list. Will you use 100 variables? Disadvantages:
Too many creation of variables?
Var name1, name2, ..., name100 : Sequence of characters What if we have more than 100
Begin variables?
read(name1, name2, ..., name100)
End

▪ Problem #2: Suppose we want to get 100 subjects’ scores of a students then
do summation of those score.
Var score1, score2, …, score100, sum : float
Will we need 100 Begin
variables to store read(score1, score2, ..., score100)
those scores? sum  score 1+ score2 + … + score100
3
End
Array
❑ What is an array?
▪ Array is a kind of data structure that stores many variables (elements) as a
single special variable.
▪ Each variable in an array is called an array element and they have the same
variable type
▪ You could have an array of integers or an array of characters or an array of
anything that has a defined data type.
Array
▪ An overview of an array:
element

Index or position
4
Array
❑ Declaring (creating) an array
▪ To declare an array, we have to choose
▪ Type of element in the array
▪ Number of elements in the array

▪ Syntax
Var identifier[number of elements] : Type of element in array

Suppose that we add values (10, 20, .., 100) to the array.
▪ Examples: Creating array The array now look like this:
Var num[20] : Integer
Var scores[10] : Float
Var name[50] : Array/sequence of characters
Var s[5][100] : Array of string (5 elements) 5
Array
❑ Index or position
▪ In array, the value of index is
▪ Start from 0 (some language may start with index 1)
▪ E.g: In C language, index starts from 0 but in Matlab index starts from 1
▪ Integer number
▪ Last value of index is equal to number of elements in array minus 1
(when its index starts with 0)

▪ Index in the bracket can either be a direct integer value or a variable or an


expression Var n : Integer
Var score[10] : Integer
Var score[10] : Integer
Begin
Begin
n  0
score[0]  70
score[n]  70
score[1]  80
score[n+1]  80
End
End
6
Array
❑ Access/use to an array
▪ To display array’s elements, we need to access to each element
▪ To access a specific element in an array, use arrayName[index]
▪ Ex: Suppose the array named ele
Var i : Integer
▪ Then to access: ele[0], ele[1], …., ele[9] Var num[10] : Integer
Begin
▪ Examples for (i0; i<10; ii+1) do
read(num[i])
end for
Var scores[10] : Float
Begin for (i0; i<10; ii+1) do
read(scores[0]) write(num[i])
read(scores[1]) end for
write(“Score student 1: ”, scores[0]) End
write(“Score student 2: ”, scores[1])
End
What does this algorithm do?
7
Array
❑ Access/use to an array
▪ To access a specific element in an array, use arrayName[index]
▪ Suppose we have an array named ele Var i : Integer
▪ Usage: ele[0], ele[1], …., ele[9] Var gender[10] : Sequence of character
Var m, n: Integer
Begin
What does these algorithms do? for (i0; i<10; ii+1) do
Then sum all those numbers together.

Finally, display display #male, #female


read(gender[i])

Then count all males and females


Var i : Integer
end for
Get 10 numbers from the user.

Var num[10] : Integer


m0

Get 10 gender from the user.


Var s: Integer
Begin n0
Finally, display the result.

for (i0; i<10; ii+1) do for (i0; i<10; ii+1) do


read(num[i]) if (gender[i]==‘M’) then
end for m=m+1
s0 else if (gender[i]==‘F’) then
for (i0; i<10; ii+1) do n=n+1
s  s + num[i] end if
end for end for
write(s)
End write(m, n) 8
End





Array
❑ Using array to solve the previous problems?
▪ Solution for Problem #1:
▪ Use an array with the size of 100 and its type is a string (sequence of characters)

Var names[100][20] : Sequence of characters Var scores[100] : float


Begin Var sum : float
for(i0; i<=99; i++) do Begin
read(names[i]) sum  0
for(i0; i<=99; i++) do
end for
read(scores[i])
End end for

for(i0; i<=99; i++) do


▪ Solution for Problem #2: sum  sum + scores[i]
end for
▪ Use an array with the size of 100 and its type is a float write(“Total scores: ”, sum)
▪ Combine those variables into one by declaring End

an array then do loop to find summation. 9


Example 1

10
Example 2: Array example with initialization values

11
Assignment Deadline: 1 week

❑ Practice exercises

1- Write C programs for the problems below:


a. Declare and store an array with 5 English’s vowels
b. Declare and store an array with English’s alphabet A-Z
c. Declare and store an array with even integer numbers 2, 4, … 100
d. Declare and store an array of 10 user names. Ask the user to input
all those 10 names. Then display their names on the screen

12
Assignment Deadline: 1 week

2- Write a C program to ask a user for 20 scores then


▪ Find the average of those scores
▪ Show the scores that are greater than the average
▪ Count number of students who got score more than average

Input number #1: 10


Input number #2: 20
…..
Input number #20: 200

=>OUTPUT: Average is: 105. Scores that are more than average are: 110, 120,
130,140,150,160,170,180,190,200 Coding hour
13
Assignment Deadline: 1 week

3-Write a program in C get 10 numbers input by a user and store in an array.


Then find the sum all elements in the array. Display the result.

4-The same to exercise #1. We also would like to count a total number of
duplicate elements and non-duplicate elements in the array.

5-The same to exercise #1. In addition, we also want to print all unique
elements in an array
14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy