Lab 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Lab – 1

Name-Rahul Churiwal
Roll No. -22052659
Sec-CSE_40

1.1 Aim of the program: Write a program to find out the second smallest and second
largest
element stored in an array of n integers.
Input: Size of the array is ‘n’ and read ‘n’ number of elements from a disc file.
Output: Second smallest, Second largest
#include <stdio.h>
#include <limits.h>
int main()
{
FILE *file;
int number;
file = fopen("numbers.txt", "r");
int count = 0;
while (fscanf(file, "%d", &number) != EOF)
{
count++;
printf("%d ", number);
}
fclose(file);
file = fopen("numbers.txt", "r");
int arr[count];
count = 0;
while (fscanf(file, "%d", &number) != EOF)
{
arr[count] = number;
count++;
}
// second smallest and second largest
int si, li, smallest = INT_MAX, largest = INT_MIN;
for (int i = 0; i < count; i++)
{
if (arr[i] < smallest)
{
smallest = arr[i];
si = i;
}

if (arr[i] > largest)


{
largest = arr[i];
li = i;
}
}
int ss = INT_MAX;
int sl = INT_MIN;
int ssi;
int sli;
for (int i = 0; i < count; i++)
{
if (arr[i] < ss && i != si)
{
ss = arr[i];
ssi = i;
}
if (arr[i] > sl && i != li)
{
sl = arr[i];
sli = i;
}
}
printf("\nSecond Largest = %d \nSecond Smallest = %d ", sl, ss);
}

Output:

1.2 Aim of the program: Given an array arr[] of size N, find the prefix sum of the
array. A prefix sum array is another array prefixSum[] of the same size, such that the
value of prefixSum[i] is arr[0] + arr[1] + arr[2] . . . arr[i].
Input Array: 3 4 5 1 2
Output Array: 3 7 12 13 15
#include <stdio.h>
int main()
{
printf("Enter the size of the Array : ");
int n;
scanf("%d", &n);
printf("\n");
int arr[n];
printf("Enter the elements \n");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int sumArr[n];
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
sumArr[i] = sum;
}
printf("Prefix Sum Array is : ");
for (int i = 0; i < n; i++)
{
printf("%d ", sumArr[i]);
}
}

Output:

1.3 Aim of the program: Write a program to read ‘n’ integers from a disc file that
must contain some duplicate values and store them into an array. Perform the
following operations on the array.
a) Find out the total number of duplicate elements.
b) Find out the most repeating element in the array.
Input:
Enter how many numbers you want to read from file: 15
Output:
The content of the array: 10 40 35 47 68 22 40 10 98 10 50 35 68 40 10
Total number of duplicate values = 4
The most repeating element in the array = 10
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
int main()
{
FILE *file;
int number;
file = fopen("numbers.txt", "r");
int count = 0;
while (fscanf(file, "%d", &number) != EOF)
{
count++;
printf("%d ", number);
}
fclose(file);
file = fopen("numbers.txt", "r");

int arr[count];
int n = count;
count = 0;
while (fscanf(file, "%d", &number) != EOF)
{
arr[count] = number;
count++;
}
bool operated[n];
for (int i = 0; i < n; i++)
{
operated[i] = false;
}
int totalduplicates = 0;
int freq = 0;
int mostelement;
int maxfreq = 0;
for (int i = 0; i < n; i++)
{
int element = arr[i];
operated[i] = true;
freq = 1;
for (int j = 0; j < n; j++)
{
if (element == arr[j] && !operated[j])
{
operated[j] = true;
// printf("duplicate found %d at %d \n" , arr[j],
j);
totalduplicates++;
freq++;
}
}
if (freq > maxfreq)
{
mostelement = element;
maxfreq = freq;
}
}
printf("\nTotal number of duplicates element = %d \n The most
repeating element is = %d ", totalduplicates, mostelement);
}

Output:

1.4 Aim of the program: Write a function to ROTATE_RIGHT(p1, p2 ) right an array


for first p2 elements by 1 position using EXCHANGE(p, q) function that
swaps/exchanges the numbers p & q. Parameter p1 be the starting address of the
array and p2 be the number of elements to be rotated.
Input:
Enter an array A of size N (9): 11 22 33 44 55 66 77 88 99
Call the function ROTATE_RIGHT(A, 5)
Output:
Before ROTATE: 11 22 33 44 55 66 77 88 99
After ROTATE: 55 11 22 33 44 66 77 88 99
#include <stdio.h>

void Exchange(int arr[], int n)


{
int dupArr[n];
dupArr[0] = arr[n - 1];
for (int i = 1; i < n; i++)
{
dupArr[i] = arr[i - 1];
}
int i;
for (i = 0; i < n; i++)
{
arr[i] = dupArr[i];
}
}
void swap(int a, int b, int arr[], int n)
{
int index1 = -1, index2 = -1;
for (int i = 0; i < n; i++)
{
if (arr[i] == a)
index1 = i;
if (arr[i] == b)
index2 = i;
}
if (index1 != -1 && index2 != -1)
{
int temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
else
{
printf("\nElements Not Found ");
}
}
int main()
{
printf("Enter the size of the Array : ");
int n;
scanf("%d", &n);
printf("\n");
int arr[n];
printf("Enter the elements \n");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
Exchange(arr, 4);
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
swap(9, 10, arr, n);
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
Output:

You might also like