You are on page 1of 1

1. Write the following functions: a. int prime (int n) returns 1 if n is a prime number, 0 otherwise. [2 marks] b.

int numconcat (int m, int n) returns the number which is a concatenation of m and n, e.g., numconcat(561, 82) returns the number 56182 [3 marks] c. We will say two primes form a concat-pair if both of their concatenations are also prime. For example, 37 and 67 is a concat-pair since the concatenations 3767 and 6737 are primes. Write a main () to create a program that finds all pairs of primes between N1 and N2 (N1 < N2 are taken as input from the user) such that they form a concat-pair. [3 marks] Submit the program in the file concatprimes.c Submit the output corresponding to N1=500, N2=600, in the file concat.out [2 marks] 2. Write the following program : a. void printarray (int A[], int n) : Print the array elements. b. int sortedinsert (int A[], int n, int x) that takes as parameter an array of integers A, the size of the array n, and an integer x. Assume that this array A is sorted in ascending order. The element x is inserted in the array A if it is not present already, so that the array continues to remain sorted. The function must return the size of the new array. c. Write a main ( ) that reads an integer n from the user, and then it reads n array elements. Upon reading each element, the function sortedinsert() must be called to insert the element. Print the array after each call by calling printarray () . 3. a. Suppose you are given a nxm rectangular grid. You start from 0,0 and have to reach m-1,n1. You may move one step to the right, or one step up. How many such paths are there? Write a recursive function to solve this problem. int countpaths (int m, int n) ; For example, there are 6 paths from 0,0 to 2,2. They are: (0,0), (0,1), (0,2), (1,2), (2,2) (0,0), (0,1), (1,1), (1,2), (2,2) (0,0), (0,1), (1,1), (2,1), (2,2) (0,0), (1,0), (1,1), (1,2), (2,2) (0,0), (1,0), (1,1), (2,1), (2,2) (0,0), (1,0), (2,0), (2,1), (2,2) b. Repeat Part a with the added constraint that you cannot make more than k consecutive steps to the right, or more than k consecutive steps upwards. For m=n=3, and k=2, there are 2 solutions. They are: (0,0), (0,1), (1,1), (1,2), (2,2) (0,0), (1,0), (1,1), (2,1), (2,2) Write a recursive function int countkpaths (int m, int n, char last, int k) ; c. Write a main ( ) that calls the above functions.

You might also like