Uploaded by hanzalahbilal17

Lecture-6-Arrays-Part-I-23032024-075118pm

advertisement
3/19/2024
LECTURE
6
Arrays
CSC 113
Computer Programming
Spring 2024
Abrar Ahmed
abrarahmed.buic@bahria.edu.pk
Department of Computer Science
Bahria University, Islamabad
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 1
1
Outline
• Array
• Declaring Array
• Initializing Array
• Accessing elements of Array
• Inputting elements of Array
• Outputting elements Array
• Operations on Arrays
• Examples
• Sorting of Arrays
• Searching in Arrays
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 2
2
1
3/19/2024
Arrays
CSC 113 – Computer Programming
6. Arrays
Slide: 3
Department of Computer Sciences
Bahria University, Islamabad
3
C++ Data Types
simple
integral
enum
structured
floating
array struct union class
char short int long bool
float double long double
address
pointer
Department of Computer Sciences
Bahria University, Islamabad
reference
CSC 113 – Computer Programming
6. Arrays
Slide: 4
4
2
3/19/2024
Arrays
• Array - a collection of a fixed number of components wherein all of the components have the same data
type
• One-dimensional array - an array in which the components are arranged in a list form
• The general form of declaring a one-dimensional array is:
dataType arrayName[intExp];
where intExp is any expression that evaluates to a positive integer
Also, intExp specifies the number of components in the array.
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 5
5
5
Arrays
• Consecutive group of memory locations
• Same name and type (int, char, etc.)
• Static entity (same size throughout program)
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 6
6
3
3/19/2024
Arrays
• To refer to an element
• Specify array name and position number (index)
• Format: arrayname[ position number ]
• First element at position 0
• N-element array list
list[ 0 ], list[ 1 ] … list[ n - 1 ]
• Nth element as position N-1
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 7
7
Arrays
• Array elements like other variables
• Assignment, printing for an integer array c
list[ 0 ] = 3;
cout << list[ 0 ];
• Can perform operations inside subscript
list[ 5 – 2 ] same as list[3]
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 8
8
4
3/19/2024
Arrays
Name of array
list[0]
-45
list[1]
6
list[2]
0
list[3]
72
list[4]
1543
list[5]
-89
list[6]
0
list[7]
62
list[8]
-3
list[9]
1
list[10]
6453
list[11]
78
Position number of the element
within array list
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 9
9
Declaring Arrays
• When declaring arrays, specify
• Name
• Type of array
• Any data type
• Number of elements
• type arrayName[ arraySize ];
int list[ 10 ]; // array of 10 integers
float d[ 3284 ]; // array of 3284 floats
• Declaring multiple arrays of same type
• Use comma separated list, like regular variables
int b[ 100 ], x[ 27 ];
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 10
10
5
3/19/2024
Examples Using Arrays
• Initializing arrays
• for loop
• Set each element
• Initializer list
• Specify each element when array declared
int myList[ 5 ] = { 1, 2, 3, 4, 5 };
• If not enough initializers, rightmost elements 0
• To set every element to same value
int myList[ 5 ] = { 0 };
• If array size omitted, initializers determine size
int myList[] = { 1, 2, 3, 4, 5 };
• 5 initializers, therefore 5 element array
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 11
11
1
2
// Initializing an array with a declaration.
3 #include <iostream>
4
7
void main()
8
{
9
// use initializer list to initialize array n
10
int myList[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
11
12
cout << "Element \t Value" << endl;
13
14
// output contents of array n in tabular format
15
for ( int index = 0; index < 10; index ++ )
16
cout << index << “\t” << myList[ index ] << endl;
17
18
19
} // end main
Department of Computer Sciences
Bahria University, Islamabad
Element
0
1
2
3
4
5
6
7
8
9
Value
32
27
64
18
95
14
90
70
60
37
CSC 113 – Computer Programming
6. Arrays
Slide: 12
12
6
3/19/2024
Arrays
• Array size
• Can be specified with constant variable (const)
const int size = 20;
• Constants cannot be changed
• Constants must be initialized when declared
• Also called named constants or read-only variables
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 13
13
Arrays
void main()
{
const int arraySize = 10;
int myArray[arraySize]; // array s has 10 elements
for (int index = 0; index < arraySize; index++) // set the values
{
myArray[index] = 2 + 2 * index;
}
for (int index = 0; index < arraySize; index++)
{
cout << "Value at index " << index << " is " << myArray[index] << endl;
}
}
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 14
14
7
3/19/2024
Arrays
• Using const
void main()
{
const int value;
value = 7;
}
// Error: x must be initialized
// Error: cannot modify a const variable
CSC 113 – Computer Programming
6. Arrays
Slide: 15
Department of Computer Sciences
Bahria University, Islamabad
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//
// Compute the sum of the elements of the array.
#include <iostream>
using namespace std;
void main()
{
const int arraySize = 10;
int myArray[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int total = 0;
// sum contents of array a
for ( int index = 0; index < arraySize; index ++ )
total += myArray[ index ];
cout << "Total of array element values is " << total << endl;
} // end main
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 16
16
8
3/19/2024
How to find size of an Array?
• sizeof()
sizeof(int);//4
sizeof(char);//1
int x; sizeof(x);//4
• For an Array?
int list[] = { 1,2,3 };
int size = sizeof(list) / sizeof(list[0]);
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 17
17
Printing Arrays
• To print an array, you have to print each element in the array using a loop
like the following:
for (int index = 0; index < ARRAY_SIZE; index++)
{
cout << list[index] << endl;
}
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 18
18
9
3/19/2024
Copying Arrays
• Can you copy array using a syntax like this?
• list = myList;
• This is not allowed in C++. You have to copy individual elements from one
array to the other as follows:
for (int index = 0; index < ARRAY_SIZE; index++)
{
list[index] = myList[index];
}
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 19
19
Finding the largest element
• Use a variable named max to store the largest element. Initially max is
myList[0]. To find the largest element in the array myList, compare each
element in myList with max, update max if the element is greater than max.
double max = myList[0];
for (int index = 1; index < ARRAY_SIZE; index++)
{
if (myList[index] > max)
max = myList[index];
}
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 20
20
10
3/19/2024
Finding the index of the largest element
double max = myList[0];
int indexOfMax = 0;
for (int index = 1; index < ARRAY_SIZE; index++)
{
if (myList[index] > max)
{
max = myList[index];
indexOfMax = index;
}
}
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 21
21
Shifting Elements
double temp = myList[0]; // Retain the first element
// Shift elements left
for (int index = 1; index < SIZE; index++)
{
myList[index - 1] = myList[index];
}
// Move the first element to fill in the last position
myList[SIZE - 1] = temp;
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 22
22
11
3/19/2024
#include <iostream>
// Random Shuffling
#include <cstdlib>
#include <ctime>
using namespace std;
const int ARRAY_SIZE = 10;
int main()
{
int myList[ARRAY_SIZE] = { 1,2,3,4,5,6,7,8,9,10 };
srand(time(0));
for (int index = 0; index < ARRAY_SIZE; index++)
{
// Generate an index randomly
int randomIndex = rand() % ARRAY_SIZE;
int temp = myList[index];
myList[index] = myList[randomIndex];
myList[randomIndex] = temp;
}
for (int index = 0; index < ARRAY_SIZE; index++)
cout << myList[index] << " ";
return 0;
}
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 23
23
srand(time(0));
int numbers[500];
int num_num;
cout << "How many numbers (up to 500)?";
cin >> num_num;
for (int index = 0; index < num_num; index++)
numbers[index] = rand() % 100;
cout << "\n Your numbers reversed are:\n";
for (int index = num_num - 1; index >= 0; index--)
cout << setw(3) << numbers[index];
cout << endl;
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 24
24
12
3/19/2024
Generating Radom Values
const int SIZE = 100;
int valueLimit = 20;
int list[SIZE];
srand(time(0));
for(int index = 0; index<SIZE; index++)
list[index] = rand() % valueLimit;
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 25
25
const int SIZE = 10;
int list[SIZE];
srand(time(0));
for(int index = 0; index<SIZE; index++)
list[index] = rand() % 20;
cout << "Element\tValue\tHistogram" << endl;
for (int index = 0; index < SIZE; index++)
{
cout << index << "\t" << list[index]<<"\t";
for (int bar = 0; bar <= list[index]; bar++)
cout << "*";
cout << endl;
}
Department of Computer Sciences
Bahria University, Islamabad
CSC 113 – Computer Programming
6. Arrays
Slide: 26
26
13
Download