Question

C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished)...

C programm , ´hello i need your help

-Given the C program primes.c with the following main method:

int main()
{
    int num, res;
    char buffer[11];
    bool finished = false;
    while (!finished)
    {
        printf("Enter n > 0 or quit\n");
        scanf("%10s", buffer);
        if (strcmp(buffer, "quit") == 0) {
            finished = true;
        } else {
            // Convert input to number and compute n-th prime
            num = atoi(buffer);
            if (num > 0) {
                res = nth_prime(num);
                printf("Prime #%d is %d\n", num, res);
            } else {
                printf("Invalid input!\n");
            }
        }
    }
    return 0;
}

The function nth_prime calculates the (very complex) then n-th prime for a parameter n>0(see full program code in primes.c ).

Adjust the program so that the calculation and output of the n-ten primes after user input from n is adopted by a child process so that the user can start further calculations by inputs in the parent process while calculations are in progress.

If the user enters the command wait, the parent process should wait for all calculations to complete. If the program also quit ends, it is also necessary to wait for the termination of all child processes.

Note: Test your implementation with input values n in the five-digit range, so that the calculations take several seconds.

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

Program :

int nth_prime(int num) {
int c;
for ( c = 2 ; c <= num - 1 ; c++ ) {
if ( num%c == 0 )
return 0;
}
if (c == num)
return 1;
}

int main()
{
int num, res;
char buffer[11];
bool finished=false;
while (!finished)
{
printf("Enter n > 0 or quit\n");
scanf("%10s", buffer);


if (strcmp(buffer, "quit") == 0) {
finished = true;
} else {   
num = atoi(buffer);
if (num > 0) {
res = nth_prime(num);
printf("Prime #%d is %d\n", num, res);
} else {
printf("Invalid input!\n");
}
}
}
return 0;
}

***********************************************************************************************

Output :

Case 1 :

Enter n > 0 or quit

123456

Prime #123456 is 0

Case 2 :

Enter n > 0 or quit

quit

***********************************************************************************************

Add a comment
Know the answer?
Add Answer to:
C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished)...
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
  • Help with C++ reverse program with leading zeros. I need to put the line from the...

    Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code. #include <iostream> #include <cstdlib> #include <iostream> using std::cout; using std::cin; using std::endl; //function templates int reverseNum(int); int main() { //variables char buf[100]; int num; while (true) { //prompt user for input cout << "Enter the number...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • Linux & C code help with creating a child process and kills itself with SIGNAL. I...

    Linux & C code help with creating a child process and kills itself with SIGNAL. I provided most of the code, I need some help to finish. Please provide output screen shot as well. Thank you! A Process creates a child process. Child process will print out "hello 0" "hello 1" ..... 1 line per second. After a certain number of seconds(user input), it kills itself WITH signal mechanism ONLY, don't use exit() on the child. You can use alarm()...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • System Programming in C

    Explain what the problem is within the program. Fix the problem when you create a child process per column. The code is given below. So basically, after the child processes have successfully excuted their code, the final print statement does not give the correct values. It prints the original matrix rather than the multiplied matrix.#include  #include  #include  #include  int main(int argc, char *argv[]){ int row = 0; int column = 0; row = atoi(argv[1]); column = atoi(argv[2]); int *A = ...

  • Question 3: [2+2] a)What, to whom and how many values a fork system call returns? b)Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in

    Question 3:                  [2+2]a) What, to whom and how many values a fork system call returns? b) Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in which Terminal login must be your arid number.int main(){      pid_t  fork_return;        fork_return = fork(); if (fork_return == 0) {                  execlp("/bin/ls", "ls", NULL);                 printf("Child process ID: %d\n", getpid());      exit(0);        }        else { wait (NULL);                 printf("Parent process ID: %d\n", getpid());         }     return 0;}

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • Question 3: [2+2] a)What, to whom and how many values a fork system call returns? b)Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in

    Arid No is 19-arid-898Question 3:                  [2+2]a) What, to whom and how many values a fork system call returns? b) Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in which Terminal login must be your arid number.int main(){      pid_t  fork_return;        fork_return = fork(); if (fork_return == 0) {                  execlp("/bin/ls", "ls", NULL);                 printf("Child process ID: %d\n", getpid());      exit(0);        }        else { wait (NULL);                 printf("Parent process ID: %d\n",...

  • I must execute in C using parallel Programming techniques the following serial program: void producer_consumer(int *buffer,...

    I must execute in C using parallel Programming techniques the following serial program: void producer_consumer(int *buffer, int size, int *vec, int n) {    int i, j;    long long unsigned int sum = 0;    for(i=0;i<n;i++) {        if(i % 2 == 0) {   // PRODUCER            for(j=0;j<size;j++) {                buffer[j] = vec[i] + j*vec[i+1];            }        }        else {   // CONSUMER            for(j=0;j<size;j++) {...

  • C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

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