A6p1.cpp
#include<bits/stdc++.h>
#include <sys/time.h>
#include <pthread.h>
using namespace std;
const int N=60;
char ar[N];
int n_threads; //stores no. of threads
int curr_thread=0; //stores index(0 to n_thread-1) of thread
// intitialize function intializes ar[] with random character
values between 'A'-'Z'
void initialize(){
for(int i=0;i<N;i++){
ar[i]='A'+rand()%26; // rand()%26 will generate random integer
between 0-25, ('A'+generated integer) will create character between
A-Z
}
}
// print function prints the string in ar[]
void print(){
for(int i=0;i<60;++i){
cout<<ar[i];
}
}
// Each thread computes 1/p_thread th part of array
void* fun(void* arg){
// the part of array to be computed can be determined by the
curr_thread(thread index)
int
s=curr_thread*(N/n_threads),e=(curr_thread+1)*(N/n_threads);
for(int i=s;i<e;++i){
int x=ar[i]-'A';
x=25-x;
ar[i]=x+'a';
}
curr_thread++;
}
int main(int argc,char *argv[]){
n_threads=argv[1][0]-'0'; // get the no. of threads to be used from
command line argument
cout<<"using "<<n_threads<<" threads\n\n";
initialize(); // initialize the character array
// print the original string
cout<<"original upper case string:\n";
print();
cout<<"\n\n";
pthread_t threads[n_threads];
// Creating threads
for(int i=0;i<n_threads;++i){
pthread_create(&threads[i],NULL,fun,(void *)NULL);
}
// joining threads i.e. waiting for all threads to
complete
for(int i=0;i<n_threads;++i){
pthread_join(threads[i],NULL);
}
// print the final string
cout<<"complementary lower case string:\n";
print();
cout<<"\n\n";
}
A6p2.cpp
#include<bits/stdc++.h>
#include <sys/time.h>
#include <pthread.h>
using namespace std;
const int N=60; //no. of elements in array
const int r=5; //no. of rows
const int c=12; //no. of columns
int ar[r][c];
int n_threads; //stores no. of threads
int curr_thread=0; //stores index(0 to n_thread-1) of thread
// intitialize function intializes ar[] with random int values
between 1-49
void initialize(){
srand(time(0));
for(int i=0;i<r;i++){
for(int j=0;j<c;++j){
ar[i][j]=(rand()%49)+1; // rand()%26 will generate random integer
between 0-48, (1+generated integer) will create integer between
1-49
}
}
}
// print function prints the elements ofar[]
void print(){
for(int i=0;i<r;i++){
for(int j=0;j<c;++j){
cout<<ar[i][j]<<' ';
}
cout<<'\n';
}
}
// Each thread computes 1/p_thread th part of array
void* fun(void* arg){
// the part of array to be computed can be determined by the
curr_thread(thread index)
int
s=curr_thread*(N/n_threads),e=(curr_thread+1)*(N/n_threads);
for(int i=s;i<e;++i){
int r_i=i/c,c_i=i%c;
int x=ar[r_i][c_i];
ar[r_i][c_i]=x*x;
}
curr_thread++;
}
int main(int argc,char *argv[]){
n_threads=argv[1][0]-'0'; // get the no. of threads to be used from
command line argument
cout<<"using "<<n_threads<<" threads\n\n";
initialize(); // initialize the array
// print the original array
cout<<"original array:\n";
print();
cout<<"\n\n";
pthread_t threads[n_threads];
// Creating threads
for(int i=0;i<n_threads;++i){
pthread_create(&threads[i],NULL,fun,(void *)NULL);
}
// joining threads i.e. waiting for all threads to
complete
for(int i=0;i<n_threads;++i){
pthread_join(threads[i],NULL);
}
// print the final array
cout<<"updated array:\n";
print();
cout<<"\n\n";
}
make file(name of make file should be "Makefile")
all: A6p1 A6p2
A6p1: A6p1.cpp
g++ A6p1.cpp -l pthread -o A6p1
A6p2: A6p2.cpp
g++ A6p2.cpp -l pthread -o A6p27
1) Explain the IDFT function in Matlab 2) Re-write a SCILAB script that performs the IDFT....
5. Create a MATLAB script to find the first and second derivative of given function using Forward, Backward, central and Taylor numerical schemes. Test your code using the following functions: f(x)-xe*+3x2 +2x -1 and find f (3) and f' (3) for with h 0.1, 0.01 and 0.001 b. Approximate y'(1) and y"(1) using the following table f(x) 0.992 0.8 0.9 0.999 1.0 1.001 1.008 Input: (copy and paste the MATLAB or Scilab script in the following box) 5. Create a...
specify Output, please. 6. Create a MATLAB script to integrate the given function using the numerical scheme called Trapezoid's Rule. a. f(x)- xe* +3x1+2x-1 and find for with h 0.1, 0.01 and 0.001 Input: (copy and paste the MATLAB or Scilab command in the following box)
Use MATLAB OR SCILAB TO PERFORM THE FOLLOWING MATRIX OPERATION Use MATLAB or Scilab to perform the following matrix operation 1 7 -2 3 10 91 5 -1 1) 2 51 13 7 -54 21 31 4 -15 2 9 6 18 -4 -2 31 Input: (copy and paste the MATLAB or Scilab command in the following box) Output: (copy and paste the output in the following box)
Write a MATLAB script to plot the function of f(x) given by: Write a MATLAB script to plot the function of f(x) given by: f(x) = integral x^2 - pi^2/4 x > pi/2 8 * cos x -pi/2 lessthanorequalto x lessthanorequalto pi/2 pi^2/4 - x^2 x < -pi/2
Write a MATLAB function/script that performs the following tasks. Approximate: 2+2 (a) Using the composite Trapezoidal rule with n=8 (b) Using the composite Simpson's rule with n = 8 (c) Display the final solution for each method along with the exact solution. Name your file: WS5_LastName_First Inital()
Using MATLAB. Create a script file that calls a user-defined function within a for/end loop to symbolically compute the derivative of ln(x) on the interval from -5 to 5, containing 100 data points. Your script file should output a plot of the derivative of ln(x) (taken from your function) vs x. Use a line with circular markers for the plot and include a grid and appropriate labels. The user-defined function should 1. Receive a single x value as input. 2....
DO THIS IN MATLAB PLEASE DO THIS IN MATLAB Create a script file that performs the following calculations. Your script will use the functions you create. Label all graphs appropriately. For this project, do not have your homemade functions fprintf anything, instead have all your fprintf commands within your script. Attach your published script file along with .m files for your functions. Exercise 1. A fundamental iterative method for finding the roots of equations of one-variable is known as Newton's...
must be done in MATLAB 2. Create a main program that calls the subroutine created on problem 1 and compare results using the following data sets: b. (1 5), (0, 8), (3, -10) С. (-10,-2). ( 4,5), (7, 3), (12, 20) Output: (copy and paste the output in the following box) Use MATLAB or Scilab to solve the following problems 1. Create a MATLAB subroutine called Lagrange.m that receives two set data points, x and y and plots the curve...
Write a matlab script(using only matlab) for this. Write a script to solve the following problem: Ask the user for the length and the width of a rectangle. These need to be passed to a function. The function is to calculate and return the area and the perimeter of the rectangle. The area is the length times the width and the perimeter is 2 times the length and 2 times the width. Make sure to suppress all output from the...
Write a Matlab Function (not a script) that performs a single iteration of the Euler's method for the regular parashot problem, (i.e. just write a function that calculates v_next) Assume: cd = 0.25 [kg/m] for t < 10sec and cd = 1.5 [kg/m] for t ≥ 10sec Hint: use only if-statements, do not use any LOOPs you only need to compute only one iteration Hint2: make sure you define the inputs and outputs of the function correctly Please show how...