Day 6 Arrays
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:
PROGRAM:
#include <iostream>
int main() {
// One-dimensional integer array
int numbers[5] = {10, 20, 30, 40, 50};
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];
PROGRAM:
#include <iostream>
int main() {
// Multi-dimensional integer array (2x3)
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };
return 0;
}
OUTPUT:
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];
Day 6 : Arrays 2
PROGRAM:
#include<iostream>
int main() {
int a[2][2], b[2][2], c[2][2], i, j, k;
return 0;
}
EXPLANATION:
1. Array Declaration:
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.
5. Return Statement:
return 0;
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.
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