chapter 7 arrays i ntroduction t o c omputer p rogramming (csc425)
TRANSCRIPT
CHAPTER 7arrays
INTRODUCTION TO COMPUTER
PROGRAMMING(CSC425)
• Introduction• Single-Dimensional Arrays
2
CONTENTS
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
• Array :is a collection of a fixed number of components where in all of the components have the same data type
3
INTRODUCTION
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
• Is used to store multiple value in a single identifier.• General array declaration syntax:
data-type array name [number of items]
For example:– float temp[5] = to store 5 float value in temp.– int max[4] = to store 4 integer value in max– char y[20] = to store a string of value
4
SINGLE-DIMENSIONAL ARRAYS
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
5
SINGLE-DIMENSIONAL ARRAYS
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Each item in an array is called an element or component. Every item has its position in an array, this position is called
index or subscript; always start at 0. Example :
int num[10] store 10 data.
Note : int num[] error! int num[]={1,2,3} not error.
num[0] refer to the first data
num[1] refer to the second data
num[2] refer to the third data
6
SINGLE-DIMENSIONAL ARRAYS
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Input & output :
int num[100] // declarationcin >> num[20]; // to enter the 21th valuecout << num[20]; // to display the 21th
value
//using looping:for(int i=0; i<5; i++)cout << num[i];
7
SINGLE-DIMENSIONAL ARRAYS
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Input & output :
Array can be initialized within their declaration:
int temp[5]={1,2,3,4,5};char codes[6]={‘s’,’a’,’m’,’p’,’l’,’e’}; orchar codes[6]=“sample”;Note : a string is terminated with a special sentinel,“\0”
the null character.
8
SINGLE-DIMENSIONAL ARRAYS
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
NOTE: If what we declare doesn’t fulfill what the size of the array
wants, for example:
int max[6]={1,2,3} we only declare three but the array wants 6; the compiler will
assume the next three integer are zero :
“ 1,2,3,0,0,0 “ If what we declare exceeds what the array wants, for example:
int max[3]={1,2,3,4,5,6,7,8} the array wants three but we declare 8” the compiler will only
take the first three integer :
“1,2,3”
9
SINGLE-DIMENSIONAL ARRAYS
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Example 3: How arrays are stored in the computer memory
Array list : int list[10];
10
SINGLE-DIMENSIONAL ARRAYS
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Accessing Array component : list[5]=34;
11
SINGLE-DIMENSIONAL ARRAYS
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Adding components in an array
List[3] = 10;
List[6] = 35;
List[5] = list[3] + list[6];
12
Processing single-dimensional arrays
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Some basic operations performed on a one-dimensional array are: Initialize Input data Output data stored in an array Find the largest and/or smallest element
Each operation requires ability to step through the elements of the array
Easily accomplished by a loop
13
Accessing array components
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Consider the declaration
int list[100]; //list is an array of the size 100int i;
This for loop steps-through each element of the array list starting at the first element
for (i = 0; i < 100; i++) //Line 1 //process list[i] //Line 2
14
Accessing array components
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
If processing list requires inputting data into list :
the statement in Line 2 takes the form of an input statement, such as the cin statement
for (i = 0; i < 100; i++) //Line 1 cin >> list[i];
15
Accessing array components
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
If processing list requires printing data from an array:
the statement in Line 2 takes the form of an output statement, such as the cout statement
for (i = 0; i < 100; i++) //Line 1 cout << list[i];
#include<iostream.h>int main (){
double myList[10], yourList[10];int index;
cout<<" Enter 10 numbers :";
for(index=0; index <10 ; index++){
cin>>myList[index]; //read component}
cout<<"\n The components inside the myList array are >> ";
for (index=0; index <10; index++){
cout<<myList[index]<< " "; //print component}
cout<<"\n\n The numbers inside the yourList array are >> ";
for (index=0; index <10; index++){
yourList[index] = myList[index]; //copy componentcout<<yourList[index]<< " ";
}cout<<"\n\n The numbers in yourList[5] is >> " <<yourList[5];
return 0;}
Example 1
17
Example 2 of Single-Dimensional Arrays
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
#include <iostream.h>
18
Example 2 - Answer
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
• Arrays are passed by reference only
• The symbol & is not used when declaring an array as a formal parameter
• The size of the array is usually omitted
19
Arrays as parameters to function
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
• If the size of one-dimensional array is specified when it is declared as a formal parameter
– It is ignored by the compiler
• The reserved word const in the declaration of the formal parameter can prevent the function from changing the actual parameter
20
Arrays as parameters to function
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Example 1 :
Constant arrays as formal parameters :
21
Arrays as parameters to function
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
22
Arrays as parameters to functionExample 2 :
23
Arrays as parameters to functionExample 2 :
24
Arrays as parameters to functionExample 3 :
25
Arrays as parameters to functionExample 4 :
26
Arrays as parameters to function
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Example 4 (cont’d) :
27
Arrays as parameters to function
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Example 4 (cont’d) :
28
Arrays as parameters to function
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Example 4 (cont’d) :
29
Arrays as parameters to function
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Example 4 - Answer :