In: Computer Science
Program :
1.
#include<iostream>
#include<cstdlib>
using namespace std;
int main() {
srand(time(NULL)); // time seed
int arr[5][5], i, j;
double sum;
cout << "\nThe 2D array is: ";
for (i = 0; i < 5; i++) {
cout << "\n";
for (j = 0; j < 5; j++) {
arr[i][j] = 1 + (rand() % 100); // generating random numbers for array
cout << arr[i][j] << " ";
}
}
for (i = 0; i < 5; i++) {
sum = 0;
for (j = 0; j < 5; j++)
sum += arr[i][j];
cout << "\nAverage of row - " << i + 1 << " is: " << sum / 5 << "\n"; // prints average of row
}
for (j = 0; j < 5; j++) {
sum = 0;
for (i = 0; i < 5; i++)
sum += arr[i][j];
cout << "\nAverage of column - " << j + 1 << " is: " << sum / 5 << "\n"; // prints average of column
}
return 0;
}
2.
#include<iostream>
#include<cstdlib>
using namespace std;
int* evenFind(int *a) {
int *eve = new int[10], j = 0;
for (int i = 0; i < 10; i++) {
if (a[i] % 2 == 0) {
eve[j] = a[i];
j++; // if array contains even numbers increase count by 1
}
}
return eve;
}
int main() {
srand(time(NULL));
int *arr = new int[10], i;
cout << "\nThe array is: ";
for (i = 0; i < 10; i++) {
arr[i] = 1 + (rand() % 100);
cout << arr[i] << " ";
}
arr = evenFind(arr);
cout << "\nThe array with even numbers is: ";
for (i = 0; i < 10; i++) {
if (arr[i] == 0)
continue;
else
cout << arr[i] << " "; // show the even elements in new array
}
cout << "\n";
}
3.
#include<iostream>
#include<cstdlib>
using namespace std;
int countFind(int *a, int v) {
int j = 0;
for (int i = 0; i < 10; i++) {
if (a[i] >= v)
j++; // increase count if array element is greater than value
}
return j;
}
int main() {
srand(time(NULL));
int *arr = new int[10], i, val, count;
cout << "\nThe array is: ";
for (i = 0; i < 10; i++) {
arr[i] = 1 + (rand() % 100);
cout << arr[i] << " ";
}
cout << "\nEnter the value: ";
cin >> val;
count = countFind(arr, val);
cout << "\nThe number of array elements greater than or equal to " << val << " is: " << count << "\n"; // show the count
}
4.
#include<iostream>
using namespace std;
int* swap(int *a) {
int temp;
for (int i = 0; i < 10; i += 2) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
return a; // swapping adjascent numbers and returning the array
}
int main() {
int arr[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}, *sw, i;
cout << "\nThe array is: ";
for (i = 0; i < 10; i++)
cout << arr[i] << " ";
sw = swap(arr);
cout << "\nThe array with swapped adjascent numbers is: ";
for (i = 0; i < 10; i++)
cout << sw[i] << " "; // displaying swapped elements of array
cout << "\n";
}
5.
#include<iostream>
#include<cstdlib>
using namespace std;
void findMedian(int *, int *, int, int *);
void findMode(int *a) {
int b[100], max = 0, i;
for (i = 0; i < 100; i++) {
if (a[i] > max)
max = a[i];
}
int n = max + 1, count[n];
for (i = 0; i < n; i++)
count[i] = 0;
for (i = 0; i < 100; i++)
count[a[i]]++;
int mode = 0, k = count[0];
for (i = 1; i < n; i++) {
if (count[i] > k) {
k = count[i]; // mode is the index with largest count
mode = i; // calculating mode
}
}
cout << "\nThe mode of the array is:" << mode << "\n";
findMedian(a, count, n, b);
}
void findMedian(int *a, int *count, int n, int *b) {
for (int i = 1; i < n; i++)
count[i] = count[i] + count[i - 1]; // updating count with summing
for (int i = 0; i < 100; i++) {
b[count[a[i]] - 1] = a[i];
count[a[i]]--; // sorting count array to calculate median
}
float median = (b[(n - 1) / 2] + b[(n / 2)]) / 2.0; // calculating mean for even array size 100
cout << "\nThe median of the array is: " << median << "\n";
}
int main() {
srand(time(NULL));
int *arr = new int[100], *m, i;
cout << "\nThe array is: ";
for (i = 0; i < 100; i++) {
arr[i] = 1 + (rand() % 100);
cout << arr[i] << " ";
}
findMode(arr);
}
Output :