Module 5 CSC 201
Module 5 CSC 201
Module 5 CSC 201
Objectives
By the end of this module, you will be able to:
Understand the concept of a list called array
array
Use multidimensional array
MODULE 6: Arrays and Lists
my List[0] 5.6
my List[1] 4.5
my List[2] 3.3
my List[3] 13.2
my List[4] 4.0
Array element at
my List[5] 34.33 Element value
index 5
my List[6] 34.0
my List[7] 45.45
99.993
my List[8]
111.23
my List[9]
Declaring Array Variables
datatype arrayRefVar[arraySize];
Example:
double myList[10];
3 2 State[2] G
4 3 State[3] O
5 4 State[4] S
6 5 State[5] \0
one-dimensional character
array
L A G O S \0
Subscrip 0 1 2 3 4 5
t:
6 – element character array
Therefore, the 3rd array element is G represented by state[2]
The important thing is that the size allocated must not be less than the
items to be stored
The following declaration is also allowed. Here, You don’t have to specify the size:
char state[ ] = “LAGOS”; //acceptable
char state[6 ] = “LAGOS”; // acceptable
char state[5] = “LAGOS”;//this truncates the end of string character
char state[20 ] = “LAGOS”;// too large waste space, extra spaces may be filled with
zeros or meaningless characters
Printing Character Array
Copying Arrays
int x = 10;
int y;
y = x;
You are not permitted to do this assignment operation in
C++
list = myList; //copying array list into another array as this
Copying into array
#include <iostream>
using namespace std;
{
for (int column = 0; column < columnSize;
column++)
{
matrix[row][column] = rand() % 100;
}
}
Summing all Elements