Array Solutions
Array Solutions
#include <iostream>
int main() {
int integers[num_inputs];
return 0;
2.
#include <iostream>
int main() {
int number;
if (integers[i] == number) {
found = true;
break;
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>
int main() {
int integers[num_inputs];
int positive_count = 0;
int negative_count = 0;
int odd_count = 0;
int even_count = 0;
if (integers[i] > 0) {
positive_count++;
negative_count++;
if (integers[i] % 2 == 0) {
even_count++;
} else {
odd_count++;
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>
int main() {
int original_array[num_inputs];
int reversed_array[num_inputs];
// Taking integer inputs from the user and storing them in the original array
reversed_array[num_inputs - 1 - i] = original_array[i];
return 0;
5.
#include <iostream>
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;
int sum = 0;
int product = 1;
product *= arr[i];
cout << "Sum of all elements: " << sum << endl;
cout << "Product of all elements: " << product << endl;
return 0;
6.
#include <iostream>
int main() {
int array2D[rows][cols];
cout << "Enter " << rows * cols << " integers to initialize the 2D array:" << endl;
}
}
return 0;
7.
#include <iostream>
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;
largest = arr[i];
smallest = arr[i];
return 0;
8.
#include <iostream>
int main() {
int num_elements;
if (num_elements <= 1) {
cout << "Invalid input! The number of elements must be greater than 1." << endl;
return 1;
// Taking integer inputs from the user and storing them in the array
cout << "Enter " << num_elements << " integers:" << endl;
max_difference = difference;
max_diff_index1 = i;
max_diff_index2 = j;
min_difference = difference;
min_diff_index1 = i;
min_diff_index2 = j;
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;
return 0;
9.
#include <iostream>
int main() {
int num_elements;
if (num_elements <= 1) {
cout << "Invalid input! The number of elements must be greater than 1." << endl;
return 1;
}
// Taking integer inputs from the user and storing them in the array
cout << "Enter " << num_elements << " integers:" << endl;
max_difference = difference;
max_diff_index1 = i;
max_diff_index2 = j;
min_difference = difference;
min_diff_index1 = i;
min_diff_index2 = j;
}
if (i != max_diff_index2) {
if (i != min_diff_index2) {
return 0;
10.
#include <iostream>
int main() {
int arr[array_size];
// Taking integer inputs from the user and storing them in the array
cout << "Enter " << array_size << " integers:" << endl;
int sum = 0;
sum += arr[i];
smallest = arr[i];
largest = arr[i];
}
cout << "Sum of all elements: " << sum << endl;
cout << "Average of all elements: " << average << endl;
return 0;