1D Array and 2d array C++ Questions
1D Array and 2d array C++ Questions
Questions
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;
}
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;
}
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;
}
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;
}
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;
}
Solution:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/21
cpp
#include <iostream>
using namespace std;
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;
}
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;
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/21
2 d arrays
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;
}
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
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;
}
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];
}
}
}
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
}
cout << "Row with maximum sum: " << rowIndex + 1 << "
(Sum: " << maxSum << ")" << endl;
return 0;
}
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;
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;
}
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];
}
}
}
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.
Input:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 15/21
mathematica
Output:
csharp
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];
}
}
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.
Input:
mathematica
Output:
csharp
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 18/21
Explanation:
reverse of an 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];
}
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;
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.
Input:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 20/21
mathematica
Output:
Reversed array: 5 4 3 2 1
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 21/21