Question

In: Computer Science

Use c++ programming Construct an array of 1000 random integers within range [0, 100] An input...

Use c++ programming 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 Here is the input.txt 5 13 24 6 17 20 1 51 36 42 2 19 67 35 64 91 96 84 72

Use c++ programming

Solutions

Expert Solution

Solution:

first make a text file with name "input.txt" and write the content to the file:

10
20
30
40
50
60

Make sure each number is in one line and file is accessible to the program.

Now, run the following program,

#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;

}

Output:

Please give thumbsup, if you like it. Thanks.


Related Solutions

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   ...
Use C Programming - Given an array of integers and a number K, find the smallest...
Use C Programming - Given an array of integers and a number K, find the smallest element in array greater than or equal to K. If such element exists in the array, display it otherwise display "-1". Example: Input:     8     1 3 4 7 8 9 9 10     5     where: First line represents the number of elements in the array. Second line represents the elements in the array. Third line represents the value of K. Output:     7 Explanation:...
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...
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].
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
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.
You can use any programming languages Using a random generator, compute 1000 integers between 1 and...
You can use any programming languages Using a random generator, compute 1000 integers between 1 and 1000. There will be duplicates in the array. Sort the array using bubble sort and merge sort. The two sorted arrays should agree. Then pick at random one element from your sorted array and use a binary search to find its position in the array.
Write a C++ program to input 10 scores (range from 0-100), calculate the average, then display...
Write a C++ program to input 10 scores (range from 0-100), calculate the average, then display the student's name, and a letter grade such as A, B, C, D, or F. It shall have a Student class with necessary private data members and constructor and public methods, such as getName, getGrade, calcScore, convertToGrade, etc. Try to create a Student object in main(), then invoke its methods to perform the tasks.
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT