Arrays
Course Code: CSC1102 &1103 Course Title: Introduction to Programming
Dept. of Computer Science
Faculty of Science and Technology
Lecturer No: 5 Week No: 3 Semester: Spring 22-23
Name & ID Md. Faruk Abdullah Al Sohan;
[email protected]Lecture 5: Outline
Arrays
The concept of array
Defining arrays
Initializing arrays
Character arrays
Variable length arrays
The concept of array
Array: a set of ordered data items
You can define a variable called x, which represents
not a single value, but an entire set of values.
Each element of the set can then be referenced by
means of a number called an index number or
subscript.
Mathematics: a subscripted variable, xi, refers to the
ith element x in a set
Programming: the equivalent notation is x[i]
Declaring an array
Declaring an array variable:
Declaring the type of elements that will be contained in the array—such as int,
float, char, etc.
Declaring the maximum number of elements that will be stored inside the
array.
The compiler needs this information to determine how much memory space to
reserve for the array.)
This must be a constant integer value
The range for valid index values:
First element is at index 0
Last element is at index [size-1]
It is the task of the programmer to make sure that array elements are referred
by indexes that are in the valid range ! The compiler cannot verify this, and it
comes to severe runtime errors !
Arrays - Example
int values[10];
Declares an array of 10 elements of type int
Using Symbolic Constants for array size:
#define N 10
…
int values[N];
Valid indexes:
values[0]=5;
values[9]=7;
Invalid indexes:
values[10]=3;
values[-1]=6;
In memory: elements of an array are stored
at consecutive locations
Arrays - Example
#include <iostream> Using symbolic
Using namespace std; constants for array
size makes program
int main () more general
{
int values[6];
int index, i;
for ( index = 0; index < 6; index++ ) {
cout<<“Enter value of element”<<index<<endl;
cin>>values[index];
}
for ( index = 0; index < 6; index++ )
cout<< “values[“<<index<<“]=“<< values[index]<<endl;
return 0; Typical loop for
} processing all
elements of an array
What goes wrong if an index goes out of range ?
#include <iostream>
using namespace std;
int main (){
int NA, NB;
cout<<"Enter NA and NB: "<<endl;
cin>>NA>>NB;
int a[NA],b[NB];
int index;
for ( index = 0; index < NA+2; ++index ){
a[index]=index;}
for ( index = 0; index < NA+2; ++index ){
cout<<"a ["<<index<<"]= "<<a[index]<<endl;}
for ( index = 0; index < NB; index++ ){
b[index]=10+index;}
for ( index = 0; index < NB+2; ++index ){
cout<<"b ["<<index<<"] ="<< b[index]<<endl;}
return 0;
}
Exercise: Fibonacci numbers
// Program to generate the first 15 Fibonacci numbers
#include <iostream>
using namespace std;
int main (){
int size; cout<< "Please give the Fibonacci range: ";
cin>>size;
int Fibonacci[size], i;
Fibonacci[0] = 0; // by definition
Fibonacci[1] = 1;
for ( i = 2; i < size; i++ )
Fibonacci[i] = Fibonacci[i-2] + Fibonacci[i-1];
for ( i = 0; i < size; i++ )
cout<< "Fibonacci["<<i<<"]="<<Fibonacci[i]<<endl;
return 0;
}
Exercise: Prime numbers
An improved method for generating prime numbers involves the notion that a number
p is prime if it is not evenly divisible by any other prime number
Another improvement: a number p is prime if there is no prime number smaller than
its square root, so that it is evenly divisible by it
If you can find a
Is p the next
primes[i] < sqrt(p)
prime number
that divides evenly
here ?
p, than p is not
prime
primes
2 3 5 7 11
0 1 2 3 4
primeIndex
Exercise: Prime numbers
Find out 1st 10 prime numbers and
store it in an array.
Range: 1 to 100
Hints: use loop & array
Initializing arrays
int counters[5] = { 0, 0, 0, 0, 0 };
char letters[5] = { 'a', 'b', 'c', 'd', 'e' };
float sample_data[500] = { 100.0, 300.0, 500.5 };
The C++ language allows you to define an array without
specifying the number of elements. If this is done, the size of
the array is determined automatically based on the number of
initialization elements: int counters[] = { 0, 0, 0, 0, 0 };
Character arrays
#include <iostream>
using namespace std;
int main ()
{
char word[] = { 'H', 'e', 'l', 'l', 'o', '!' };
int i;
for ( i = 0; i < 6; ++i ){
cout<<word[i]<<“\n”;
}
return 0;
}
a special case of character arrays: the character string type =>in a later chapter
Exercise:
1. Develop a program that stores your Name and ID using two
different arrays and displays your information at the end.
2. Develop a program that can store 4 integer numbers and 4
floating point numbers by asking the user for inputs in arrays.
The program estimates the summation, average as well as
multiplication of the stored numbers and prints all the
results.
3. Develop a program that takes three student’s CGPAs as inputs
using a single array and finds the lowest CGPA.