Question

Part 1: Write a C program that takes an integer command line argument n, spawns n...

Part 1: Write a C program that takes an integer command line argument n, spawns n processes that will each generate a random numbers between -100 and 100, and then computes and prints out the sum of these random numbers. Each process needs to print out the random number it generates.

name the program 003_2.c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <stdlib.h>

int randomNumbers(n) //Function to generate and sum the 2 random numbers between -100 and 100 taking "n" as a parameter which will be taken as an input from command line arguement  

{

int upper = 100;//upper limit for the rand() function

int lower = -100;//lower limit for the rand() function.

int i;//variable for the "for loop" iterations

if(n==0)//if there are no spawns or the user given value is zero process will be exited

return 0;

for(i=0;i<=n;i++)//for loop to print the random numbers and their sum for number of iterations i.e. spawns given by user

{

int num1 = (rand() % (upper - lower + 1)) + lower;//generating first random number between -100 and 100

printf("Random Number1 for %d iteration : %d\n",i,num1);//Printing the first random number

int num2 = (rand() % (upper - lower + 1)) + lower;//generating second random number between -100 and 100

printf("Random Number2 for %d iteration : %d\n",i,num2);//Printing second random number

printf("Sum of 2 Random Numbers for %d iteration : %d\n",i,num1+num2);//Printing sum of two random numbers

}

}

int main()//main function

{

long int n;

printf("Enter number of spawns :");

scanf("%d",&n);//taking the input from the user through command line

randomNumbers(n);//calling the function randomNumbers() by passing the user input

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Part 1: Write a C program that takes an integer command line argument n, spawns n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program called test that takes one command line argument, an integer N. When...

    Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits

  • Write a program in C that takes a file name as the only argument on the...

    Write a program in C that takes a file name as the only argument on the command line, and prints out the number of 0-bits and 1-bits in the file int main ( int argc , char * argv [] ) { // Check if the user gave an argument , otherwise print " ERROR : no argument " // Check if the file can be read , otherwise print " ERROR : can ’t read " // Otherwise ,...

  • Write a C++ program that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60...

    Write a C++ program that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 49 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to square all 60 array elements. You should divide this update task among the n threads as evenly as possible. Print the array both before and after the update separately as...

  • Problem: Write a program that behaves as described below.If the first command-line argument after the program...

    Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate the...

  • Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusi...

    Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string (‘A’<->’Z’, ‘B’<->’Y’, ‘C’<->’X’, etc). You should divide this conversion task among the n threads as evenly as possible. Print out the string both before...

  • In C write a program that must accept one argument from the command line. The argument...

    In C write a program that must accept one argument from the command line. The argument is the name of the file containing the processes (for example: processes.csv).This file is comma-separated with three columns (process ID, arrival time and burst time) with each row for an individual process. You can assume that this file will have a maximum of 10 processes. For example Input: processes.csv ProcessID,Arrival Time,Burst Time 0,1,3 1,0,5 2,9,8 3,10,6 Where the first element is processes, the second...

  • Java!!! Write a program that takes a file name as its command line argument. It assumes...

    Java!!! Write a program that takes a file name as its command line argument. It assumes the file is a binary data file and checks the first 4 bytes of the file to see whether they contain the integer −889275714. It outputs “yes” or “no” or an error message if there was an IOException generated. (Trivia question: Why test for that particular value? Hint: Try it on several .class files.)

  • Your task is to write a C++ program that consumes integer values as command line arguments...

    Your task is to write a C++ program that consumes integer values as command line arguments and returns the arithmetic mean of these values. To increase the flexibility of the program, there should be no set number of arguments. To overcome this, we will require the argument argv[1] to be the number of integers the user enters. For example, if the user wants to calculate the arithmetic mean of 4 numbers, they would pass in 4 as the first argument...

  • Need help writing four short recursive programs: Write a program called Permutations.java that takes an integer...

    Need help writing four short recursive programs: Write a program called Permutations.java that takes an integer command-line argument, n, and enumerates all permutations of the first n letters in the English alphabet. Be sure to handle exceptions of the types: i) bad user input (i.e., double, string), ii) n > 26 or n < 0. Write a program called PermutationsK.java that takes two integers, n and k, as command-line arguments and prints out all P(n,k) permutations that contain exactly k...

  • Write a program file_max.c that reads a set of integer numbers from a file and prints...

    Write a program file_max.c that reads a set of integer numbers from a file and prints out the maximum number to standard output. The name of the input file should be specified as a command line argument.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT