100% found this document useful (1 vote)
19 views

Chapter 2 Programming II Ppt

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
19 views

Chapter 2 Programming II Ppt

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CHAPTER 2

ARRAYS
MOTIVATION

• have 15 numbers to read in from a file and sort into ascending order
Getting-Started-with-Array-Data-Structure.webp

WHAT IS AN ARRAY

• A collection of identical data objects, which are stored in consecutive memory


locations under a common heading or a variable name.
• Elements-> variable
• Properties of arrays:
• The starting index of an array is 0, not 1.
• The last index is one less than the size of the array.
• If the array has size elements, the range is 0...size-1.
• Arrays contain data of a single type
• illegal to refer to an element outside of the array bounds
• address of its first element …
ARRAY DECLARATION A (ONE-
DIMENSIONAL )
• DataTypename arrayname [array size]; known at the time compilation takes place [ size] and [index]
• int day [5];
#include <iostream>
• Initializing Arrays
using namespace std;
• global array int day [5]; 0
int numbers[5] ;
• gllocal array int day [5]; un determined
int main()
• int day [5] = { 16, 2, 77, 40, 12071 };
{
int numbers[5] ;
• Elements ->equal or less than the length int day [5] = { 16, 2, 77, 40};
for (int i = 0; i < 5; ++i)
• int day [] = { 1, 2, 7, 4, 12,9 };
{
• You can use the initialization form only when defining the array.
cout << numbers[i] << " ";
• int arr [] = {16, 2, 77, 40, 12071};
• int ar [4]; }
• ar[]={1,2,3,4};//not allowed
• arr=ar;//not allowed return 0;
}
ACCESSING AND PROCESSING ARRAY
ELEMENTS
• name [ index ] day[2] = 75;
• two possible uses of brackets [ ] with arrays:
• int day[5]; // declaration of a new Array (begins with a type name)
• day[2] = 75; // access to an element of the Array.
• int a=1;
• day [0] = a;
• day[a] = 3;
• b = day [a+2];
• day [day[a]] = day[2] + 5;

• Example
• Input 10 elements
• Display 10 elements
• Sum of 10 input elements
• Average 10 elements
• Sort 10 elements
• Search 1 element from 10 elements
ARRAYS AS PARAMETERS

• pass its address no & operator


• Declation & definition void procedure (int arg[])
• call like this procedure (myarray);
• int myarray [40];
SORTING
• #include <iostream>

• using namespace std;


• void printArray(int array[], int size) {
• void sort(int array[], int size)
• for (int i = 0; i < size; ++i) {
• {
• cout << " " << array[i];
• for (int i = 0; i < size -2; i++)
• }
• {
• cout << "\n";
• for (int j = 0; j < size - 2; j++)
• }
• {
• int main() {
• if (array[i] > array[j])


• int data[] = {-2, 45, 0, 11, -9};
{

• int temp = array[i];


• int size = 5;

• array[i] = array[j]; • Sort(data, size);

• array[j] = temp; • cout << "Sorted Array in Ascending


Order:\n";
• }
• printArray(data, size);
• }
• }
• }

• }
2.6 MULTIDIMENSIONAL ARRAYS

• int matrix[3][5];
• int matrix [3][5]; is equivalent to int matrix [15]; (3 * 5 = 15)
• pass two dimensional array
• Having the following function prototype:
• void print(int A[][3],int N, int M)
• In order to pass to this function an array declared as:
• int arr[4][3];
• we need to write a call like this:
• print(arr);
2.6 MULTIDIMENSIONAL ARRAYS

• cout << "Matrix 2: " << endl;


• #include <iostream> • int main() { • for(int i = 0; i < ROWS; i++) {
• • int matrix1[ROWS]
using namespace std; • for(int j = 0; j < COLS; j++) {
[COLS] = {{1, 2, 3}, {4, 5, 6}};
• cout << matrix2[i][j] << " ";
• const int ROWS = 2;
• int matrix2[ROWS] • }
• const int COLS = 3; [COLS] = {{7, 8, 9}, {10, 11, 12}};
• cout << endl;
• void addMatrices(int mat1[][COLS], int mat2[][COLS], int result[]• int result[ROWS][COLS];
• }
[COLS]) • addMatrices(matrix1, matrix2, resul•
cout << "Result: " << endl;
t);
• { • for(int i = 0; i < ROWS; i++) {
• cout << "Matrix 1: " << endl;
• for(int i = 0; i < ROWS; i++) { • for(int j = 0; j < COLS; j++) {
• for(int i = 0; i < ROWS; i++) { • cout << result[i][j] << " ";
• for(int j = 0; j < COLS; j++) {
• for(int j = 0; j < COLS; j++) { • }
• result[i][j] = mat1[i][j] + mat2[i][j]; • cout << matrix1[i][j] << " "; • cout << endl;
• } • } • }

• } • cout << endl; • return 0;

• } • }
• }

You might also like