1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
DIA [1.3K]
3 years ago
12

Write a C program that creates two threads to run the Fibonacci and the Runner processes. Threads will indicate the start and th

e end of their work by printing statements in the format "Thread K Starting" "Thread K Finished", where K is the thread number. When the threads finish, the parent thread will output the results generated by the two threads by printing in the format, "The sum of first 5 numbers is: 15" and "The first 5 numbers in the Fibonacci sequence are: 0, 1, 1, 2, 3" if the user had input 5 as the value of N.
Computers and Technology
1 answer:
Maurinko [17]3 years ago
3 0

Answer:

see explaination

Explanation:

#include <pthread.h>

#include <stdio.h>

int sumOfN = 0;

int arr[1000];

int n;

void * findSumOfN(void *a){

printf("Thread 1 Starting\n");

sumOfN = (n * (n+1)) / 2; //finds sum of first n natural numbers

printf("Thread 1 Finished\n");

pthread_exit(0);

}

void * fibonacci(void *a){

printf("Thread 2 Starting\n");

arr[0]=0;

arr[1]=1;

for(int i=2;i<n;i++) //find fibonacci numbers iteratively

arr[i]=arr[i-1]+arr[i-2];

printf("Thread 2 Finished\n");

pthread_exit(0);

}

int main(void){

printf("Please enter the value of n \n");

scanf("%d", &n);

if (n <= 0)

{

printf("Wrong input ! \n"); //input validation

return 0;

}

pthread_t thread1, thread2;

pthread_create(&thread1,NULL, findSumOfN, NULL); //Create threads

pthread_create(&thread2,NULL, fibonacci, NULL);

pthread_join(thread1,NULL); //Start threads

pthread_join(thread2, NULL);

printf("The sum of first %d numbers is : %d \n",n, sumOfN);

printf("The first %d fibonacci numbers are : \n",n);

for (int i = 0; i < n; ++i)

{

printf("%d ", arr[i]);

}

printf("\n");

return(0);

}

You might be interested in
Write a 2-to-3-page research paper describing in plain language the basic purpose, theory, and implementation of GUI application
laila [671]

Answer:

       

Explanation:

4 0
3 years ago
Traffic collisions are among the top killers of children in America.
Blababa [14]
Whats the question if its true or false, then true
8 0
3 years ago
Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is the first chara
Nostrana [21]

Answer:

char firstLeter = name.charAt(0);

Explanation:

Consider the program below:

public class num2 {

   public static void main(String[] args) {

     String name = "Smith";

     char firstLetter = name.charAt(0);

       System.out.println(firstLetter);

   }

}

In this program, A string name is defined and assigned the value "Smith".

Java's String method charAt() is called and passed the argument of index 0. This causes it to output the character S. Since this is the first letter in the String is at index 0.

5 0
3 years ago
4. An abstract data type is defined as _____.
marusya05 [52]

Answer: object that persist set of values and operations as behavior

Explanation: Abstract data types(ADT)are those object value which are dependent upon the collection of the values and collection of the operations.The organization of the data object is not specified in ADT but the operations that are to be carried out are defined in it.

The implementation individuality displayed by the ADT and hiding of the representation or design makes it abstract.

8 0
3 years ago
g Q 2 Suppose I need a data structure having the following characteristics: a) must be very space efficient, and b) I have a goo
SIZIF [17.4K]

Explanation:

a.I would choose an advanced data structure like  dispersion tables.

Scatter tables, better known as hash tables, are one of the most frequently used data structures. To get an initial idea, scatter tables make it possible to have a structure that relates a key to a value, such as a dictionary. Internally, the scatter tables are an array. Each of the array positions can contain none, one or more dictionary entries.

It will normally contain a maximum of one, which allows quick access to the elements, avoiding a search in most cases. To know at which position in the array to search or insert a key, a scatter function is used. A scatter function relates each key to an integer value. Two equal keys must have the same dispersion value, also called a hash value, but two different keys can have the same dispersion value, which would cause a collision.

B. If I eliminated the characteristic b, a possible candidate keeping the previous characteristics would be: Balanced binary trees

Balanced binary trees are data structures that store key-data pairs, in an orderly way according to the keys, and allow quick access to the data, given a particular key, and to go through all the elements in order.

They are appropriate for large amounts of information and have less overhead than a scatter table, although the access time is of greater computational complexity.

Although the storage form has a tree structure internally, this is not externalized in the API, making the handling of the data transparent. They are called balanced because, each time they are modified, the branches are rebalanced in such a way that the height of the tree is as low as possible, thus shortening the average time of access to the data.

5 0
3 years ago
Other questions:
  • Which of the following would you click to access Help and Support?
    6·2 answers
  • What is cryptocurrency ? I need to do a research one it please help!
    6·1 answer
  • TWO MINUTES LEFT<br> ________ __________ is the physical arrangement of devices on a network.
    9·1 answer
  • Scenes for films are almost always filmed in the same sequence they appear in the script.
    14·1 answer
  • A computer essentially takes input, processes it, and produces output. Which person developed a machine in the mid-1880s that ac
    6·1 answer
  • It is believed that Taiwan is the original home of these early Oceania explorers. Maori Lapita Polynesians Aborigines
    13·2 answers
  • Police officers conducting traffic stops on minority drivers more than non-minority drivers is an example of what?
    8·1 answer
  • Searching for a particular record in a database is called “querying the data.”<br> True<br> False
    9·2 answers
  • PLEASE HELP ILL GIVE BRAINLIEST IMMEDIATLEY TO THE CORRECT ;-;
    10·1 answer
  • Please help!!
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!