Need to design a c++ program do the following. Problem is listed and is followed
ID: 3755541 • Letter: N
Question
Need to design a c++ program do the following. Problem is listed and is followed by intended output.
need to solve recurrence t(n)=t(n-1)+n, for n>1, t(1)=a; where a is some constant of type double Enter a and n
1. Find t(a, n) using recursion What is complexity of your solution, when you use recursion?
2. find t(a,n)==f(a,n) in O(1) complexity Provide code for both solutions. for n<1, t(a,n) is not defined, so throw exception if user enters not number, and throw exception if user enters number less than 1 catch both exceptions and ask user to re-enter n value
************************************************************************
intended output:
t(1)=a Enter constant double a = w
you entered not double type value
t(1)=a Enter constant double a = r
t(1)=a Enter constant double a = 0.5
Enter int n =0
you entered 0 number is less than 1
t(1)=a Enter constant double a = 4
Enter int n =-3
you entered -3 number is less than 1
t(1)=a Enter constant double a = 0.5
Enter int n =8
recursive t(0.5,8)=35.5
complexity O(1) f(0.5,8)=35.5
Do you want to repeate? y/n: y
t(1)=a Enter constant double a = r
t(1)=a Enter constant double a = 1
Enter int n =5
recursive t(1,5)=15
complexity O(1) f(1,5)=15
Do you want to repeat? y/n: n
Explanation / Answer
// C++ program to find the element
// occurring odd number of times
#include<bits/stdc++.h>
using namespace std;
// Funtion to find the element
// occurring odd number of times
int getOddOccurrence(int arr[], int arr_size)
{
for (int i = 0; i < arr_size; i++) {
int count = 0;
for (int j = 0; j < arr_size; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count % 2 != 0)
return arr[i];
}
return -1;
}
// driver code
int main()
{
int arr[] = { 2, 3, 5, 4, 5, 2,
4, 3, 5, 2, 4, 4, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function calling
cout << getOddOccurrence(arr, n);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.