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 C program to combine two arrays of the same size arranged in order descendant....
Write a C program to combine two arrays of the same size arranged in order descendant. Test data : Enter the number of elements to be stored in the first array: 3 Input 3 elements in the arrangement: element [0]: 1 element [1]: 2 element [2]: 3 Enter the number of elements to be stored in the second array: 3 Input 3 elements in the arrangement: element [0]: 1 element [1]: 2 element [2]: 3 Expected output: The combined array...
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...
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.
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...
In C++ Write a simple program to generate random integers between X and Y and store...
In C++ Write a simple program to generate random integers between X and Y and store them in a file, one number per line. The user should input the number of elements to generate and the boundary numbers X and Y (e.g. the inputs 100, 0, 999 mean your program should generate a list of 100 integers with values between 0 and 999, inclusive).
language: JAVA the Problem Below are a series of problems you need to solve using recursive...
language: JAVA the Problem Below are a series of problems you need to solve using recursive methods. You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. (15 points for main method) DescArrayCheck   Write a recursive method that checks whether an array of integers -...
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....