Question

In: Computer Science

Construct an array of 1000 random integers within range [0, 100] An input file input.txt is...

Construct an array of 1000 random integers within range [0, 100]

An input file input.txt is provide. Each line of input.txt is a query integer that you need to check how many of that number is in your random integer array. For each query integer, fork a new child process to do the counting. The output is for each input query, output the count and child process id. For example:

$> query: 13    count: 5    pid: 13342

$> query: 22    count: 3    pid: 13357

5
13
24
6
17
20
1
51
36
42
2
19
67
35
64
91
96
84
72

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

SINCE IT IS USING RANDOM VALUE THAT'S WHY OUTPUT DIFFER

#include <iostream>

#include <unistd.h>

#include <sys/wait.h>

#include <string.h>

#include <stdlib.h>

#include <time.h>

#include <stdio.h>

using namespace std;

int main(int argc, char *argv[])

{

  int arr[1000];

  int number;

  char line[10];

  pid_t pid;

  FILE *f = fopen("input.txt", "r"); //opening file

  //generating numbers

  srand(time(0)); //seed to random number generator

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

    arr[i] = rand() % 101;

  cout << endl;

  while (fgets(line, 10, f))

  {

    sscanf(line, "%d", &number); //reading the number

    pid = fork();

    if (pid == 0)

    {

      int count = 0, index = 0;

      while (index < 1000)

      {

        if (arr[index] == number)

          count++;

        index++;
      }

      cout << "query: " << number << "\tcount: " << count << "\tpid: " << getpid() << endl;

      fclose(f);

      exit(1);
    }

    else

    {

      wait(NULL);
    }
  }

  cout << endl;

  return 0;
}

input.txt

5
13
24
6
17
20
1
51
36
42
2
19
67
35
64
91
96
84
72

Related Solutions

(C++) Create a data file and name it "input.txt". manually save 10 integers into the file....
(C++) Create a data file and name it "input.txt". manually save 10 integers into the file. Write a program to read the data and calculate the average of events and odds, separately. Print out the average values.
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
Write a C++ pgm which 1.reads an input file named 'input.txt' 2.content of input.txt are not...
Write a C++ pgm which 1.reads an input file named 'input.txt' 2.content of input.txt are not known to you 3.copies content of input file into an output file named 'output.txt' 4.contents of output.txt should be exactly the same as contents of input.txt 5.the user should be given a choice to select input and output file in any folder 6.remember to check that input file opened successfully
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
Write a Python program called “exam.py”. The input file, “input.txt”, is given to you in the...
Write a Python program called “exam.py”. The input file, “input.txt”, is given to you in the Canvas exam instructions. Name your output file “output.txt”. Ensure the program consists of a main function and at least one other function. Ensure the program will read the input file, and will output the following information to the output file as well as printing it to the screen: output the text of the file to screen output the number of words in the file...
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1...
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1 and 100 inclusive Then, creates two more 100-element arrays, one holding odd values and the other holding even values. Prints both of the new arrays to the console. In C++. Thank you!
Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0.
This program is for C.Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0. Combine the elements of A + B to create two- dimensional array C = A + B. Display array A, B and C to the screen for comparison. (Note a[0] + b[0] = c[0], a[1] + b[1] = c[1], etc.)
Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT