I need this in C++
Write a function using the following structure and prototype. struct Stats { float avg; //Average value of an integer array float median; //Median value of an integer array int *mode; //array containing the modes int nModes; //number of modes in the array int maxFreq; //max frequency of modes }; Stats *avgMedMode(int *,int); The function takes in an integer array and the size of the array. Then returns a pointer to a structure containing the average, median and mode. You will then write a printStat() function that will print the results contained in the stats structure. I will input a small integer array to test this out so ask for how many inputs to fill the array, then the values to place into the array. Make sure you delete the dynamic array creation for the mode when you exit the problem. Note: The array may have no modes, 1 mode or many modes.
#include<iostream>
#include<algorithm>
using namespace std;
struct Stats
{
float avg; //Average value of an integer array
float median; //Median value of an integer array
int *mode; //array containing the modes
int nModes; //number of modes in the array
int maxFreq; //max frequency of modes
};
Stats *avgMedMode(int *,int);
Stats *avgMedMode(int *a,int n)
{
float avg=0,sum=0;
for(int i=0;i<n;i++)
sum=sum+a[i];
// cout<<"sum = "<<sum<<endl;
avg=sum/n;
sort(&a[0],&a[n]);//sort all the elements
// for(int i=0;i<n;i++)
// cout<<a[i]<<" ";
float median=0;
if(n%2==0)
{
int mid=n/2;
median=(a[mid]+a[mid-1])/2.0;
//if n is even, then median is the average of two middle values,
//here index starts from 0, so middle values are, eg. for n=10
//so index 0 to 9, middle are indexes 4 and 5, so 10/2=5 and 5-1=4
}
else
{
median=a[n/2];//index starts at 0, so middle is n/2
}
int b[n][2];
//b is 2d array has b[i][0] as the element itself from a[i]
//and b[i][1] as its count calculated in the below loop
int index=0;//index of b array
int temp=a[0],count=1;
//initially 1 element to temp, start i at 1
//to compare, and count =1 (initially as the element is counted which is im temp)
for(int i=1;i<n;i++)
{
if(temp==a[i])
{
count++;
}
else
{
b[index][0]=temp;
b[index][1]=count;
index++;
temp=a[i];
count=1;
//store the count and element in b, change temp and reset the count
}
if(i==n-1)//at the last element, we also need to store whatever is there in temp and count to b
{
b[index][0]=temp;
b[index][1]=count;
index++;//now index is the count of all distinct elements, size of b
}
}
// for(int i=0;i<index;i++)
// cout<<b[i][0]<<" "<<b[i][1]<<endl;
int maxCount=0;
for(int i=0;i<index;i++)
{
if(maxCount<b[i][1])
maxCount=b[i][1];
}
// cout<<"max Count= "<<maxCount<<endl;
int numberMaxCounts=0;//will store the number number of modes
for(int i=0;i<index;i++)
if(b[i][1]==maxCount)
{
numberMaxCounts++;
}
int *mode;
if(numberMaxCounts==index || n==0)//if all elements have equal count, then there is no mode
{
mode='\0';
maxCount=0;
numberMaxCounts=0;
if(n==0)
{
avg=0;
median=0;
}
}
else
{
int ind=0;
mode=new int[numberMaxCounts];
for(int i=0;i<index;i++)
{
if(b[i][1]==maxCount)
mode[ind++]=b[i][0];
}
}
Stats *s=new Stats();
s->avg=avg;
s->median=median;
s->maxFreq=maxCount;
s->nModes=numberMaxCounts;
s->mode=mode;
return s;
}
void printStat(Stats s)
{
cout<<"\nStats are :\n";
cout<<"Average : "<<s.avg<<endl;
cout<<"Median : "<<s.median<<endl;
cout<<"Number of Modes : "<<s.nModes<<endl;
cout<<"Max Frequency of mode : "<<s.maxFreq<<endl;
if(s.mode=='\0')
cout<<"There is no mode!"<<endl;
else
{
cout<<"Mode : "<<endl;
for(int i=0;i<s.nModes;i++)
cout<<"\t\t"<<s.mode[i]<<endl;
}
}
int main()
{
int n;
cout<<"Enter the size of array! : ";
cin>>n;
int *a;
a=new int[n];
cout<<"Enter its elements: \n";
for(int i=0;i<n;i++)
cin>>a[i];
Stats *s=avgMedMode(a,n);
printStat(*s);
delete [](s->mode);
delete [](s);
}
//output
I need this in C++ Write a function using the following structure and prototype. struct Stats...
C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...
Write a function with the following prototype struct node* copyList(struct node* 1ist) The struct node is the same used in question 18. The function should create a new, separate copy of the linked list pointed to by the list parameter and return a pointer to the head of the newly created linked list copy.
C++ Checkpoint 11.33 Look at the following structure definition: struct Date int month int day int year, Write the prototype for a function called getlnput that accepts a pointer to a Date structure as a parameter. Type your program submission here. Checkpoint 11.28 Look at the following structure definition struct Rectangle int length int width Write the definition of a pointer to a Rectangle structure called rectPtr and assign it the value nullptr Fype your progzam submission heze
Write a function that will initialize a variable of type struct case using the following prototype: void initCase(struct case * newCase, int bottles, char * soda);
Write a C++ program that has this FRACTION structure struct FRACTION{ int num; int den; }; That will keep track of numerator and denominator of a fraction. using pointers that is : getFraction() function will now return a pointer too a struct. multiply()function will now accept two struct via a pointer and return the result via a pointer. add()function will now accept two struct via a pointer and return the result via a pointer.
4. Write a C definition (prototype) for a function called power which takes a float and an integer as inputs, and returns a float as a result. 5. Write the body of the function from (4). This function should raise the first argument to the power of the second, by multiplying it by itself that many times (don’t use the inbuilt pow function, write your own). Do not worry about “special” cases like negative powers. Please do Q.5
A linked list of integers is built using the following struct: struct node { int data; struct node *next; }; Define a function named max that returns the maximum integer in a list. The function takes one arguments, a pointer to the head of the list. The function returns an integer, which is the maximum value. If the list is empty, return zero. NOTE: You know nothing about the values in the list. They could all be negative!
what is the answer of number 8?? 8. (3 pt) Write a prototype and a statement for calling the function with following given condi- tions.(please define properly all variables that you will use.) Function name is 'compute' which should return an integer pointer and have two parameters: a reference to a double, and a float array. uble compute int ouble&Ho im Ptr-new int i double di Plont forrato 1i doule compute( pennd, forrt) 1 a stattment for calling the function...
IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...
USING C++: Write a function that given a 2D dynamic array of integers, will return the number of elements whose absolute value is smaller than a given value x. The function should take as arguments (in this order): a pointer to the array (i.e., 2D dynamic array) the number of rows the number of columns the given value The function should return the number of elements smaller in absolute value than epsilon E.g. if A={{0.2, -0.1, 3.4},{4.4, 0, -2}} and...