Question

In: Computer Science

Data Structure: 1. Write a program for f(n) = 1^2+2^3+…+n^2. (i^2 = i*i) 2. If you...

Data Structure:

1. Write a program for f(n) = 1^2+2^3+…+n^2. (i^2 = i*i)

2. If you have the following polynomial function

f(n)=a0 +a1 x + a2x2+…+an xn ,

then you are asked to write a program for that, how do you do?

3. Write a function in C++ to sort array A[]. (You can assume that you have 10 elements in the array.)

4. Analyze the following program, tell us what does it do for each location of “???” ( i.e. add your comments for the code ) . boolean func(int Start, int End) ///??? What does the function do

{

int k = (Start + End)/2;

if (Start==End){ // ending state

if(N == A[k-1]) // ??? return true; // ???

else return false; // ??? }

if (N <= A[k-1]) return func(Start, k); // ???

else return func(k+1, End); // ???

return false; } // func

5. Write a program that implements Linked-Lists with function append() and removeHeadNode(). Can you use it for Queues? How?

Solutions

Expert Solution

Sol.

Part 1.

This fuction will compute F(n) where n is the user input value and it is also nth term in series.

#include <bits/stdc++.h>
using namespace std;

void series(int a, int r, int n)
{
int curr_term;
for (int i = 0; i < n; i++) {
curr_term = a * pow(r, i);
cout << curr_term << " ";
}
}
int main()
{
int a = 1; // First number of series
int r = 2; // Common ratio
int n; // N th term
cout<<"Enter Nth term that you want: "<<endl;
cin>>n;
series(a, r, n);
return 0;
}

Part 2.

GIven series is the binomial expension. so to compute the binomial expenesion

nCr = (n!) / ((n-r)! * (r)!) and general term is 
Tr+1 = nCn-rAn-rXr

and binomial coffient is nCi+1 = nCi*(n-i)/(i+1)

suppose a = 1 , x = 1 and n = 5

then serires will be 1 5 10 10 5 1

so lets make program for this.

#include <bits/stdc++.h>
using namespace std;

void bio(int A, int X, int n)
{
int nthterm = pow(A, n);
cout << nthterm << " ";
for (int i = 1; i <= n; i++) {
nthterm = nthterm * X * (n - i + 1)/(i * A);
cout << nthterm << " ";
}
}

int main()
{
int a; // Value of constant a
cout<<"Enter value of a: "<<endl;
cin>>a;
int x; // value of x
cout<<"Enter value of x: "<<endl;
cin>>x;
int n = 5; // Power
cout<<"Enter value of n"<<endl;
cin>>n;
bio(a,x,n);
return 0;
}

Part 3.

There are many various algorithm exist to sort the array. here is bubble sort implemetation.

#include<bits/stdc++.h>
using namespace std;

void Sort(int* ar, int n)
{
int i,j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (ar[j] > ar[j+1])
swap(ar[j], ar[j+1]);
}
int main()
{
int ar[10] = {0,3,2,5,3,7,4,8,6,8};
int n = sizeof(ar)/sizeof(ar[0]);
Sort(ar, n);
for(int i=0;i<10;i++)
cout<<ar[i]<<" ";
return 0;
}
part 4.

Here is boolean func is the implemetation of binary search to find the target element in the array.

boolean func(int Start, int End){

int k = (Start + End)/2; // finding the middle element of array.

if (Start==End){ // if start reaches at the end of array then enetr to the condition

if(N == A[k-1]) return true;// here N is the target variable and we are comparing it with middle's previous element if match we will return 1 or true to the boolean func
else return false; // if does not match then return false or 0.

if (N <= A[k-1]) return func(Start, k); // if target element N is less then midlle's previous element then we got that target will find in between 0 to k-1 so we call start to k in fuction

else return func(k+1, End); // if target is greater then call k+1 to end in array

return false; } // func
}

part 5 implentation of append and removalheadnode.

class Node

{

    public:

    int data;

    Node *next;

};

void append(Node* head, int new_data)
{
Node* new_node = new Node();
Node *last = *head;
new_node->data = new_data;
new_node->next = NULL;

if (head == NULL)
{
head = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return;
}

part 5. delete head node in list

Node* removeHeadNode(struct Node* head)
{
if (head == NULL)
return NULL;
Node* temp = head;
head = head->next;
  
delete temp;
  
return head;
}


Related Solutions

a) write a program to compute and print n, n(f), f(f(n)), f(f(f(n))).........for 1<=n<=100, where f(n)=n/2 if...
a) write a program to compute and print n, n(f), f(f(n)), f(f(f(n))).........for 1<=n<=100, where f(n)=n/2 if n is even and f(n)=3n+1 if n is odd b) make a conjecture based on part a. use java
2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2)...
2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2) with f(0) = 0 and f(1) = 1. Find f(54) by a program or maually. Note that this number must be positive and f(53) = 53.......73 (starting with 53 and ending with 73). I must admit that my three machines including a desktop are unable to find f(54) and they quit during computation. The answer is f(54) = 86267571272 */ The Java code: public...
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n −...
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n − 2) for n ≥ 2 (a) Use strong induction to show that F(n) ≤ 2^n for all n ≥ 0. (b) The answer for (a) shows that F(n) is O(2^n). If we could also show that F(n) is Ω(2^n), that would mean that F(n) is Θ(2^n), and our order of growth would be F(n). This doesn’t turn out to be the case because F(n)...
Define the following function f(n) = 5(2^n)-(2^(n-1)), n ≥ 1. Write a recursive definition for the...
Define the following function f(n) = 5(2^n)-(2^(n-1)), n ≥ 1. Write a recursive definition for the function f(n)? Consider the following recurrence: an= 2an-1 + 3 (where a1 = 1). Compute the values of an for n ≤ 5. Find a solution for the recurrence definition and validate its correctness. Consider the following recurrence: an=2an-1 +an-1an-2 (where a1 = 1). Compute the values of an for n ≤ 5.
Write a combinatorial proof for 1 n + 2 ( n − 1 ) + 3...
Write a combinatorial proof for 1 n + 2 ( n − 1 ) + 3 ( n − 2 ) + ⋯ + ( n − 1 ) 2 + n 1 = ( n + 2 choose 3 ) .
Suppose f : N→N satisestherecurrencerelation f(n + 1) (f(n) 2 if f(n)iseven 3f(n)+ 1 if f(n)isodd...
Suppose f : N→N satisestherecurrencerelation f(n + 1) (f(n) 2 if f(n)iseven 3f(n)+ 1 if f(n)isodd . Notethatwiththeinitialcondition f(0) 1,thevaluesofthefunction are: f(1) 4, f(2) 2, f(3) 1, f(4) 4, and so on, the images cyclingthroughthosethreenumbers. Thus f isNOTinjective(andalso certainlynotsurjective). Mightitbeunderotherinitialconditions?3 (a) If f satisestheinitialcondition f(0) 5,is f injective? Explain whyorgiveaspecicexampleoftwoelementsfromthedomain withthesameimage. (b) If f satisestheinitialcondition f(0) 3,is f injective? Explain whyorgiveaspecicexampleoftwoelementsfromthedomain withthesameimage. (c) If f satisestheinitialcondition f(0) 27,thenitturnsoutthat f(105) 10 and no two numbers less than 105 have the same...
3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a...
3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a function to quickly compute this sequence. >>> [hc(i) for i in range(1,20)] [1, 1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 7, 8, 8, 8, 8, 9, 10, 11]
c++ program You are to use a Heap data structure for this assignment I currently work...
c++ program You are to use a Heap data structure for this assignment I currently work for an investment/insurance company and I’m looking for clients to call, ones with funds.  I need to have a schedule that shows the list of customers to call and the order to be called.  The list of customers names and phone numbers are in the file ‘NamesAndPhoneV2.txt’.  A second file contains a net worth value for each client.  The files are separated for security and protection reasons, but...
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3!...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3! /3+4! /4+5! /5 using the function1, • to convert decimal number to binary number using the function2, • to check whether a number is a prime number or not using the function3, • to check whether two given strings are an anagram using the function4. important must do in (Multi-Filing) of c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT