Question

C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program...

C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED )

Q]. Write a program that displays all the prime numbers in the given array with the following constraint.
Constraint: Only those prime numbers should be displayed whose location is a composite number.
Although you may have several prime numbers in the array, only those prime numbers should be displayed which are stored at non-prime locations. Remember that the first position in an array corresponds to the location/index 0.


Note: Negative integers, 0 and 1, are neither prime nor composite.

Sanity checks required:
The number of input parameters should be in accordance with the exercise number.
You can assume the input parameters to be integers. However, you should verify them for their sign (positive/negative).


Input format: ./a.out [exercise number] [size of the array] [Elements of the array (space separated)]
Output format: Elements separated by space
   Print # if there is no element that satisfies the given constraints.


Test Case1:
./a.out 3 9 2 64 7 83 67 7 3 9 -15    (67 and 3 occur at locations 4 and 6, respectively)
67 3
Test Case2
: ./a.out 3 7 5 7 346 1 2 19 17   
2 17
Test Case 3
: ./a.out 3 9 10 20 2 2 6 1 0 8 1     (no number will be displayed)
#
Test Case 4: ./a.out 3 4 10 20 2140 2 20 7       (Incorrect number of total arguments)
error  

Note the following points.
1] In the test case 1, -15 is not considered for printing since it is not prime.
2] In the test case 2, 5 and 7 are not printed because their locations are 0 and 1, none of which is a composite number.
3 ] In the test case 3, even though 0 and 1 occur at composite locations, they are not printed because they are not prime.

Homework Answers

Answer #1


#include <stdio.h>
#include<math.h>

int prime(int n){
    // if n<2 return false (0,1 are not primes.)
    if(n < 2){
        return 0;
    }
    
    // Check for factors of a number in range (2,sqrt(n)), as for a 
    // number to be composite there should be atleast one factor in this range.
    for(int i=2;i<=(int)(sqrt(n));i++){
        if(n%i == 0){
            return 0;
        }
    }
    return 1;
}

int main(int argc, char *argv[])
{
    //argc gives number of inputs and char* argv[] stores each input.
    // n stores size of array.
    int n = atoi(argv[2]);
    
    // I am assuming the excersize number is provided correctly as your
    // have not provided the meaning of it.
    
    // Checking if total number of arguments are correctly given.
    if(argc != 3 + n){
        printf("error\n");
    }
    
    else{
        
        // Copying numbers from argv to int array.
        // atoi(x) converts string x to integer.
        int arr[n];
        for(int i=3;i<argc;i++){
            arr[i-3] = atoi(argv[i]);
             
        }
        
        // Setting primes_present = 0, which stores number of primes present in composite locations.
        int primes_present = 0;
        for(int i=2;i<n;i++){
            // Checking if number is greater than 0, if it is prime and present in composite location.
            // If yes print it.
            // Use prime() function to check if a number is prime.
            if(arr[i] > 0 && prime(arr[i]) == 1 && prime(i) == 0){
                printf("%d ",arr[i]);
                primes_present++;
            }
        }
        
        // If no primes are present in composite locations, print #.
        if(primes_present == 0){
            printf("#");
        }
        
       
    }

    return 0;
}

I have added comments for better understanding. I would love to resolve any queries in the comments.

Please consider dropping an upvote to help a struggling college kid :)

Happy Coding !!

Know the answer?
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
Similar Questions
Write a program in ARM assembly language that copies each element of array A to consecutive...
Write a program in ARM assembly language that copies each element of array A to consecutive fourth elements of array B, i.e., A[0] to B[0], A[1] to B[3], A[2] to B[7], etc. The array A is 12 elements long, and each element is a number that is 32 bits (1 word) wide. Assume the base address of array A is in register R2, and the base address of array B is in R3.
EMBEDDED: Write a program in ARM assembly language that copies each element of array A to...
EMBEDDED: Write a program in ARM assembly language that copies each element of array A to consecutive fourth elements of array B, i.e., A[0] to B[0], A[1] to B[3], A[2] to B[7], etc. The array A is 12 elements long, and each element is a number that is 32 bits (1 word) wide. Assume the base address of array A is in register R2, and the base address of array B is in R3.
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
C++ program for : 1. Given an array of real numbers and the dimension n (n...
C++ program for : 1. Given an array of real numbers and the dimension n (n is entered from the keyboard). B form an array, each element bi - arithmetic mean of the array elements and excluding ai. 2. Given an array of integers dimension n. Change the array so that all the elements of the module which is not equal to the maximum element of the array, replaced by zero, and equal - unit. 3. Given an array of...
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT(...
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT( TRUE/ FALSE) QUIZ 8 Array Challenge Have the function ArrayChallenge(arr) take the array of numbers stored in arr and return the string true if any two numbers can be multiplied so that the answer is greater than double the sum of all the elements in the array. If not, return the string false. For example: if arr is [2, 5, 6, -6, 16, 2,...
Write a program to determine the minimum element in an array of ten elements. The program...
Write a program to determine the minimum element in an array of ten elements. The program should have the following: 1. Class Name as ArrayProcessing. The main method should create an array of size 10 2. There should be two methods besides the main method in the class namely inputArray and MinimumElement 3. InputArray method should assign the ten elements in the array. Use scanner to input these elements. The array of 10 numbers in the method "InputArray" should be...
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Write a code in c++ using linear insertion following the steps below. Comment your work. 1....
Write a code in c++ using linear insertion following the steps below. Comment your work. 1.    Ask the user for the name of a file containing data. If it does not exist, the program should display an error, then ask for a new file name. Entering an asterisk (*) as the first and only character on a line should terminate the program. 2.     You can use a statically-allocated one-dimensional array of doubles for this with length 100. You...
Write a program which: Write a program which uses the following arrays: empID: An array of...
Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to hold each employee’s gross salary....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT