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

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.
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)...
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...
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...
-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++
Show that (a)Sn=<(1 2),(1 3),……(1 n)>. (b)Sn=<(1 2),(2 3),……(n-1 n)> (c)Sn=<(1 2),(1 2 …… n-1 n)>
Show that (a)Sn=<(1 2),(1 3),……(1 n)>. (b)Sn=<(1 2),(2 3),……(n-1 n)> (c)Sn=<(1 2),(1 2 …… n-1 n)>
Write a program that uses a structure to store the following weather data for a particular...
Write a program that uses a structure to store the following weather data for a particular month: Total Rainfall High Temperature Low Temperature Average Temperature. The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month. (The average temperature should be calculated.) Once the data are entered for all the months, the program should calculate and display the average...
write a java program that implements the splay tree data structure for the dictionary abstract data...
write a java program that implements the splay tree data structure for the dictionary abstract data type. Initially the program reads data from "in.dat", and establishes an ordinary binary search tree by inserting the data into the tree. The data file contains integers, one per line. in.dat file contents: 3456 5678 1234 2369 7721 3354 1321 4946 3210 8765 Then the program starts an interactive mode. The commands are as follows. S 1000 - splay the tree at 1000 F...
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt...
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt the user for three numbers and displays them in ascending order. First, the program will prompt the user for each of the three numbers. Next, find the smallest value of the three. Then decide which of the other two is the next smallest. Then have the program display the three numbers in ascending order. Be sure to do the following: Determine what the input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT