Question

In: Computer Science

C++ please 1. Randomly assign integers in the range of 1-100 to a two-dimensional array. Write...

C++ please

1. Randomly assign integers in the range of 1-100 to a two-dimensional array. Write a program that finds the average value of the rows and the average value of the columns. Display the averages.
2. Create an array of randomly generated numbers in any range. Write a function that takes the array as an argument and returns an array that consists of only the even numbers in the original array. Use the function in a program.
3. Create an array of randomly generated numbers ranging from 1 to 100. Write a function that counts the number of array elements greater than or equal to a given value and returns the count. Use the function in a program.
4. Create an array of ten values using an initializer list. Write a function that swaps adjacent values, so that an array with the elements [1,2,3,4] becomes [2,1,4,3], but for 10 elements and not just 4. Display both the original array and the new array in your program.
5. Look up the definition of median and mode. Create an array of 100 randomly generated integers. Write a function that determines the median value of the array and a function that determines the mode of the array. Use the functions in a program.

Solutions

Expert Solution

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 :


Related Solutions

(a) Write an Ada declaration for a rectangular two dimensional array of integers named A, assuming that the subscripts will range from 1 to 10 in each dimension.
Problem 1. (a) Write an Ada declaration for a rectangular two dimensional array of integers named A, assuming that the subscripts will range from 1 to 10 in each dimension.(b) Write an Ada declaration for a one-dimensional array named A, where each element in the array is a one-dimensional array of integers. Assume that the subscripts for both will range from 1 to 10.(c) What are the advantages of declaring the array as described in part (a)?(d) What are the...
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names...
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names intArray17.b. Create a loop to calculate the sum of every element in the first column.c. Create a loop to calculate the sum of every element in the array.
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names...
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names intArray17. Create a loop to calculate the sum of every element in the first column. Create a loop to calculate the sum of every element in the array.
Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0.
This program is for C.Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0. Combine the elements of A + B to create two- dimensional array C = A + B. Display array A, B and C to the screen for comparison. (Note a[0] + b[0] = c[0], a[1] + b[1] = c[1], etc.)
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements....
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements. Fill the array with random integers (use a loop). Neatly output each element in the one-dimensional array. Next convert your one-dimensional array of 24 elements into a two-dimensional array of 6 x 4 elements. Neatly output each element of the two-dimensional array. The values will be identical to the one-dimensional array – you’re just converting from one dimension to two.
One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable...
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable array size. Your class should support storing an integer at a specific index value, retrieving the integer at a specific index value, and automatically increasing storage for the saved values.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT