0% found this document useful (0 votes)
31 views14 pages

2005063

The documents contain code snippets for C/C++ programs that demonstrate different array operations and sorting algorithms like segregating arrays, sorting in two ways based on parity, finding the kth smallest element, replacing elements with next greatest elements, multiplying consecutive elements, and arranging rows and columns of a matrix.

Uploaded by

Swastik Agarwal
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)
31 views14 pages

2005063

The documents contain code snippets for C/C++ programs that demonstrate different array operations and sorting algorithms like segregating arrays, sorting in two ways based on parity, finding the kth smallest element, replacing elements with next greatest elements, multiplying consecutive elements, and arranging rows and columns of a matrix.

Uploaded by

Swastik Agarwal
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/ 14

Q1

#include<stdio.h>

#include<stdlib.h>

int main()

int *p,n,i;

printf("How many numbers you want to enter: ");

scanf("%d",&n);

p=(int*)malloc(n * sizeof(int));

printf("\nEnter %d Numbers:\n\n",n);

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

scanf("%d",p+i);

printf("\nArray in Reverse Order: \n\n");

for(i=n-1;i>=0;i--)

printf(" %d",*(p+i));

return 0;

Q2

#include<iostream>

using namespace std;

/* prints element and NGE pair

for all elements of arr[] of size n */

void printNGE(int arr[], int n)

int next, i, j;

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


{

next = -1;

for (j = i + 1; j < n; j++)

if (arr[i] < arr[j])

next = arr[j];

break;

cout << arr[i] << " -- "

<< next << endl;

int main()

int arr[] = {11, 13, 21, 3};

int n = sizeof(arr)/sizeof(arr[0]);

printNGE(arr, n);

return 0;

Q1

#include <bits/stdc++.h>

using namespace std;

/* Function to replace every element with the

next greatest element */


void nextGreatest(int arr[], int size)

// Initialize the next greatest element

int max_from_right = arr[size-1];

// The next greatest element for the rightmost element

// is always -1

arr[size-1] = -1;

// Replace all other elements with the next greatest

for(int i = size-2; i >= 0; i--)

// Store the current element (needed later for updating

// the next greatest element)

int temp = arr[i];

// Replace current element with the next greatest

arr[i] = max_from_right;

// Update the greatest element, if needed

if(max_from_right < temp)

max_from_right = temp;

/* A utility Function that prints an array */

void printArray(int arr[], int size)

int i;

for (i = 0; i < size; i++)


cout << arr[i] << " ";

cout << endl;

/* Driver program to test above function */

int main()

int arr[] = {16, 17, 4, 3, 5, 2};

int size = sizeof(arr)/sizeof(arr[0]);

nextGreatest (arr, size);

cout << "The modified array is: \n";

printArray (arr, size);

return (0);

Q2

#include<iostream>

using namespace std;

void modify(int arr[], int n)

// Nothing to do when array size is 1

if (n <= 1)

return;

// store current value of arr[0] and update it

int prev = arr[0];

arr[0] = arr[0] * arr[1];

// Update rest of the array elements

for (int i=1; i<n-1; i++)


{

// Store current value of next interaction

int curr = arr[i];

// Update current value using previous value

arr[i] = prev * arr[i+1];

// Update previous value

prev = curr;

// Update last array element

arr[n-1] = prev * arr[n-1];

// Driver program

int main()

{int arr[] = {2, 3, 4, 5, 6};

int n = sizeof(arr)/sizeof(arr[0]);

modify(arr, n);

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

cout << arr[i] << " ";

return 0;

Q3

#include <stdio.h>

void main()

static int array1[10][10], array2[10][10];


int i, j, k, a, m, n;

printf("Enter the order of the matrix \n");

scanf("%d %d", &m, &n);

printf("Enter co-efficients of the matrix \n");

for (i = 0; i < m; ++i)

for (j = 0; j < n; ++j)

scanf("%d", &array1[i][j]);

array2[i][j] = array1[i][j];

printf("The given matrix is \n");

for (i = 0; i < m; ++i)

for (j = 0; j < n; ++j)

printf(" %d", array1[i][j]);

printf("\n");

printf("After arranging rows in ascending order\n");

for (i = 0; i < m; ++i)

for (j = 0; j < n; ++j)

for (k =(j + 1); k < n; ++k)

if (array1[i][j] > array1[i][k])

{
a = array1[i][j];

array1[i][j] = array1[i][k];

array1[i][k] = a;

for (i = 0; i < m; ++i)

for (j = 0; j < n; ++j)

printf(" %d", array1[i][j]);

printf("\n");

printf("After arranging the columns in descending order \n");

for (j = 0; j < n; ++j)

for (i = 0; i < m; ++i)

for (k = i + 1; k < m; ++k)

if (array2[i][j] < array2[k][j])

a = array2[i][j];

array2[i][j] = array2[k][j];

array2[k][j] = a;

}
for (i = 0; i < m; ++i)

for (j = 0; j < n; ++j)

printf(" %d", array2[i][j]);

printf("\n");

Q3

#include <algorithm>

#include <iostream>

using namespace std;

// Function to return k'th smallest element in a given array

int kthSmallest(int arr[], int n, int k)

// Sort the given array

sort(arr, arr + n);

// Return k'th element in the sorted array

return arr[k - 1];

int main()

int arr[] = { 12, 3, 5, 7, 19 };

int n = sizeof(arr) / sizeof(arr[0]), k = 2;

cout << "K'th smallest element is " << kthSmallest(arr, n, k);

return 0;

}
Q4

#include <algorithm>

#include <iostream>

using namespace std;

// Function to return k'th smallest element in a given array

int kthSmallest(int arr[], int n, int k)

// Sort the given array

sort(arr, arr + n);

// Return k'th element in the sorted array

return arr[k - 1];

// Driver program to test above methods

int main()

int arr[] = { 12, 3, 5, 7, 19 };

int n = sizeof(arr) / sizeof(arr[0]), k = 2;

cout << "K'th smallest element is " << kthSmallest(arr, n, k);

return 0;

Q5

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

#include <ctype.h>
int main()

int arrayNum[15];

int a;

int max=0;

int location;

for( a=0; a < 15; a++)

printf("Enter element %d:", a);

scanf("%d",&arrayNum[a]);

for(a=0; a < 15; a++)

printf("%d\n", arrayNum[a]);

for (a = 1; a < 15; a++)

if (arrayNum[a] > max)

max = arrayNum[a];

location = a+1;

printf("Max element in the array in the location %d and its value %d\n", location, max);
for(a=0; a<15; a++)

if(arrayNum[a+1] == arrayNum[a])

continue;

else

printf("Number %d: %d occurences\n", arrayNum[a]);

return 0;

Q6

#include <bits/stdc++.h>

using namespace std;

// Function to segregate 0s and 1s

void segregate0and1(int arr[], int n)

int count = 0; // Counts the no of zeros in arr

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

if (arr[i] == 0)

count++;

// Loop fills the arr with 0 until count

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

arr[i] = 0;
// Loop fills remaining arr space with 1

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

arr[i] = 1;

// Function to print segregated array

void print(int arr[], int n)

cout << "Array after segregation is ";

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

cout << arr[i] << " ";

// Driver function

int main()

int arr[] = { 0, 1, 0, 1, 1, 1 };

int n = sizeof(arr) / sizeof(arr[0]);

segregate0and1(arr, n);

print(arr, n);

return 0;

Q7#include <bits/stdc++.h>

using namespace std;

// To do two way sort. First sort even numbers in

// ascending order, then odd numbers in descending


// order.

void twoWaySort(int arr[], int n)

// Current indexes from left and right

int l = 0, r = n - 1;

// Count of odd numbers

int k = 0;

while (l < r)

// Find first even number

// from left side.

while (arr[l] % 2 != 0)

l++;

k++;

// Find first odd number

// from right side.

while (arr[r] % 2 == 0 && l < r)

r--;

// Swap even number present on left and odd

// number right.

if (l < r)

swap(arr[l], arr[r]);

}
// Sort odd number in descending order

sort(arr, arr + k, greater<int>());

// Sort even number in ascending order

sort(arr + k, arr + n);

// Driver code

int main()

int arr[] = { 1, 3, 2, 7, 5, 4 };

int n = sizeof(arr) / sizeof(int);

twoWaySort(arr, n);

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

cout << arr[i] << " ";

return 0;

You might also like