0% found this document useful (0 votes)
38 views5 pages

Lab 01 Muskan

The document contains 3 programming tasks. Task 1 involves rearranging an array of integers so that even numbers are on the left and odd numbers on the right. Task 2 involves predicting a unique encryption key used to encrypt each character in a message. Task 3 involves copying elements from a 2D array into a 1D array while avoiding duplicates.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views5 pages

Lab 01 Muskan

The document contains 3 programming tasks. Task 1 involves rearranging an array of integers so that even numbers are on the left and odd numbers on the right. Task 2 involves predicting a unique encryption key used to encrypt each character in a message. Task 3 involves copying elements from a 2D array into a 1D array while avoiding duplicates.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

TASK : 01

1. Write a function in Java (or any language) to read a list of 10 integer numbers and arrange them
in such a manner that all the even numbers start from the left and all the odd numbers start
from the right.

Input: 1 2 3 5 7 2 2 7 8 9

Output: 1 3 5 7 7 9 2 2 2 8

package lab1.Task;
import java.io.*;

public class Main {


// function to rearrange the array in given way.
static void rearrangeEvenAndOdd(int arr[], int n)
{
// variables
int j = -1,temp;

// quick sort method


for (int i = 0; i < n; i++) {

// if array of element
// is odd then swap
if (arr[i] % 2 == 0) {

// increment j by one
j++;

// swap the element


temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

// Driver code
public static void main(String args[])
{
int arr[] = { 14, 16, 17, 45, 4, 10, 11, 45 };
int n = arr.length;

rearrangeEvenAndOdd(arr, n);

for (int i = 0; i < n; i++)


System.out.print(arr[i] + " ");
}
}

TASK :02

2. Suppose that we have the following message stored in string named message as
below: String message = “Hello world”;
The message above was encrypted in such a way that every character was encrypted
after adding a unique key to it. The message after encryption become:
Output: Igopt&^w{vo
Write a function that will help you in predicting the unique key used for each
character of our input message.

package lab1.Task;

public class Task2 {


public static void main(String []args)
{
String msg="Helloworld";
String decryupt= "Igopt&^w{vo";
char c;
char store;
for(int i =0 ; i<msg.length();i++) {
c=msg.charAt(i);
store=(char)(c+i+1);
System.out.println(store);
}
for(int i =0; i<decryupt.length();i++) {
c = decryupt.charAt(i);
c=(char)(c-i-1);
System.out.print(c);
}
}
}
TASK:03
3. Write a function named noDup() that takes a 2D array of size 4x5 and a 1D array of
size 20. It should then copy all the elements of 2D array into 1D array but should avoid
duplication.
package lab1.Task;

public class ArrayDuplicateRemover {

public static void main(String[] args) {


int[][] twoDArray = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 7, 13, 14, 15},
{16, 17, 18, 19, 20}
};

int[] oneDArray = new int[20];

noDup(twoDArray, oneDArray);

for (int i = 0; i < oneDArray.length; i++) {


int num = oneDArray[i];
if (num != 0) {
System.out.print(num + " ");
}
}
}

public static void noDup(int[][] source, int[] target) {


boolean[] seen = new boolean[21]; // Since elements are 1 to 20

for (int row = 0; row < source.length; row++) {


for (int col = 0; col < source[row].length; col++) {
int num = source[row][col];
if (!seen[num]) {
target[row * source[row].length + col] = num;
seen[num] = true;
}
}
}
}
}

You might also like