C++ Language This project has 3 parts: Part 1) In this first part, you will work
ID: 3792682 • Letter: C
Question
C++ Language
This project has 3 parts:
Part 1) In this first part, you will work with an integer array containing 100 elements initialized to random numbers from 1 to 30. After initialization, display your array. Then, call the function Frequent to find the value that occurs most frequently in the array. To do that, you will need one more array to count the occurrences of each value. When all occurrences are counted, it will be necessary to find the greatest of them. The largest element of the second array will give you the most frequent value of the original array. If you have several frequent numbers, display all of them. It is NOT necessary to return information to the main.
Required functions:
void frequent(int[],int);
void read_array(int[], int, int min_range, int max_range);
void print_array(int[], int);
The function read_array is used to initialize a new array of integers to random numbers from 1 to 30 with the help of functions rand() and srand(time(0))).
When called, the function print_array will display the current array (10 numbers per line).
Part 2) In this second part you will be tasked with finding the two largest elements in an integer array of 100 elements. The program will use the following function with the prototype:
void two_largest(int[],int, int*,int*,int*,int*);
In this part, set up a do/while loop which will be repeated until a -1 is entered from the user. On every step of the loop:
1) Use the function read_array to initialize a new array of integers to random numbers from 1 to 200 with the help of rand() and srand(time(0));
2) Use the function print_array to display the current array.
3) Call the function two_largest to find the two largest elements in the array and their subscripts (if there are two equal largest elements, detect both - they are the solution to the problem). This function MUST leave the original array unchanged. The function must return the largest elements and their subscripts using the pointers in the parameter list.
4) Display the largest elements with subscripts in your main
Part 3) In this third part, write a function that accepts an array of integers and its size as arguments. The function should create a new array that is of half size the argument array (round up the fraction if the size of the argument array is odd). The value of the first element (Element 0) of the new array should be the sum of the first two elements of the argument array (Element 0 and 1). Element 1 of the new array should be the sum of Element 2 and 3 of the argument array, and so forth. If the size of the argument array is odd, then the last element of the new array should be the same as the last element of the argument array. The function should return a pointer to the new array to the main. IN THIS FUNCTION AND THIS FUNCTION
ALONE, PLEASE USE POINTER NOTATION (NOT SUBSCRIPTS) to move through elements in the arrays. Before calling this function, display your original array. When the function call is completed, display the new array and the original array.
You can use the same functions read_array and print_array as before. Initialize the array with integer random numbers in the range from 10 to 30.
Explanation / Answer
#include<stdio.h>
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int array[100];
int temp=0;
int count=1;
int frequent=array[0];
//to insert the values in the array
for(int i=0;i<100;i++)
{
int s=rand()%30+1;
array[i]=s;
}
//to find most frequent value in tha array
for(int j=0;j<100;j++)
{
int tempCount=0;
temp=array[j];
tempCount++;
for(int k=j+1;k<100;k++)
{
if(array[k] == temp)
{
tempCount++;
if(tempCount > count)
{
frequent = temp;
count = tempCount;
}
}
}
}
cout<<"most popular element is "<<frequent<< "the count is "<<count <<endl;
//to display the between the values
cout<<"enter which range to which range you need to read the values in the array need to enter the values between 1 to 100"<<endl;
cout<<"enter the low value"<<endl;
int lowerbound=0;
cin>>lowerbound;
cout<<"enter the upper bound"<<endl;
int upperbound=0;
cin>>upperbound;
for(int i=lowerbound;i<upperbound;i++)
{
cout<<"between the value "<<array[i]<<" ";
}
//for displaying the array values
for(int i=0;i<100;i++)
{
cout<<array[i]<<endl;
}
}
output
most popular element is 22the count is 6
enter which range to which range you need to read the values in the ar
o enter the values between 1 to 100
enter the low value
25
enter the upper bound
50
between the value 23
between the value 22
between the value 27
between the value 9
between the value 6
between the value 18
between the value 7
between the value 12
between the value 19
between the value 10
between the value 23
between the value 18
between the value 20
between the value 26
between the value 25
between the value 24
between the value 22
between the value 3
between the value 4
between the value 4
between the value 15
between the value 22
between the value 2
between the value 24
between the value 29
12
18
5
11
30
5
19
19
23
15
6
6
2
28
2
12
26
3
28
7
22
25
3
4
23
23
22
27
9
6
18
7
12
19
10
23
18
20
26
25
24
22
3
4
4
15
22
2
24
29
18
15
23
28
28
20
24
22
20
29
17
6
21
13
19
17
11
3
5
29
27
16
21
10
11
21
7
22
4
9
10
24
25
5
7
21
17
27
12
29
25
10
27
14
18
29
9
13
10
12
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.