Section 2
PDS Lab Lab-5 28.04.2023
Instructions:
● This lab is based on the topics: 1D array, Functions and Recursion
● You should save each program with the file name as specified against each problem as
<Lab#>-<Assignment#>-<Roll#>.c. For example, 05-01-22CS10006.c to save Program to
1st assignment in Lab 5 with Roll Number 22CS10006
● You should upload each program to the Moodle system. Also, copy+paste your
programs to the text window on the test page.
● You must use the Code::Blocks to write, compile and run your programs.
● There are three problems and the maximum time allowed is 180 minutes.
● Do not use pointers or 2D arrays.
Problem 1
Consider an array of integers of size N. Do the following:
a. Read n elements (n≤N) and store them in the array, where N = 100.
Use macro for setting the limit.
(Hint: Set the upper limit using #define N 100)
b. Rearrange the elements in the array so that
i. all negative numbers on the left side of the array and
ii. all positive numbers on the right side of the array
Note the following:
● You should not use any other array.
● The rearranging should not change the original order of the entered numbers.
[10+10+10 = 30]
Test cases:
# INPUT OUTPUT
5 Original Array: -1 5 6 -4 -3
1
-1 5 6 -4 -3 Rearraged Array: -1 -4 -3 5 6
5 Original Array: -1 -3 4 5 6
2
-1 -3 4 5 6 Rearranged Array: -1 -3 4 5 6
6 Original Array: -1 -2 -3 -4 -5 -6
3
-1 -2 -3 -4 -5 -6 Rearranged Array: -1 -2 -3 -4 -5 -6
6 Original Array: 1 2 3 4 5 -6
4
1 2 3 4 5 -6 Rearranged Array: -6 1 2 3 4 5
4 Original Array: 1 4 7 23
5
1 4 7 23 Rearranged Array: 1 4 7 23
6 101 Error: n > 100
Solution 1
// Code creator: Abhishek (
[email protected]) and Nishkal (
[email protected])
#include <stdio.h>
#define N 100 // Maximum number of elements in the array
int main()
{
int n, arr[N]; // n is the number of elements in the array taken from user
int i, j=0, temp; // i is the index of the array, j is the index of the first
positive number
printf("\nEnter the number of elements (n <= %d): ", N);
scanf("%d", &n);
if (n > N) // Checking if the number of elements is greater than the maximum number of
elements
return printf("Error: n > %d", N); // If yes, then return an error message
printf("\nEnter %d elements: ", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("\nOriginal Array: ");
for (i = 0; i < n; i++) // Printing the original array
printf("%d ", arr[i]);
for (i = 0; i < n; i++)
{
// If the element is negative,
// then shift all the elements to the right of the first positive number by one
position
if (arr[i] < 0)
{
temp = arr[i];
for (int k = i; k > j; k--) // Shifting all the elements to the right of the
first positive number by one position
arr[k] = arr[k - 1];
arr[j] = temp;
j++; // Incrementing the index of the first positive number
}
}
printf("\nRearranged Array: ");
for (i = 0; i < n; i++) // Printing the rearranged array
printf("%d ", arr[i]);
return printf("\n");
}
Problem 2
A prime number (or a prime) is a natural number greater than 1 that is not a product of two
smaller natural numbers. A natural number greater than 1 that is not prime is called
a composite number. For example, 5 is prime because the only ways of writing it as a
product, 1 × 5 or 5 × 1, involve 5 itself. However, 6 is composite because it is a product (2 × 3)
in which both numbers are smaller than 6.
There is one oldest and best-known unsolved problems in number theory and all
of mathematics is called Goldbach's conjecture. It asserts that all positive even integers ≥ 4
is the sum of two prime numbers.
For examples:
6=3+3
8=3+5
10 = 3 + 7
12 = 5 + 7
14 = 3 + 11
28 = 11 + 17 and so on…
You have to write a program for the following:
● Write a function int primeCheck (int i) {...} to test if i is a prime
number or not.
● Use this function and for any even integer n, show that n = p + q, where p and q
are two prime numbers. Your program should read the value of n from the user.
[10+20 = 30]
Test cases:
# INPUT OUTPUT
1 2 -1
2 5 -1
3 4 2, 2
4 60 7, 53
5 124 11, 113
6 550 3, 547
Solution 2
// code creator: Vaishnavi Munghate ([email protected])
#include <stdio.h>
// returns 0 if i is not prime, 1 if prime
int primeCheck (int i) {
// input validation
if(i<2) return 0;
// iterate over possible divisors of i
int divisor;
for( divisor = 2 ; divisor <= i-1 ; divisor++ ){
// return 0 if any divisor divides i
if( i % divisor == 0) return 0;
}
// if none divisor divides i, return 1
return 1;
}
int main() {
// take input
int n;
scanf("%d", &n);
// input validation
if(n<4 || n%2){
printf("-1\n");
return 0;
}
// break n into p & q
int p, q;
// iterate over possible prime values of p & q
for(p = 2 ; p<= n-2 ; p++ ){
int q = n-p;
// print result if both p & q are prime
if( primeCheck(p) && primeCheck(q) ){
printf("%d, %d\n", p, q);
break;
}
}
return 0;
}
Problem 3
A function void swap(int p, int q) {...} if called with two values p and q, then
after its execution, it will interchange the values in them. For example, if p = 555 and q = -555,
then after the execution of swap function, it will be p = -555 and q = 555.
Write a program to do the following:
(a) Define the function void swap(int p, int q).
(b) Define an array of integers of size n and initialize it reading n values from the user.
(c) Write a recursive function void reverse(int *a){…} which would reverse the
ordering of the elements in the array.
For example: [1, 2, 3, 4, 5] will be [5, 4, 3, 2, 1]
Note that you can pass an entire array a of integers to the function reverse() as
int *a. This is a special feature in C language, which you will learn in due class.
(d) Write another recursive function void cyclicShift (int *a, int
direction) {…} which would left cyclic shift (if direction = 1) or right cyclic shift
(if direction ≠ 1).
For example:
Input array: [5, 4, 3, 2, 1]
The array after left cyclic shift: [4, 3, 2, 1, 5]
The array after right cyclic shift: [1, 5, 4, 3, 2]
Note the following:
● You may use swap() function, if necessary.
● Do not use any additional array other than the array to store the input values.
[5+15+20 = 40]
Test cases:
# INPUT OUTPUT
Enter the size of the Original array: 1 2 3 4 5
array and direction: 5 1 Array after reverse: 5 4 3 2 1
1 Enter 5 integers: Array after left cyclic shift:
1 2 3 4 5 4 3 2 1 5
Enter the size of the Original array: 3 4 5 6
array and direction: 4 -1 Array after reverse: 6 5 4 3
2 Enter 4 integers: Array after right cyclic
3 4 5 6 shift: 3 6 5 4
Enter the size of the Original array: -1 -3 4 5 6
array and direction: 5 1 Array after reverse: 6 5 4 -3
Enter 5 integers: -1
3
-1 -3 4 5 6 Array after left cyclic shift:
5 4 -3 -1 6
Enter the size of the Original array: -1 -3 4 5 6
array and direction: 5 Array after reverse: 6 5 4 -3
234 -1
4
Enter 5 integers: Array after right cyclic
-1 -3 4 5 6 shift: -1 6 5 4 -3
Solution 3:
// Code creator: sairam([email protected])
#include <stdio.h>
void swap(int *p, int *q) {
int temp = *p;
*p = *q;
*q = temp;
}
void reverse(int *a, int start, int end) {
if (start >= end) {
return;
}
swap(&a[start], &a[end]);
reverse(a, start + 1, end - 1);
}
void cyclicShift(int *a, int n, int direction) {
if (n <= 1) {
return;
}
if (direction == 1) { // Left cyclic shift
int first = a[0];
for (int i = 0; i < n - 1; i++) {
a[i] = a[i + 1];
}
a[n - 1] = first;
} else { // Right cyclic shift
int last = a[n - 1];
for (int i = n - 1; i > 0; i--) {
a[i] = a[i - 1];
}
a[0] = last;
}
}
int main() {
int n,direction;
printf("Enter the size of the array and direction: ");
scanf("%d %d", &n, &direction);
int a[n];
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nOriginal array: ");
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
// Reverse the array using the recursive function reverse()
reverse(a, 0, n - 1);
printf("\nArray after reverse: ");
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
cyclicShift(a, n, direction);
// Left cyclic shift the array using the recursive function cyclicShift()
if(direction == 1){
printf("\nArray after left cyclic shift: ");
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}}
// Right cyclic shift the array using the recursive function cyclicShift()
else{
printf("\nArray after right cyclic shift: ");
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}}
printf("\n");
return 0;
}