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

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].
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.
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!
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.
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...
C++ please 1. Randomly assign integers in the range of 1-100 to a two-dimensional array. Write...
C++ please 1. Randomly assign integers in the range of 1-100 to a two-dimensional array. Write a program that finds the average value of the rows and the average value of the columns. Display the averages. 2. Create an array of randomly generated numbers in any range. Write a function that takes the array as an argument and returns an array that consists of only the even numbers in the original array. Use the function in a program. 3. Create...
Find the number of integers between 100 and 1000 that are
Find the number of integers between 100 and 1000 that are (i) divisible by 7  (ii) not divisible by 7      
Write a java program that inserts 25 random integers from 0 to 100 in order into...
Write a java program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT