Use Lagrange interpolation to find the polynomial p3(x) of degree 3 or less, that agree with the following data: p3(−1) = 3, p3(0) = −4, p3(1) = 5, and p3(2) = −6.
Using python to solve
In: Computer Science
Write a parallel program using Pthread based on given sequential solution. Please set the thread number as 10 in your code.
Given Sequential Solution:
#include <stdlib.h> #include <stdio.h> #include <string.h> #define MAX 10240 int total = 0; int n1,n2; char *s1,*s2; FILE *fp; int readf(FILE *fp) { if((fp=fopen("strings.txt", "r"))==NULL){ printf("ERROR: can't open string.txt!\n"); return 0; } s1=(char *)malloc(sizeof(char)*MAX); if(s1==NULL){ printf("ERROR: Out of memory!\n"); return -1; } s2=(char *)malloc(sizeof(char)*MAX); if(s2==NULL){ printf("ERROR: Out of memory\n"); return -1; } /*read s1 s2 from the file*/ s1=fgets(s1, MAX, fp); s2=fgets(s2, MAX, fp); n1=strlen(s1); /*length of s1*/ n2=strlen(s2)-1; /*length of s2*/ if(s1==NULL || s2==NULL || n1<n2) /*when error exit*/ return -1; return 0; } int num_substring(void) { int i,j,k; int count; for (i = 0; i <= (n1-n2); i++){ count=0; for(j = i,k = 0; k < n2; j++,k++){ /*search for the next string of size of n2*/ if (*(s1+j)!=*(s2+k)){ break; }else{ count++; } if(count==n2){ total++; /*find a substring in this step*/ } } } return total; } int main(int argc, char *argv[]) { int count; readf(fp); count = num_substring(); printf("The number of substrings is: %d\n", count); return 1; } strings.txt file:
Thss is an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. This is an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss ss. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That is a kiwi fruit. This is an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. This is a banana. This is a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ssss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. This is a banana. This is a berry. That is cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. This is a banana. This is a berry. This is cherry. That ss a haw. Thss ss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ssss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. This is a banana. This is a berry. This is cherry. That is a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss ss an avocado. There ss a peach on the tree. This is a banana. This is a berry. This is cherry. This is a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree This is a banana. This is a berry. This is cherry. This is a haw. Thss is a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. This is a banana. This is a berry. This is cherry. This is a haw. This is a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the is
Given Template for program using 10 threads:
#include <pthread.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #define MAX 10240 #define NUM_THREADS 10 int n1,n2; char *s1,*s2; FILE *fp; int countArray[NUM_THREADS]={0}; //read input file and generate string s1/s2 and length n1/n2 int readf(FILE *fp) { if((fp=fopen("strings.txt", "r"))==NULL){ printf("ERROR: can't open string.txt!\n"); return 0; } s1=(char *)malloc(sizeof(char)*MAX); if(s1==NULL){ printf("ERROR: Out of memory!\n"); return -1; } s2=(char *)malloc(sizeof(char)*MAX); if(s1==NULL){ printf("ERROR: Out of memory\n"); return -1; } /*read s1 s2 from the file*/ s1=fgets(s1, MAX, fp); s2=fgets(s2, MAX, fp); n1=strlen(s1); /*length of s1*/ n2=strlen(s2)-1; /*length of s2*/ if(s1==NULL || s2==NULL || n1<n2) /*when error exit*/ return -1; return 0; } int num_substring(int t) { //add your logic here //1, how to distribute different parts of string s1 into different threads //2, how to sum up the total number of substring from all threads return 0; } void *calSubStringThread(void *threadid){ long tid = (long)threadid; printf("This is thread %ld, ", tid); int num = num_substring(tid); printf("find num of is: %d\n", num); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int t, rc; int totalNum = 0; readf(fp); for(t=0; t<NUM_THREADS; t++){ rc = pthread_create(&threads[t], NULL, calSubStringThread, (void *) (size_t)t); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } for(t=0; t<NUM_THREADS; t++){ pthread_join(threads[t], NULL); } printf("The number of substrings is: %d\n", totalNum); return 1; }
In: Computer Science
Please give me your knowledge about;
Which requirements (groups) in the requirements in the SRS would benefit from formalization?
In: Computer Science
def num_to_digit_rec(num, base): """ Return a list of digits for num with given base; Return an empty list [] if base < 2 or num <= 0 """ # Write your code here return [] def digit_sum(num, base): """ Return the sum of all digits for a num with given base Your implementation should use num_to_digit_rec() function """ # Write your code here return 0 def digit_str(num, base): """ Given a number and a base, for base between [2, 36] inclusive Converts the number to its string representation using digits 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ to represent digits 0 to 35. Return the string representation of the number Return an empty string '' if base is not between [2, 36] Your implementation should use num_to_digit_rec() function """ digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" # Can not find str representations for base not in [2, 36] if base > 36 or base < 2: return "" # Calculate and return the str representation for num for the given base # Write your code here return "" def uses_only(word, letters): """ Return True if word only uses characters from letters; otherwise return False """ # Write your code here return True def digit_to_num(rep, base): """ Return -1 if base is not between [2,36] inclusive; or if the string rep contains characters not a digit for the base; Return the number represented by the string for the given base otherwise. For example digit_to_num("1001", 2) is 9; digit_to_num("ABC", 16) is 2748. """ # Check if the base is valid if base > 36 or base < 2: return -1 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" rep = rep.upper() # Check if the rep only uses proper digits # Write your code here return 0 def run(): num = int(input("Enter a num: ")) base = int(input("Enter a base: ")) print(f"Digit list is {num_to_digit_rec(num, base)}") print(f"Digit sum is {digit_sum(num, base)}") print(f"String Rep is {digit_str(num, base)}") rep = input(f"Enter a string rep of a num with base {base}: ") print(f"The number is {digit_to_num(rep, base)}") if __name__ == "__main__": run()
Example:
Enter a num: 0 Enter a base: 5 Digit list is [] Digit sum is 0 String Rep is 0 Enter a string rep of a num with base 5: 3 The number is 3
In: Computer Science
Write a program that prompts for and reads in the two side lengths of a right triangle (floating point numbers) and then calls a float-valued function, hypot1, that calculates and returns the length of the hypotenuse of the triangle. The program then displays the two side lengths and the hypotenuse length. Note: The hypot1 function returns the hypotenuse length – it does not display it; the function should not contain any cout nor cin statements. Math review: Recall that for any right triangle with side lengths a and b, and hypotenuse length c, a2+b2=c2. Here is an example of what output should look like from running your program (user input is shown in bold).
Enter the side lengths: 1.2 3.4
First side length = 1.2
Second side length = 3.4
Hypotenuse length = 3.60555
In: Computer Science
In: Computer Science
How to reverse linked list below,thank you!
#include <stdlib.h>
#include <stdio.h>
struct list
{
int data;
struct list *next;
};
typedef struct list node;
typedef node *link;
int main()
{
link ptr,head;
int num,i;
head = ( link ) malloc(sizeof(node));
ptr = head;
printf("enter 10 data \n");
for ( i = 0; i <= 9; i++ )
{
scanf("%d",&num);
ptr->data = num;
ptr->next = ( link ) malloc(sizeof(node));
if ( i == 9 )
ptr->next = NULL;
else
ptr = ptr->next;
}
printf("original linked list\n");
ptr = head;
while ( ptr != NULL )
{
printf("data==> %d\n",ptr->data);
ptr = ptr->next;
}
system("pause");
return 0;
}
In: Computer Science
In this assignment we are not allowed to make changes to Graph.h it cannot be changed! I need help with the Graph.cpp and the Driver.cpp.
Your assignment is to implement a sparse adjacency matrix data structure Graph that is defined in the header file Graph.h. The Graph class provides two iterators. One iterator produces the neighbors for a given vertex. The second iterator produces each edge of the graph once.
Additionally, you must implement a test program that fully exercises your implementation of the Graph member functions. Place this program in the main() function in a file named Driver.cpp.
The purpose of an iterator is to provide programmers a uniform way to iterate through all items of a data structure using a forloop. For example, using the Graph class, we can iterate thru the neighbors of vertex 4 using:
Graph::NbIterator nit ; for (nit = G.nbBegin(4); nit != G.nbEnd(4) ; nit++) { cout << *nit << " " ; } cout << endl ;
The idea is that nit (for neighbor iterator) starts at the beginning of the data for vertex 4 in nz and is advanced to the next neighbor by the ++ operator. The for loop continues as long as we have not reached the end of the data for vertex 4. We check this by comparing against a special iterator for the end, nbEnd(4). This requires the NbIterator class to implement the ++, !=and * (dereference) operators.
Similarly, the Graph class allows us to iterate through all edges of a graph using a for loop like:
Graph::EgIterator eit ; tuple<int,int,int> edge ; for (eit = G.egBegin() ; eit != G.egEnd() ; eit++) { edge = *eit ; // get current edge cout << "(" << get<0>(edge) << ", " << get<1>(edge) << ", " << get<2>(edge) << ") " ; } cout << endl ;
Note that each edge should be printed only once, even though it is represented twice in the sparse adjacency matrix data structure.
Since a program may use many data structures and each data structure might provide one or more iterators, it is common to make the iterator class for a data structure an inner class. Thus, in the code fragments above, nit and eit are declared asGraph::NbIterator and Graph::EgIterator objects, not just NbIterator and EgIterator objects.
Here are the specifics of the assignment, including a description for what each member function must accomplish.
Requirement: your implementation must dynamically resize the m_nz and m_ci arrays. See the descriptions of Graph(constructor) and addEdge, below.
Requirement: other than the templated tuple class, you must not use any classes from the Standard Template Library or other sources, including vector and list. All of the data structure must be implemented by your own code.
Requirement: your code must compile with the original Graph.h header file. You are not allowed to make any changes to this file. Yes, this prevents you from having useful helper functions. This is a deliberate limitation of this project. You may have to duplicate some code.
Requirement: a program fragment with a for loop that uses your NbIterator must have worst case running time that is proportional to the number of neighbors of the given vertex.
Requirement: a program fragment with a for loop that uses your EgIterator must have worst case running time that is proportional to the number of vertices in the graph plus the number of edges in the graph.
Graph.h:
#ifndef _GRAPH_H_ #define _GRAPH_H_ #include <stdexcept> // for throwing out_of_range exceptions #include <tuple> // for tuple template class Graph { public: // Graph constructor; must give number of vertices Graph(int n); // Graph copy constructor Graph(const Graph& G); // Graph destructor ~Graph(); // Graph assignment operator const Graph& operator= (const Graph& rhs); // return number of vertices int numVert(); // return number of edges int numEdge(); // add edge between u and v with weight x void addEdge(int u, int v, int x); // print out data structure for debugging void dump(); // Edge Iterator inner class class EgIterator { public: // Edge Iterator constructor; indx can be used to // set m_indx for begin and end iterators. EgIterator(Graph *Gptr = nullptr, int indx = 0); // Compare iterators; only makes sense to compare with // end iterator bool operator!= (const EgIterator& rhs); // Move iterator to next printable edge void operator++(int dummy); // post increment // return edge at iterator location std::tuple<int,int,int> operator*(); private: Graph *m_Gptr; // pointer to associated Graph int m_indx; // index of current edge in m_nz int m_row; // corresponding row of m_nz[m_indx] }; // Make an initial edge Iterator EgIterator egBegin(); // Make an end iterator for edge iterator EgIterator egEnd(); // Neighbor Iterator inner class class NbIterator { public: // Constructor for iterator for vertices adjacent to vertex v; // indx can be used to set m_indx for begin and end iterators NbIterator(Graph *Gptr = nullptr, int v = 0, int indx = 0); // Compare iterators; only makes sense to compare with // end iterator bool operator!=(const NbIterator& rhs); // Move iterator to next neighbor void operator++(int dummy); // Return neighbor at current iterator position int operator*(); private: Graph *m_Gptr; // pointer to the associated Graph int m_row; // row (source) for which to find neighbors int m_indx; // current index into m_nz of Graph }; // Make an initial neighbor iterator NbIterator nbBegin(int v); // Make an end neighbor iterator NbIterator nbEnd(int v); private: int *m_nz; // non-zero elements array int *m_re; // row extent array int *m_ci; // column index array int m_cap; // capacity of m_nz and m_ci int m_numVert; // number of vertices int m_numEdge; // number of edges }; #endif
Graph.cpp outline:
#include "Graph.h"
Graph::Graph(int n) {
}
Graph::Graph(const Graph& G) {
}
Graph::~Graph() {
}
const Graph& Graph::operator= (const Graph& rhs) {
}
// This Function will return the number of vertices in the
graph
int Graph::numVert() {
}
int Graph::numEdge() {
}
void Graph::addEdge(int u, int v, int x) {
}
void Graph::dump() {
}
Graph::EgIterator::EgIterator(Graph *Gptr = nullptr, int index =
0) {
}
bool Graph::EgIterator::operator!= (const EgIterator& rhs)
{
}
void Graph::EgIterator::operator ++(int dummy) {
}
std::tuple<int, int, int> Graph::EgIterator::operator *()
{
}
Graph::EgIterator Graph::egBegin() {
}
Graph::EgIterator Graph::egEnd() {
}
Graph::NbIterator::NbIterator(Graph *Gptr = nullptr, int v = 0,
int indx = 0) {
}
bool Graph::NbIterator::operator !=(const NbIterator& rhs)
{
}
void Graph::NbIterator::operator ++(int dummy) {
}
int Graph::NbIterator::operator *() {
}
Graph::NbIterator Graph::nbBegin(int v) {
}
Graph::NbIterator Graph::nbEnd(int v) {
}
In: Computer Science
5. Virtualization and cloud computing are rapidly expanding into the enterprise as viable solutions. Discuss the connections between these two technologies and the security implications of a virtual network infrastructure in the cloud vs. a physical network infrastructure onsite.
In: Computer Science
C++ Code Writing prompt:
Grade Calculation: Write a program that asks the user to enter in a number greater than or equal to zero and less than or equal to 100. If they do not you should alert them and end the program. Next, determine the letter grade associated with the number. For example, A is any grade between 90 and 100. Report the letter grade to the user.
In: Computer Science
a) TCP/IP what is the name of the packets at the data link layer?
b) What are the two identifiers to locate a process in other part of the world so we can connect to it?
c) Supposed you are making a broadcast in your local network and the broadcasts reach a router . Would the router forward this broadcast message further or stop it? Why or why not?
d) Tcp/IP is the defacto network architecture for today's internet/ What does this mean?
In: Computer Science
A computer manufacturer offers the possibility of purchasing computers via the Internet. The customer can select a computer on the manufacture’s web page. The computers are classified into servers, desktops and portables. The customer can select a standard configuration or can build a desired configuration online. The configurable components are presented as pick lists of available options. For each new configuration, the system can calculate price. To place an order, the customer must fill out the shipment and payment information. Acceptable payment methods are credit cards and checks. Once the order has been entered, the system sends a confirmation e-mail message to the customer with details of the order. While waiting for the arrival of the computer, the customer can check the order status online at any time. The back end order processing consists of the steps needed to verify the customer’s credentials and payment method, to request the ordered configuration from the warehouse, to print an invoice, and to request the warehouse to ship the computer to the customer. Consider the following extended requirements: The customer uses the manufacturer’s online shopping web page to view the standard configuration of the chosen server, desktop or portable computer. The customer chooses to view the details of the configuration, perhaps with the intention of buying it as it or to build a more suitable configuration. The price for each configuration can be computed at the customer’s request. The customer may choose to order the computer online or may request that the salesperson contact him/her to explain order details, negotiate the price, etc. before the order is actually place. To place the order, the customer must fill out an online form with shipment and invoice address, and with payment details (credit card or cheque). After the customer’s order has been entered into the system, the salesperson sends and electronic request to the warehouse with details of the ordered configuration. The details of the transaction, including an order number and customer account number, are e-mailed to the customer, so that the customer can check the status of the order online. The warehouse obtains the invoice from the salesperson and ships the computer to the customer.
For the above Scenario draw the UML diagrams
Q1: Draw Activity diagram make possible assumptions and also draw Sequence diagrams of the major 3 functions?
In: Computer Science
Consider the following table showing multiple transactions. Find all frequent itemsets using Apriori, then list all the strong association rules knowing that min_sup count = 2, and min_conf = 60%.
TID |
Items |
T1 T2 T3 T4 T5 T6 |
A, B, D, E A, B, C C, E B, C A A, B, C |
Please write, not a screenshot
In: Computer Science
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel.
You must insert the following comments at the beginning of your program and write our commands in the middle:
Write a C++ Program:
/*
// Name: Your Name
// ID: Your ID
// Purpose Statement:~~~
*/
#include <iostream>
#include <string>
using namespace std;
void removeVowels(string& str);
bool isVowel(char ch);
int main()
{
string str;
cout << "Enter a string: ";
…
…
YOUR CODE HERE
…
…
return 0;
}
void removeVowels(string& str)
{
int len = str.length();
…
…
YOUR CODE HERE
…
…
}
bool isVowel(char ch)
{
switch (ch)
…
…
YOUR CODE HERE
…
…
}
In: Computer Science
rite a method with the following header:
public static void showGradeDistribution(int a, int b, int c, int d,
int f)
It should print a graph (using asterisks) for each of the letters entered in the reverse order of the
parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C
and D and F, the message “Strong class!” should be displayed. For example a method call of:
showGradeDistribution(5,7,4,4,3);
Would print:
A: *****
B: *******
C: ****
D: ****
F: ***
Strong clas
In: Computer Science