1D Array and 2d array C++
Questions
iam sending you a link of file ... it is for my competitive coding
subject create all the questions related to that file so that i can
print this chatgpt and take it as notes in my exam beacuse its is
open book and open notes exam.......................actually iam not
able upload it.. i will tell you topics
1. 1 D array solve all those programming question that might be
important for that exam please solve all in c++
Got it! I'll generate a set of programming questions related to 1D
arrays in C++ that are likely to be relevant for your exam and solve
them as well. Let’s dive right in:
1D Array Questions and Solutions
Question 1: Sum of All Elements
Write a program to calculate the sum of all elements in a 1D array.
Solution:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/21
cpp
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
cout << "Sum of all elements: " << sum << endl;
return 0;
}
Question 2: Find the Largest and Smallest Element
Write a program to find the largest and smallest elements in a 1D
array.
Solution:
cpp
#include <iostream>
#include <climits>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/21
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int largest = INT_MIN, smallest = INT_MAX;
for (int i = 0; i < n; i++) {
if (arr[i] > largest) largest = arr[i];
if (arr[i] < smallest) smallest = arr[i];
}
cout << "Largest element: " << largest << endl;
cout << "Smallest element: " << smallest << endl;
return 0;
}
Question 3: Reverse the Array
Write a program to reverse a 1D array.
Solution:
cpp
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Reversed array: ";
for (int i = n - 1; i >= 0; i--) {
cout << arr[i] << " ";
}
cout << endl;
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/21
return 0;
}
Question 4: Count Occurrences of an Element
Write a program to count the number of occurrences of a given
element in a 1D array.
Solution:
cpp
#include <iostream>
using namespace std;
int main() {
int n, x, count = 0;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Enter the element to count: ";
cin >> x;
for (int i = 0; i < n; i++) {
if (arr[i] == x) count++;
}
cout << "Occurrences of " << x << ": " << count << end
l;
return 0;
}
Question 5: Check if Array is Sorted
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/21
Write a program to check if the given 1D array is sorted in non-
decreasing order.
Solution:
cpp
#include <iostream>
using namespace std;
int main() {
int n;
bool isSorted = true;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 1; i < n; i++) {
if (arr[i] < arr[i - 1]) {
isSorted = false;
break;
}
}
if (isSorted) {
cout << "Array is sorted." << endl;
} else {
cout << "Array is not sorted." << endl;
}
return 0;
}
Question 6: Rotate an Array
Write a program to rotate a 1D array to the left by `k` positions.
Solution:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/21
cpp
#include <iostream>
using namespace std;
void rotateArray(int arr[], int n, int k) {
k %= n;
int temp[k];
for (int i = 0; i < k; i++) {
temp[i] = arr[i];
}
for (int i = 0; i < n - k; i++) {
arr[i] = arr[i + k];
}
for (int i = 0; i < k; i++) {
arr[n - k + i] = temp[i];
}
}
int main() {
int n, k;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Enter the number of rotations: ";
cin >> k;
rotateArray(arr, n, k);
cout << "Array after rotation: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Question 7: Find Second Largest Element
Write a program to find the second largest element in a 1D array.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/21
Solution:
cpp
#include <iostream>
#include <climits>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int largest = INT_MIN, secondLargest = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != lar
gest) {
secondLargest = arr[i];
}
}
if (secondLargest == INT_MIN) {
cout << "No second largest element." << endl;
} else {
cout << "Second largest element: " << secondLarges
t << endl;
}
return 0;
}
These questions and solutions cover a broad range of operations on
1D arrays and should be helpful for your competitive coding subject.
Let me know if you’d like additional problems or clarifications!
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/21
2 d arrays
Here’s a set of 2D array questions and solutions in C++ to help with
your open-notes exam:
2D Array Questions and Solutions
Question 1: Sum of All Elements
Write a program to calculate the sum of all elements in a 2D array.
Solution:
cpp
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows and columns: ";
cin >> rows >> cols;
int arr[rows][cols];
cout << "Enter the elements of the array: ";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> arr[i][j];
}
}
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum += arr[i][j];
}
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/21
cout << "Sum of all elements: " << sum << endl;
return 0;
}
Question 2: Transpose of a Matrix
Write a program to find the transpose of a 2D array (matrix).
Solution:
cpp
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows and columns: ";
cin >> rows >> cols;
int arr[rows][cols], transpose[cols][rows];
cout << "Enter the elements of the array: ";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> arr[i][j];
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = arr[i][j];
}
}
cout << "Transpose of the matrix:" << endl;
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
cout << transpose[i][j] << " ";
}
cout << endl;
}
return 0;
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/21
Question 3: Matrix Multiplication
Write a program to multiply two 2D arrays (matrices).
Solution:
cpp
#include <iostream>
using namespace std;
int main() {
int rows1, cols1, rows2, cols2;
cout << "Enter the dimensions of the first matrix (row
s and columns): ";
cin >> rows1 >> cols1;
cout << "Enter the dimensions of the second matrix (ro
ws and columns): ";
cin >> rows2 >> cols2;
if (cols1 != rows2) {
cout << "Matrix multiplication not possible!" << e
ndl;
return 0;
}
int mat1[rows1][cols1], mat2[rows2][cols2], result[row
s1][cols2] = {0};
cout << "Enter elements of the first matrix:" << endl;
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
cin >> mat1[i][j];
}
}
cout << "Enter elements of the second matrix:" << end
l;
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
cin >> mat2[i][j];
}
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 10/21
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
cout << "Resultant matrix after multiplication:" << en
dl;
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}
Question 4: Find the Row with Maximum Sum
Write a program to find the row in a 2D array with the maximum
sum.
Solution:
cpp
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows and columns: ";
cin >> rows >> cols;
int arr[rows][cols];
cout << "Enter the elements of the array: ";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> arr[i][j];
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 11/21
}
int maxSum = 0, rowIndex = 0;
for (int i = 0; i < rows; i++) {
int rowSum = 0;
for (int j = 0; j < cols; j++) {
rowSum += arr[i][j];
}
if (rowSum > maxSum) {
maxSum = rowSum;
rowIndex = i;
}
}
cout << "Row with maximum sum: " << rowIndex + 1 << "
(Sum: " << maxSum << ")" << endl;
return 0;
}
Question 5: Search an Element in a Matrix
Write a program to search for an element in a 2D array and print its
position if found.
Solution:
cpp
#include <iostream>
using namespace std;
int main() {
int rows, cols, x;
cout << "Enter the number of rows and columns: ";
cin >> rows >> cols;
int arr[rows][cols];
cout << "Enter the elements of the array: ";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> arr[i][j];
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 12/21
}
cout << "Enter the element to search: ";
cin >> x;
bool found = false;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] == x) {
cout << "Element found at position: (" <<
i << ", " << j << ")" << endl;
found = true;
}
}
}
if (!found) {
cout << "Element not found in the matrix." << end
l;
}
return 0;
}
Question 6: Diagonal Sum
Write a program to calculate the sum of the main diagonal elements
of a square matrix.
Solution:
cpp
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the size of the square matrix: ";
cin >> n;
int arr[n][n];
cout << "Enter the elements of the matrix: ";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 13/21
cin >> arr[i][j];
}
}
int diagonalSum = 0;
for (int i = 0; i < n; i++) {
diagonalSum += arr[i][i];
}
cout << "Sum of main diagonal elements: " << diagonalS
um << endl;
return 0;
}
These problems cover core operations and algorithms related to 2D
arrays in C++ and should be excellent preparation material for your
exam. Let me know if you need additional problems or explanations!
sum of elements divisible by 3
Here's a program to calculate the sum of elements in a 2D array
that are divisible by 3:
Program: Sum of Elements Divisible by 3
cpp
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter the number of rows and columns: ";
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 14/21
cin >> rows >> cols;
int arr[rows][cols];
cout << "Enter the elements of the array: ";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> arr[i][j];
}
}
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] % 3 == 0) {
sum += arr[i][j];
}
}
}
cout << "Sum of elements divisible by 3: " << sum << e
ndl;
return 0;
}
How it Works
1. Input: The user specifies the number of rows and columns and
then provides elements of the 2D array.
2. Logic:
Loop through the entire array.
Check if each element is divisible by 3 using the modulo
operator (`%`).
If divisible, add it to the sum.
3. Output: Print the sum of all elements divisible by 3.
Example Input and Output
Input:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 15/21
mathematica
Enter the number of rows and columns: 2 3
Enter the elements of the array: 3 5 6 9 12 8
Output:
csharp
Sum of elements divisible by 3: 30
Let me know if you'd like additional variations or optimizations!
index is divisible by 5 and element is divisible by 7 average..
Here’s a program to calculate the average of elements in a 2D
array where the index is divisible by 5 and the element is divisible
by 7:
Program: Average of Elements with Specific Conditions
cpp
#include <iostream>
using namespace std;
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 16/21
int main() {
int rows, cols;
cout << "Enter the number of rows and columns: ";
cin >> rows >> cols;
int arr[rows][cols];
cout << "Enter the elements of the array: ";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> arr[i][j];
}
}
int sum = 0, count = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// Check if the index (i, j) is divisible by 5
and element is divisible by 7
if ((i * cols + j) % 5 == 0 && arr[i][j] % 7 =
= 0) {
sum += arr[i][j];
count++;
}
}
}
if (count > 0) {
double average = (double)sum / count;
cout << "Average of elements with index divisible
by 5 and element divisible by 7: " << average << endl;
} else {
cout << "No elements satisfy the condition." << en
dl;
}
return 0;
}
How it Works
1. Input:
The user inputs the number of rows and columns,
followed by the elements of the 2D array.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 17/21
2. Logic:
The program checks each element to see if both
conditions are met:
The index (calculated as `i * cols + j` for 2D index
flat representation) is divisible by 5.
The element is divisible by 7.
If both conditions are satisfied, the element is added to
the sum, and a counter (`count`) is incremented.
3. Output:
If any elements meet the conditions, the program
calculates and prints the average of those elements.
If no elements satisfy the condition, it outputs a message
indicating no matching elements were found.
Example Input and Output
Input:
mathematica
Enter the number of rows and columns: 3 4
Enter the elements of the array: 7 10 21 14 5 35 49 28 42
56 63 70
Output:
csharp
Average of elements with index divisible by 5 and element
divisible by 7: 42.0
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 18/21
Explanation:
In the example, the element at index `(0, 0)` is `7`, which is
divisible by 7 and its index (0) is divisible by 5.
Other elements are checked similarly.
Let me know if you'd like further modifications!
reverse of an array
Here’s a simple C++ program to reverse a 1D array:
Program: Reverse of a 1D Array
cpp
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
// Reverse the array
int start = 0, end = n - 1;
while (start < end) {
// Swap elements at start and end
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 19/21
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
// Move towards the middle
start++;
end--;
}
cout << "Reversed array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
How it Works
1. Input:
The program first asks for the number of elements in the
array and then the elements themselves.
2. Reversal Logic:
The array is reversed by swapping elements from the start
with the end. We continue swapping until the start index
is greater than or equal to the end index.
3. Output:
After reversing the array, the program prints the reversed
array.
Example Input and Output
Input:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 20/21
mathematica
Enter the number of elements: 5
Enter the elements: 1 2 3 4 5
Output:
Reversed array: 5 4 3 2 1
Let me know if you'd like further clarification or more examples!
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 21/21