(SP - 25) Lecture 1 - 1D Arrays - NOTES
(SP - 25) Lecture 1 - 1D Arrays - NOTES
Lecture 1
One Dimensional Arrays
Course Logistics
• Instructors
– Dr. Maryam Nabil (Scientific Computing Department)
– Dr. Salsabil Amin (Basic Sciences Department)
• Contact: [email protected]
• Office: 3rd floor, next to Tolba Lecture hall
2
Course Material
Google Drive
(will be sent to your admin)
Course Logistics
4
Course Logistics – Textbook
Available References:
• C++ How to Program, 5th edition, Deitel &
Deitel, Prentice Hall- Pearson Education
International, 2005.
• Competitive Programming, Handbook for
ACM ICPC and IOI Contestants, Steven
Halim, Felix Halim, 2013. (Recommended by the
late Youssef El-Kayyali- ACM Head of
Training Committee)
• Problem Solving with C++, 8th edition,
Walter Savitch, Addison Wesley-Pearson
Education International, 2012.
5
Course Logistics – Outline
1.1D array
2.Functions 1 (by value)
3.Structures+(Project announcement)
4.GUI Support Session+ Problem Solving +(Project Registration)
5.Functions 2 (By Ref) + passing arrays
6.2D array + Problems
7.Pointers I (Concept)
8.Pointers II
9.Recursion
10.Revision
6
Arrays
7
Opening Problem
Ideas?
-8-
Single-Dimensional Arrays
-9-
Arrays
What is a dimension?
Name
type arrayName[arraySize];
int grades[21]; // array of 21 integers
float numbers[324]; // array of 324 floats
Declaration ??
float myList[10];
myList[0] 5.6
myList[1] 4.5
myList[2] 3.3
5th Element??
myList[3] 13.2
myList[4] 65.3
5th Array myList[5] 34.33 5th
element Element
myList[6] 34.0
at index 4 value
myList[7] 45.45
myList[8] 99.993
myList[9] 111.23
-14-
Declaring Arrays
-15-
Constants
16
Constants
Const Vs.#define
-17-
Constants
Const Vs.#define
18
Declaration and Initialization
Declaration
type var_name[size]; → allocates memory
Value or
Constant or
Expression
→ constant
NOT variable
Array Elements
int c[10];
To refer to an element
Specify array name and position number (index)
Format: arrayname[positionNumber]
-20-
Exercise: Difference Between Indices?
Array size
= number
of elements
double grades[10]; (Const)
Position/
index of grades[9] = 97.5;
array
element
(can be
variable) cin>> grades[2];
-21-
No Bound Checking
int myScores[10];
• C++ does not check array’s boundary.
-22-
Initializing Array
-23-
Initializing Arrays: Method 1
Initializer list
Declaring and initializing in one step:
int n[5] = {14, 22, 63, 24, 15};
float myList[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
-24-
Initializing Arrays: Method 1
right most
elements are
assigned zero
-25-
Caution
-26-
Initializing Arrays
You can omit the array size when you declare the
array using initializers
int n[] = {14, 22, 63, 24, 15};
5 initializers, therefore 5 elements in array
-27-
Initializing Arrays:
Method 2
User enters values of array elements.
int main()
{
int arr[5];
cout<<"Enter Array Elements: "<<endl;
for (int i = 0; i < 5; i++)
cin>> arr[i];
cout<<"Displaying Array Elements: "<<endl;
for (int i = 0; i < 5; i++)
cout<< arr[i] << endl;
return 0;
}
-28-
Remember
-29-
Copying Arrays
-31-
Exercise
int grade[9];
for ( int i = 0; i <= 9; i++ ) Wrong
cin >> grade[i];
-32-
Exercise
int numbers[1];
Wrong
numbers = 34;
-33-
*
Exercise: Output?
num num%3
6 0
7 1
8 2
9 0
Random numbers from 0 to 99 10 1
11 2
12 0
-34-
*
Exercise: Output?
Syntax Errors
because
constant
variable must
be initialized
when declared
-35-
*
Exercise: Output?
-36-
Problem: Sum Array Elements
-38-
Problem : Sum Array Elements
Compute the sum of the elements entered by user.
const int size = 5;
int numbers[size];
int sum = 0;
-42-
Solution
const int size = 100;
float numbers[size];
float sum = 0;
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
3 6 4 1 9 7 3 2 8
-44-
Linear Search
-45-
*
Problem: Search Maximum
const int size = 50;
int numbers[size];
int max=0;
Does it work
for negative
for (int i = 0; i < size; i++) numbers?
{
cout<< "Enter number: ";
cin>> numbers[i];
}
max = scores[0];
for (int i = 1; i < size; i++)
if (scores[i] > max)
max = scores[i];
-50-
To-Do List:
1. Sheet 1
2. Home Assignment 1
3. Clean code presentation (Self-Study)