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
xenn [34]
3 years ago
14

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:
OleMash [197]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
Instructions Write a program that allows the user to enter the last names of five candidates in a local election and the number
KengaRu [80]

Here is code in java.

import java.util.*;

class Election

{ //main method

   public static void main(String args[]){

      // create an object of Scanner class to read input

      Scanner s = new Scanner(System.in);

      // string array to store name of candidates

       String name[] = new String[5];

       // int array to store vote count of candidates

       int v_count[] = new int[5];

       double p = 0;

       int i = 0, sum = 0, high =0, win = 0;

for(i = 0; i < 5;i++)

       {

          System.out.print("last name of Candidate " + (i+1) + ":");

          // read name of Candidate

          name[i] = s.next();

          System.out.print(" number of votes received: ");

          // read vote of Candidate

           v_count[i] =s.nextInt();

if(v_count[i] > high)

              {

                  // highest vote

                  high = v_count[i];

                  win = i;

              }

              // total vote count

           sum +=v_count[i];

       }

     // printing the output

      System.out.println("\nCandidate\tVotes Received\t% of TotalVotes\n");

       for(i = 0; i < 5;i++)

       {

           // % of vote of each Candidate

           p =(v_count[i]*100)/sum;

           // print the output

          System.out.println(name[i] + "\t\t" + v_count[i] + "\t\t\t" +p);

       }

       // print the total votes

      System.out.println("\nTotal Votes:\t" + sum);

      // print the Winner of the Election

       System.out.println("Winner of the Election is: " + name[win]);

   }

}

Explanation:

Create a string array "name" to store the name of candidates.Create Integer array "v_count" to store votes os each candidates.Calculate total votes of all candidates and find the highest vote amongst all candidate and print it.Find % of votes received by each candidate.Print these stats.

Output:

last name of Candidate 1:patel

number of votes received: 54

last name of Candidate 2:singh

number of votes received: 76

last name of Candidate 3:roy

number of votes received: 33

last name of Candidate 4:yadav

number of votes received: 98

last name of Candidate 5:sharma

number of votes received: 50

Candidate       Votes Received  % of TotalVotes

patel           54                      17.0

singh           76                      24.0

roy             33                      10.0

yadav           98                      31.0

sharma          50                      16.0

Total Votes:  311

Winner of the Election is: yadav

8 0
2 years ago
Write a program that will ask user to enter your name and count number of character ?
Svet_ta [14]

Answer:

672

Explanation:

because

8 0
3 years ago
Technician A says that the excessive length of a heater hose is intended to protect the heater core from undue stress. Technicia
hammer [34]
It is both true that <span>Technician A says that the excessive length of a heater hose is intended to protect the heater core from undue stress. Technician B says that excessive wear adds to the length of a heater hose, and a replacement heater hose should be roughly three to four inches shorter than its predecessor.  So the asnwer is letter B.</span>
6 0
3 years ago
Why is hard disk called random access medium​
Effectus [21]

Answer:

Data is accessed in a random-access manner, meaning the individual blocks of data can be stored and retrieved in any given order or time.

Explanation:

5 0
1 year ago
Read 2 more answers
Wht is RAM called Random Access memory and ROM IS called Read only memory​
nasty-shy [4]

Answer: Computer memory is of two basic type – Primary memory(RAM and ROM) and Secondary memory(hard drive,CD,etc.). Random Access Memory (RAM) is primary-volatile memory and Read Only Memory (ROM) is primary-non-volatile memory.

Explanation: Hope this helps :)

6 0
2 years ago
Other questions:
  • The protocol that enables computers on the Internet to communicate with one another is called _____.
    10·2 answers
  • Complete the sentence to identify disadvantages of top-down programming design. Choose all that apply. Top-down programming desi
    9·1 answer
  • why does it not let me create a new account when i log out of this one and go to join now after i fill everything out it wont le
    11·1 answer
  • Describe a time when you influenced someone else’s knowledge around technology, whether it be an app, a new gadget, etc. What di
    14·1 answer
  • How can I make a video game?
    8·2 answers
  • Describe the basic features of the relational data model and discuss their importance to the end user and the designer. Describe
    9·1 answer
  • Q13. On which option do you click to
    12·1 answer
  • Implement a metho d to nd the k-th largest element in an array of size N using a minimum priority queue (MinPQ). Assume that the
    8·1 answer
  • The physical parts of Computer are kwon as<br>​
    15·1 answer
  • _________ are the special effects that you see when one slide changes to another in slide show view​
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!