Having trouble with writing the following 3 function definitions in C++. Any hel
ID: 3566995 • Letter: H
Question
Having trouble with writing the following 3 function definitions in C++. Any help with any of them would be very appreciated.
1) Write a function called Reverse that takes in two parameters: an integer array the size of the array. This function should reverse the order of the array's contents. No returned value.
2) Write a function called HowMany that takes in three parameters:
This function should count the number of occurences of the given value in the array (i.e. how many times this value appears), and then return the count.
3) Write a function called SumEvens that takes in three parameters:
This function should compute and return the sum of the first N even values in the array. (If there are fewer than N even numbers, then you'll just add them all (i.e. all of the even numbers)).
Thank you. I just need help with the definitions. If you can help with any of them I would appreciate it.
Explanation / Answer
#include<iostream>
void Reverse(int a[],int size);
int HowMany(int a[],int size,int num);
int SumEvens(int a[],int size,int N);
int main()
{
int a[100];
int n;
cout<<"Enter array size ";
cin>>n;
cout<<"Enter array elements ";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Reverse of array ";
Reverse(a,n);
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<"Enter element to find ";
int num;
cin>>num;
int count = HowMany(a,n,num);
cout<<"Number of occurences is :"<<count<<" ";
cout<<"Enter first N even value ";
int even;
cin>>even;
int sum = SumEvens(a,n,even);
cout<<"Sum of First N even numbers is :"<<sum<<" ";
}
void Reverse(int a[],int size)
{
int temp;
for(int i=0;i<size/2;i++)
{
temp = a[i];
a[i] = a[size-i-1];
a[size-i-1]=temp;
}
}
int HowMany(int a[],int size,int num)
{
int count=0;
for(int i=0;i<size;i++)
{
if(a[i]==num)
count++;
}
return count;
}
int SumEvens(int a[],int size,int N)
{
int sum=0,count=0;
for(int i=0;i<size;i++)
{
if(a[i]%2==0)
{
if(count<N)
{
sum = sum + a[i];
count++;
}
}
}
return sum;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.