I need Help PLZ. I can't solving this in C
program
Given is a C program count_primes.c, which is an array Generates
10000 six-digit random numbers and then determines the number of
primes in that array. It also measures the time that elapses during
the calculation and returns it to the console along with the number
of primes it finds.
(a) Rewrite the program so that N threads are used to count the primes in the array. Each thread is supposed to check a separate part of the array (eg in case of two threads half each).
The number of threads should be passed as a program
argument and not be firmly coded! Here is the maximum possible
value for
N up10 be limited.
Note: Use POSIX threads. Include the header
<pthread.h>. Also, when calling gcc, you must specify
-pthread as an additional option. B .: gcc -o
count_primescount_primes.c-g-pthread
(b) Test your program for N = 1,2,4 and 8 threads and graph the
results.
Explain briefly what value the runtime is for very large N is
expected to converge.
In addition, briefly describe a negative effect that you can get
with very large ones N gets.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/time.h> // Include for use time functions
#include <pthread.h> // Include for POSIX threads
/** Return milliseconds elapsed since given time.
*/
int getElapsedTime(struct timeval startTime)
{
struct timeval stopTime, elapsedTime;
gettimeofday(&stopTime, NULL);
timersub(&stopTime, &startTime, &elapsedTime);
return 1000*elapsedTime.tv_sec + elapsedTime.tv_usec/1000;
}
/** Return true if given number z is a prime number.
*/
bool isPrime(int z)
{
int t = 2;
int tmax = z / 2;
while (t <= tmax && z % t != 0) {
t++;
}
return t > tmax;
}
/**
* Main program: Check n integer values for primes.
*/
int main(int argc, char* argv[])
{
// Create n random integers in array
int numValues = 10000;
int values[numValues];
for (int i = 0; i < numValues; i++) {
values[i] = rand() % 900000 + 100000;
}
// Get current time for time measurement
struct timeval startTime;
gettimeofday(&startTime, NULL);
// Check array for primes in main thread
// TODO: Distribute this to m threads!
int numPrimes = 0;
for (int i = 0; i < numValues; i++) {
if (isPrime(values[i])) {
numPrimes++;
}
}
// Measure computation time
int msec = getElapsedTime(startTime);
// Display results
printf("Found %d primes in %d values in %d ms\n",
numPrimes, numValues,
msec);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/time.h> // Include for use time functions
#include <pthread.h> // Include for POSIX threads
struct arguments
{
int * array;
int num;
int noPrimes;
};
/** Return milliseconds elapsed since given time. */
int getElapsedTime(struct timeval startTime)
{
struct timeval stopTime, elapsedTime;
gettimeofday(&stopTime, NULL);
timersub(&stopTime, &startTime, &elapsedTime);
return 1000*elapsedTime.tv_sec + elapsedTime.tv_usec/1000;
}
/** Return true if given number z is a prime number. */
bool isPrime(int z)
{
int t = 2;
int tmax = z / 2;
while (t <= tmax && z % t != 0) {
t++;
}
return t > tmax;
}
/** function to find number of primes in given array
*/
void *numOfPrimes(void * args)
{
struct arguments * arg = (struct arguments *)
args;
int *values=arg->array;
int numValues=arg->num;
int numPrimes = 0;
for (int i = 0; i < numValues; i++) {
if (isPrime(values[i])) {
numPrimes++;
arg->noPrimes=numPrimes;
}
}
}
/**
* Main program: Check n integer values for primes.
*/
int main(int argc, char* argv[])
{
// Create n random integers in array
int numValues = 10000;
int values[numValues];
for (int i = 0; i < numValues; i++) {
values[i] = rand() % 900000 + 100000;
}
// Get current time for time measurement
struct timeval startTime;
gettimeofday(&startTime, NULL);
int N;
printf("enter number of threds you need to add\n");
scanf("%d",&N);
pthread_t thread[N]; //thread array to store N number of
threads
struct arguments *args[N];
for(int i=0;i<N;i++){
args[i]=(struct arguments *)malloc (sizeof (struct
arguments));
args[i]->array=&values[0]+i*N; //divide array
into N parts
args[i]->num=numValues/N;
args[i]->noPrimes=0; //to store number of primes
from each thread
pthread_create(& thread[i], NULL,
numOfPrimes,args[i]);
}
for(int i=0;i<N;i++)
{
pthread_join(thread[i],NULL); //wait till all the
threds finish their job before main going to terminate them
}
// Measure computation time
int msec = getElapsedTime(startTime);
int numPrimes=0;
for(int i=0;i<N;i++)
{
numPrimes+=args[i]->noPrimes;
}
// Display results
printf("Found %d primes in %d values in %d ms\n",
numPrimes, numValues, msec);
return 0;
}
I need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that...
1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...
Help with a Parallel and Distributed Programming assignment. In this assignment, you will be exploring different methods of counting the prime numbers between 1 and N. You will use 8 threads, and each will be given a range in which to count primes. The question is: where do you store your counter? Do you use a local variable? Do you use a global variable? Please use the following function to determine whether an integer number is a prime. // Return...
Need help. write a C program stack-ptr.c that implements a stack using a link list. Below is a skeleton code to start with.Jjust edit to make thread friendly. examplpe: push(5, &top); push(10, &top); push(15, &top); int value = pop(&top); value = pop(&top); value = pop(&top); this program currently has a race condition. use Pthread mutex locks to fix the race conditions. test you now thread safe stack by creating 200 concurrent threads in main() that push and pop values. -use...
Please change this 2 thread program to a 4 thread program. /*compile as "gcc thread.c -pthread" */ #include <pthread.h> #include <stdio.h> #include <time.h> #define SIZE 100000000 //100 M //#define SIZE 10000 #define NUMBER_OF_TIMES 100 float a[SIZE]; typedef struct { int start; int end; double sum; int pid; } range; void *thread_function(void *arg) //define a function that will be executed from //within a thread { range *incoming = (range *) arg;...
My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...
Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random numbers between 0 and 100. The array is sent to a function that finds the indices of all items > 50. A new array is created and the indices are stored inside it. The size of the new arrays MUST BE the same as the number of items > 50. The function returns the new array which is then printed out by main. Here...
Hi I need a fix in my program. The program needs to finish after
serving the customers from the queue list.
Requeriments:
Headers:
DynamicArray.h
#ifndef DynamicArray_h
#define DynamicArray_h
#include
using namespace std;
template
class DynamicArray
{
V* values;
int cap;
V dummy;
public:
DynamicArray(int = 2);
DynamicArray(const DynamicArray&);
~DynamicArray() { delete[] values; }
int capacity() const { return cap; }
void capacity(int);
V operator[](int) const;
V& operator[](int);
DynamicArray& operator=(const DynamicArray&);
};
template
DynamicArray::DynamicArray(int cap)
{
this->cap = cap;
values =...
Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...
Question 1 In the following incomplete C program: #include stdlib.h> #include <stdio.h> int main () { int i, n, max; int array[100]; ... } return 0; } an array of random int values is populated with n random values. Write only the code to find the location of the maximum value in the array. Question 2 Follow these instructions in your Linux account: 1. Create a file called data.txt in the root folder in your account. 2. Create a new...
How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses mutex to lock and unlock the shared resource (dotstr.sum) for access control as shown below. pthread_mutex_lock (&mutexsum); dotstr.sum += mysum; printf("Thread %ld did %d to %d: mysum=%f global sum=%f\n", offset,start,end,mysum,dotstr.sum); pthread_mutex_unlock (&mutexsum); 2. Modify dot1m.c program to use reader-writer lock (instead of mutex). Replace the codes (for mutex) by the codes (for reader-writer lock). To initialize reader-writer lock, pthread_rwlock_initializer. At the end,...