0% found this document useful (0 votes)
3 views

Array

Uploaded by

kuttukuttan126
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Array

Uploaded by

kuttukuttan126
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

VeryShortAnswer:

1: "All the elements in an array must be the same data type."


2: "The elements of an array with ten elements are numbered from 0 to 9."
3: "An array element is accessed using an index or subscript."
4: "If AR is an array, AR[7] refers to the 8th element."
5: "For int a[3] = {2, 3, 4};, the value of a[1] is 3."
6: "For int a[] = {1, 2, 4};, the value of a[1] is 2."
7: "For int a[5] = {1, 2, 4};, the value of a[4] is 0 (default)."
8: "Array initialization: int scores[6] = {89, 75, 82, 93, 78, 95};"
9: "Printing all elements of an array is an example of a traversal operation."
10: "24 bytes are required to store int a[2][3] (2 x 3 x 4 bytes per int)."
11: "If elements are doubled, binary search requires log2(2m) searches."

ShortAnswer:
1: "An Array is a collection of elements of the same data type stored in contiguous memory locations."
2: "The declaration int studlist[1000]; creates an array named studlist with space for 1000 integers."
3: "Memory for a single-dimensional array is allocated contiguously in stack or heap."
4: |
"C++ Code to count even and odd numbers in an array of 10 elements:"
```cpp
int arr[10], evenCount = 0, oddCount = 0;
for (int i = 0; i < 10; i++) {
cin >> arr[i];
if (arr[i] % 2 == 0) evenCount++;
else oddCount++;
}
cout << "Even: " << evenCount << ", Odd: " << oddCount;
```
5: "Initialization for array num: int num[] = {2, 3, 4, 5};"
6: "Traversal is the process of accessing each element of an array sequentially."
7: "Sorting arranges elements of an array in ascending or descending order."
8: "Searching is finding the location of a specific element in an array."
9: "Bubble sort is a sorting algorithm where adjacent elements are compared and swapped if necessary."
10: "Binary search is a searching algorithm that repeatedly divides a sorted array to find an element."
11: "A 2D array is an array of arrays stored in a matrix format with rows and columns."
12: "Memory for a 2D array is allocated contiguously row-wise."

LongAnswer:
1:
Question: "Illustrate the working of binary search technique for searching an element 45 in the array AR = {25, 81, 36, 15, 45, 58, 70}."
Answer: |
Step-by-step working of Binary Search:
- Step 1: Sort the array: {15, 25, 36, 45, 58, 70, 81}.
- Step 2: Start with `low = 0` and `high = 6`.
- Step 3: Calculate `mid = (low + high) / 2` `mid = 3`.
- Step 4: Compare `AR[mid]` with 45:
- `AR[3] = 45`. Match found.
- Output: The element 45 is found at index 3 in the sorted array.

2:
Question: "Write C++ statements to accept two single-dimensional arrays of equal length and find the difference between corresponding
elements."
Answer: |
```cpp
int arr1[5], arr2[5], diff[5];
for (int i = 0; i < 5; i++) {
cin >> arr1[i];
}
for (int i = 0; i < 5; i++) {
cin >> arr2[i];
diff[i] = arr1[i] - arr2[i];
}
for (int i = 0; i < 5; i++) {
cout << diff[i] << " ";
}
```

3:
Question: "Illustrate the working of bubble sort method for sorting the elements {32, 25, 44, 16, 37, 12}."
Answer: |
Step-by-step working of Bubble Sort:
- Pass 1: Compare adjacent elements, swap if necessary:
- {25, 32, 16, 37, 12, 44}
- Pass 2: Repeat:
- {25, 16, 32, 12, 37, 44}
- Pass 3: Repeat:
- {16, 25, 12, 32, 37, 44}
- Pass 4: Repeat:
- {16, 12, 25, 32, 37, 44}
- Pass 5: Repeat:
- {12, 16, 25, 32, 37, 44}
- Final Output: {12, 16, 25, 32, 37, 44}

4:
Question: "Illustrate the working of selection sort for sorting the elements {24, 45, 98, 56, 76, 24, 15}."
Answer: |
Step-by-step working of Selection Sort:
- Pass 1: Find smallest element (15) and swap with 1st position.
- {15, 45, 98, 56, 76, 24, 24}
- Pass 2: Find next smallest (24) and swap with 2nd position.
- {15, 24, 98, 56, 76, 45, 24}
- Repeat steps until sorted:
- Final Output: {15, 24, 24, 45, 56, 76, 98}
5:
Question: "Write a program to find the difference between two matrices."
Answer: |
```cpp
int mat1[3][3], mat2[3][3], diff[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> mat1[i][j];
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> mat2[i][j];
diff[i][j] = mat1[i][j] - mat2[i][j];
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << diff[i][j] << " ";
}
cout << endl;
}
```

6:
Question: "Write a program to find the sum and average of elements in a 2D array."
Answer: |
```cpp
int arr[3][3], sum = 0, count = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> arr[i][j];
sum += arr[i][j];
count++;
}
}
double average = (double)sum / count;
cout << "Sum: " << sum << ", Average: " << average;
```

7:
Question: "Write a program to find the largest element in a 2D array."
Answer: |
```cpp
int arr[3][3], maxElement = INT_MIN;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> arr[i][j];
if (arr[i][j] > maxElement) {
maxElement = arr[i][j];
}
}
}
cout << "Largest Element: " << maxElement;
```

You might also like