Arrays - Chapter 5 PDF
Document Details
Uploaded by CommendableFable
Tunis Business School
Tags
Related
- CS50x Introduction to Computer Science - Lecture 2 (2024) PDF
- COMP 1000 Computer Science I Exam 2 Review - PDF
- University of Zambia CSC 2000 Computer Programming Past Paper PDF 2021
- Computing Fundamentals (CS116) 2D Arrays Lecture PDF
- ITEC81: VB.NET Arrays (2) Lecture Notes PDF
- Computer Science - I Past Paper PDF 2022
Summary
This document provides a lecture on arrays in programming, covering both one-dimensional and two-dimensional arrays. It describes the concepts of arrays and elements using algorithms and C programming examples.
Full Transcript
Chapter 5: Arrays 1 Outcomes By the end of this chapter, the students will: Be able to define, declare and fill an array of only one dimension. Be able to define, declare and fill a two dimensional array. Know how to read elements from an array and a matrix by writing an...
Chapter 5: Arrays 1 Outcomes By the end of this chapter, the students will: Be able to define, declare and fill an array of only one dimension. Be able to define, declare and fill a two dimensional array. Know how to read elements from an array and a matrix by writing an algorithm and a C program. 2 Why we use arrays ? What do you propose if you want to store 100 students marks? → Declare 100 variables, one for each mark. X → Declare an array of it. ✔ 3 Arrays definition An array is a linear data structure that holds a fixed-size sequential collection of elements of the same type (homogeneous) The size of an array must be a constant value Two kinds of arrays are managed : The one-dimensional arrays The Multidimensional arrays (we are limited in this course to two-dimensional arrays). By analogy with the mathematical concepts of vector and matrix, arrays types with one and two indices are often called vector type and matrix type, respectively. 4 Array type may be identified with other data types such as strings. 1D Arrays Declaration A one-dimensional array has a single column of elements It is an array having a single index value that represents all the array’s elements The compiler has to know the number of elements of an array → its size/length The size information corresponds to the number of memory slots to be booked. Declaration in Algorithms :Array [1..] of DataType indicates the type o Example f the Array values, such as int, float, etc. A1: Array [1..100] of integers ArrayName specifies Declaration in a C program the name of the array < DataType > []; 5 Size indicates the physical size Example of the array int A1 ; 1D - Arrays representation int Abc is an array of 10 integers as follows: Algorithm: 9th Element Abc Abc Abc ………. ………. ………. ………. Abc Abc 1 2 3 …………… 9 10 Index 9th Element In C language: Abc Abc Abc ……… ……… ……… ……… ……… Abc Abc..... 6 0 1 2 …………… 8 9 Index 1D- Arrays representation An index can also be an arithmetic expression : Example : Abc[i+2], Abc[i*2+j] The ith element of Abc array is denoted Abc[i] : 1st element : Abc 2nd element : Abc 3nd element : Abc 7 Arrays representation Each element of the array is similar to a single variable and can handle one value at a time. Likewise, an array element can : Algorithm C language Contain an assignment A[i] 5 A[i] = 5 ; operation Appear in an expression x A[i] *2 X= A[i] *2; Appear in an output Write (" Output ", A[i] ) printf(" Output %d ", A[i] ); instruction 8 Appear in an input Read (A[i]) scanf("%d ",&A[i]); instruction Arrays Physical and Logical Sizes Often, not all of the memory of an array is used. For example, we might have an array with 100 spots while we are currently only using the first 10 of them. The physical size of an array : is the number of slots that are available. - Ex: int A means we physically booked 100 memory slots The logical size of an array : is the total number of occupied slots. → it must be less than or equal to the physical size. 9 1D Arrays Declaration – Example 1 Let’s say you want to declare an array of four floats named “Mark” We use the following instruction to declare it: float Mark; Here, we declared an array, called Mark, of floating-point type and size 4 Meaning, it can hold 4 floating-point values Last index = First index = 0 Length -1 10 1D Arrays Initialization- Remarks In C programming, arrays can be initialized when they are declared: float Mark={88.5,64,90.25,100}; It is not necessary to define the size of arrays during initialization float Mark[]={88.5,64,90.25,100}; In this case, the compiler determines the size of array by calculating the number of elements of an array 88.5 64 90.25 100 11 1D Arrays Data Access A specific element in an array is accessed by an index This is done by placing the index of the element within square brackets after the name of the array Example : Mark Arrays in C programming - Key notes Arrays have 0 as the first index not 1 In this example, Mark If the size of an array is n, to access the last element, (n-1) index is used In this example, Mark 12 1D Arrays Data Access – Example2 Consider the declared array named “myfirstarray” of size 4. To fill each element (access it), you can use the following code: 13 1D Arrays - Example 3 A is an array of N integers. Read the N elements of A, compute their sum and display it. Algorithm SumArray #include var main() N,i,S :Integer { A :Array [1..10] of Integer int A,N,i,S; begin printf("Please enter the size of A :\t"); write("Please enter the size of A : ") scanf("%d", &N); read (N) for (i=0;i