0% found this document useful (0 votes)
30 views4 pages

Day 6 Arrays

The document provides an overview of arrays in C++, including one-dimensional, two-dimensional, and multi-dimensional arrays, along with their syntax and examples. It explains how to declare, initialize, and access elements in these arrays, as well as a program for matrix multiplication. Additionally, it includes C++ array mastery drills for practical application of the concepts learned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

Day 6 Arrays

The document provides an overview of arrays in C++, including one-dimensional, two-dimensional, and multi-dimensional arrays, along with their syntax and examples. It explains how to declare, initialize, and access elements in these arrays, as well as a program for matrix multiplication. Additionally, it includes C++ array mastery drills for practical application of the concepts learned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Day 6 : Arrays

ARRAYS:
An array is a collection of elements of the same data type, stored in contiguous
memory locations.

TYPES:
1. One-dimensional Arrays:

It contains either row or column.


SYNTAX:
data_type array_name[array_size ];

data_type: The data type of the elements in the array .

array_name: The name of the array .

array_size : The number of elements in the array .

PROGRAM:

#include <iostream>

int main() {
// One-dimensional integer array
int numbers[5] = {10, 20, 30, 40, 50};

// Accessing elements of the array using index


std::cout << "Element at index 2: " << numbers[2] << std::en

return 0;
}

OUTPUT:
Element at index 2: 30

EXPLANATION:
In this example, we declare a one-dimensional integer array called numbers with
five elements. The elements of the array are initialized with the values 10, 20, 30, 40,
and 50, respectively. The index of arrays in C++ starts from 0, so numbers[0] refers
to the first element (10), numbers[1] to the second element (20), and so on. We
access the element at index 2 using numbers[2], which is 30. The program then
prints "Element at index 2: 30" to the console.

Day 6 : Arrays 1
2. two-dimensional Arrays:
It contains both row and column.
SYNTAX:
data_type array_name [row_size ][column_size];

data_type: The data type of the elements in the array.

array_name : The name of the array .

row size: The number of rows in the array .

column _size : The number of columns in the array .

PROGRAM:

#include <iostream>

int main() {
// Multi-dimensional integer array (2x3)
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };

// Accessing elements of the multi-dimensional array using i


std::cout << "Element at (1, 2): " << matrix[1][2] << std::e

return 0;
}

OUTPUT:

Element at (1, 2): 6

EXPLANATION:
In this example, we declare a two-dimensional integer array called matrix with 2 rows
and 3 columns. The elements of the array are initialized with the values 1, 2, 3, 4, 5,
and 6. To access elements of the multi-dimensional array, we use two indexes: the
first index specifies the row number, and the second index specifies the column
number. In this case, matrix[1][2] refers to the element in the second row (index 1)
and the third column (index 2), which is 6. The program then prints "Element at (1,
2): 6" to the console.

3.Multi-dimensional Arrays

Multi-dimensional arrays in C++ are arrays that have more than one dimension. They
are essentially arrays of arrays.
SYNTAX:
// Declaration of a two-dimensional array
data_type array_name[row_size][column_size];

// Declaration of a three-dimensional array


data_type array_name[x_size][y_size][z_size];

Day 6 : Arrays 2
PROGRAM:

#include<iostream>

int main() {
int a[2][2], b[2][2], c[2][2], i, j, k;

std::cout << "Enter A Matrix\n";


for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
std::cin >> a[i][j];
}
}

std::cout << "Enter B Matrix\n";


for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
std::cin >> b[i][j];
}
}

for (i = 0; i < 2; i++) {


for (j = 0; j < 2; j++) {
c[i][j] = 0;
for (k = 0; k < 2; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}

std::cout << "Multiplication Matrix\n";


for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
std::cout << c[i][j] << "\t";
}
std::cout << "\n";
}

return 0;
}

EXPLANATION:

1. Array Declaration:

int a[2][2], b[2][2], c[2][2], i, j, k;

Declares matrices A, B, and C, and loop control variables i, j, k.

Day 6 : Arrays 3
2. Input for Matrix A and B:

Asks the user to input values for matrices A and B using nested loops.

3. Matrix Multiplication (C = A * B):

Nested loops to iterate over each element of matrices A, B, and C.

c[i][j] is calculated as the sum of products of corresponding elements of A and


B.

4. Output Resultant Matrix C:

Prints the resulting matrix C to the console.

5. Return Statement:

return 0;

Indicates the successful completion of the program.

In summary, this program performs matrix multiplication of two 2x2 matrices (A and B)
and outputs the resulting matrix (C). The user is prompted to input values for matrices A
and B, and the program calculates the product matrix.

C++ Array Mastery Drills

1. Design a basic calculator program using an array to store numerical values.


Implement functionalities for addition, subtraction, multiplication, and division. Allow
the user to perform multiple operations in sequence.

2. Create a to-do list manager using an array to store tasks. Implement features to add
tasks, mark tasks as completed, and display the current list of tasks. Consider
adding a feature to remove completed tasks.

3. Develop a simple "Guess the Number" game. Use an array to store a predefined list
of numbers. Have the program randomly select a number for the user to guess.
Provide feedback on whether the user's guess is too high, too low, or correct.

4. Build a program that generates multiplication tables. Use an array to store the
results of multiplication for a specific number. Allow the user to input a number, and
then display the multiplication table for that number.

5. Create a program that counts the occurrences of words in a given sentence. Use an
array to store unique words and another array to store the corresponding counts.
Display the word frequency at the end.

Day 6 : Arrays 4

You might also like