0% found this document useful (0 votes)
25 views14 pages

Array Solutions

The documents demonstrate how to perform various operations on arrays in C++, including taking user input, traversing arrays, and calculating properties of array elements like sums and differences.

Uploaded by

Satwik Naskar
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)
25 views14 pages

Array Solutions

The documents demonstrate how to perform various operations on arrays in C++, including taking user input, traversing arrays, and calculating properties of array elements like sums and differences.

Uploaded by

Satwik Naskar
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/ 14

1.

#include <iostream>

using namespace std;

int main() {

const int num_inputs = 10;

int integers[num_inputs];

// Taking integer inputs from the user

cout << "Enter 10 integers:" << endl;

for (int i = 0; i < num_inputs; ++i) {

cout << "Enter integer " << i + 1 << ": ";

cin >> integers[i];

// Printing integers on the screen

cout << "Integers entered by the user:" << endl;

for (int i = 0; i < num_inputs; ++i) {

cout << integers[i] << " ";

cout << endl;

return 0;

2.

#include <iostream>

using namespace std;

int main() {

const int num_inputs = 10;


int integers[num_inputs];

// Taking integer inputs from the user

cout << "Enter 10 integers:" << endl;

for (int i = 0; i < num_inputs; ++i) {

cout << "Enter integer " << i + 1 << ": ";

cin >> integers[i];

// Asking the user for a number to search

int number;

cout << "Enter a number to search: ";

cin >> number;

// Checking if the number is present in the array

bool found = false;

for (int i = 0; i < num_inputs; ++i) {

if (integers[i] == number) {

found = true;

break;

// Informing the user about the presence of the number

if (found) {

cout << number << " is present in the array." << endl;

} else {

cout << number << " is NOT present in the array." << endl;

return 0;
}

3.

#include <iostream>

using namespace std;

int main() {

const int num_inputs = 20;

int integers[num_inputs];

// Taking integer inputs from the user

cout << "Enter 20 integers:" << endl;

for (int i = 0; i < num_inputs; ++i) {

cout << "Enter integer " << i + 1 << ": ";

cin >> integers[i];

// Counters for positive, negative, odd, and even numbers

int positive_count = 0;

int negative_count = 0;

int odd_count = 0;

int even_count = 0;

// Counting positive, negative, odd, and even numbers

for (int i = 0; i < num_inputs; ++i) {

if (integers[i] > 0) {

positive_count++;

} else if (integers[i] < 0) {

negative_count++;

if (integers[i] % 2 == 0) {
even_count++;

} else {

odd_count++;

// Printing the results

cout << "Number of positive numbers: " << positive_count << endl;

cout << "Number of negative numbers: " << negative_count << endl;

cout << "Number of odd numbers: " << odd_count << endl;

cout << "Number of even numbers: " << even_count << endl;

return 0;

4.

#include <iostream>

using namespace std;

int main() {

const int num_inputs = 10;

int original_array[num_inputs];

int reversed_array[num_inputs];

// Taking integer inputs from the user and storing them in the original array

cout << "Enter 10 integers:" << endl;

for (int i = 0; i < num_inputs; ++i) {

cout << "Enter integer " << i + 1 << ": ";

cin >> original_array[i];

// Copying elements to the reversed array in reverse order


for (int i = 0; i < num_inputs; ++i) {

reversed_array[num_inputs - 1 - i] = original_array[i];

// Printing the elements of the reversed array

cout << "Elements in the reversed array:" << endl;

for (int i = 0; i < num_inputs; ++i) {

cout << reversed_array[i] << " ";

cout << endl;

return 0;

5.

#include <iostream>

using namespace std;

int main() {

const int array_size = 5; // You can change this value to match the size of your array

int arr[array_size];

// Taking integer inputs from the user and storing them in the array

cout << "Enter " << array_size << " integers:" << endl;

for (int i = 0; i < array_size; ++i) {

cout << "Enter integer " << i + 1 << ": ";

cin >> arr[i];

// Calculate the sum of all elements

int sum = 0;

for (int i = 0; i < array_size; ++i) {


sum += arr[i];

// Calculate the product of all elements

int product = 1;

for (int i = 0; i < array_size; ++i) {

product *= arr[i];

// Printing the sum and product

cout << "Sum of all elements: " << sum << endl;

cout << "Product of all elements: " << product << endl;

return 0;

6.

#include <iostream>

using namespace std;

const int rows = 3; // Number of rows in the 2D array

const int cols = 4; // Number of columns in the 2D array

int main() {

int array2D[rows][cols];

// Initializing elements of the 2D array

cout << "Enter " << rows * cols << " integers to initialize the 2D array:" << endl;

for (int i = 0; i < rows; ++i) {

for (int j = 0; j < cols; ++j) {

cin >> array2D[i][j];

}
}

// Printing all elements of the 2D array

cout << "Elements of the 2D array:" << endl;

for (int i = 0; i < rows; ++i) {

for (int j = 0; j < cols; ++j) {

cout << array2D[i][j] << " ";

cout << endl;

return 0;

7.

#include <iostream>

using namespace std;

const int array_size = 5; // You can change this value to match the size of your array

int main() {

int arr[array_size];

// Taking integer inputs from the user and storing them in the array

cout << "Enter " << array_size << " integers:" << endl;

for (int i = 0; i < array_size; ++i) {

cout << "Enter integer " << i + 1 << ": ";

cin >> arr[i];

// Find the largest and smallest elements in the array

int largest = arr[0];


int smallest = arr[0];

for (int i = 1; i < array_size; ++i) {

if (arr[i] > largest) {

largest = arr[i];

if (arr[i] < smallest) {

smallest = arr[i];

// Printing the largest and smallest elements

cout << "Largest element: " << largest << endl;

cout << "Smallest element: " << smallest << endl;

return 0;

8.

#include <iostream>

#include <cstdlib> // For abs() function

using namespace std;

int main() {

int num_elements;

// Taking the number of elements as input

cout << "Enter the number of elements in the array: ";

cin >> num_elements;

// Check if the number of elements is valid

if (num_elements <= 1) {
cout << "Invalid input! The number of elements must be greater than 1." << endl;

return 1;

int* arr = new int[num_elements];

// Taking integer inputs from the user and storing them in the array

cout << "Enter " << num_elements << " integers:" << endl;

for (int i = 0; i < num_elements; ++i) {

cout << "Enter integer " << i + 1 << ": ";

cin >> arr[i];

// Finding the pair of elements with maximum and minimum difference

int max_difference = abs(arr[0] - arr[1]);

int min_difference = max_difference;

int max_diff_index1 = 0, max_diff_index2 = 1;

int min_diff_index1 = 0, min_diff_index2 = 1;

for (int i = 0; i < num_elements - 1; ++i) {

for (int j = i + 1; j < num_elements; ++j) {

int difference = abs(arr[i] - arr[j]);

if (difference > max_difference) {

max_difference = difference;

max_diff_index1 = i;

max_diff_index2 = j;

if (difference < min_difference) {

min_difference = difference;
min_diff_index1 = i;

min_diff_index2 = j;

// Printing the pair of elements with maximum and minimum difference

cout << "Pair of elements with maximum difference: " << arr[max_diff_index1] << " and " <<
arr[max_diff_index2] << endl;

cout << "Pair of elements with minimum difference: " << arr[min_diff_index1] << " and " <<
arr[min_diff_index2] << endl;

delete[] arr; // Freeing the dynamically allocated array

return 0;

9.

#include <iostream>

#include <cstdlib> // For abs() function

using namespace std;

int main() {

int num_elements;

// Taking the number of elements as input

cout << "Enter the number of elements in the array: ";

cin >> num_elements;

// Check if the number of elements is valid

if (num_elements <= 1) {

cout << "Invalid input! The number of elements must be greater than 1." << endl;

return 1;
}

int* arr = new int[num_elements];

// Taking integer inputs from the user and storing them in the array

cout << "Enter " << num_elements << " integers:" << endl;

for (int i = 0; i < num_elements; ++i) {

cout << "Enter integer " << i + 1 << ": ";

cin >> arr[i];

// Finding the subarray with maximum and minimum difference

int max_difference = abs(arr[0] - arr[1]);

int min_difference = max_difference;

int max_diff_index1 = 0, max_diff_index2 = 1;

int min_diff_index1 = 0, min_diff_index2 = 1;

for (int i = 0; i < num_elements - 1; ++i) {

for (int j = i + 1; j < num_elements; ++j) {

int difference = abs(arr[i] - arr[j]);

if (difference > max_difference) {

max_difference = difference;

max_diff_index1 = i;

max_diff_index2 = j;

if (difference < min_difference) {

min_difference = difference;

min_diff_index1 = i;

min_diff_index2 = j;
}

// Printing the subarray with maximum and minimum difference

cout << "Subarray with maximum difference: [";

for (int i = max_diff_index1; i <= max_diff_index2; ++i) {

cout << arr[i];

if (i != max_diff_index2) {

cout << ", ";

cout << "]" << endl;

cout << "Subarray with minimum difference: [";

for (int i = min_diff_index1; i <= min_diff_index2; ++i) {

cout << arr[i];

if (i != min_diff_index2) {

cout << ", ";

cout << "]" << endl;

delete[] arr; // Freeing the dynamically allocated array

return 0;

10.

#include <iostream>

using namespace std;


const int array_size = 5; // You can change this value to match the size of your array

int main() {

int arr[array_size];

// Taking integer inputs from the user and storing them in the array

cout << "Enter " << array_size << " integers:" << endl;

for (int i = 0; i < array_size; ++i) {

cout << "Enter integer " << i + 1 << ": ";

cin >> arr[i];

// Calculating the sum of all elements

int sum = 0;

for (int i = 0; i < array_size; ++i) {

sum += arr[i];

// Calculating the average of all elements

double average = static_cast<double>(sum) / array_size;

// Finding the smallest and largest elements in the array

int smallest = arr[0];

int largest = arr[0];

for (int i = 1; i < array_size; ++i) {

if (arr[i] < smallest) {

smallest = arr[i];

if (arr[i] > largest) {

largest = arr[i];
}

// Printing the sum, average, smallest, and largest elements

cout << "Sum of all elements: " << sum << endl;

cout << "Average of all elements: " << average << endl;

cout << "Smallest element: " << smallest << endl;

cout << "Largest element: " << largest << endl;

return 0;

You might also like