0% found this document useful (0 votes)
19 views12 pages

Week - 9

The document contains various C programming code snippets that demonstrate algorithms for sorting, searching, and manipulating arrays. Key functionalities include sorting arrays by squares, checking for arithmetic progression, and finding two numbers that sum to a target. Additionally, it includes examples of binary search and counting bits in integers.

Uploaded by

mondalbabai8900
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views12 pages

Week - 9

The document contains various C programming code snippets that demonstrate algorithms for sorting, searching, and manipulating arrays. Key functionalities include sorting arrays by squares, checking for arithmetic progression, and finding two numbers that sum to a target. Additionally, it includes examples of binary search and counting bits in integers.

Uploaded by

mondalbabai8900
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

In-Lab – 1 (page No 114)

int* sortedSquares(int* nums, int numsSize, int* returnSize)


{
int* result = (int*)malloc(numsSize * sizeof(int));
*returnSize = numsSize;
int left = 0;
int right = numsSize-1;
int index = numsSize-1;
while (left <= right)
{
int leftSquare = nums[left]*nums[left];
int rightSquare = nums[right]*nums[right];

if (leftSquare > rightSquare)


{
result[index] = leftSquare;
left++;
}
else
{
result[index] = rightSquare;
right--;
}
index--;
}
return result;
}

In-Lab – 2 (page No 115)

#include <stdio.h>

int main()
{
int n,x;
scanf("%d %d",&n,&x);
int a[n],flag=0,i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]==x)
{
flag=1;
break;
}
}
if(flag==1)
printf("YES");
else
printf("NO");

return 0;
}

In Lab – 3 (page No 116)

int search(int* nums, int numsSize, int target)


{
int mid,low,high;
low=0;
high = numsSize-1;
while(low <= high)
{
mid = low + (high - low)/2;
if(nums[mid] == target)
{
return mid;
}
else if(nums[mid] > target)
{
high = mid-1;
}
else
{
low = mid + 1;
}
}
return -1;
}
Post Lab – 1 (page No 117)

void sort(int* arr , int arrSize )


{
int temp;
for(int i = 0 ; i < arrSize ; i++)
{
for( int j = 0 ; j < arrSize - 1 - i ; j++)
{
if( arr[j] > arr[j+1] )
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
bool canMakeArithmeticProgression(int* arr, int arrSize)
{
if( arrSize == 0)
return false;
if( arrSize == 1)
return true;
sort(arr,arrSize);
int diff = arr[1] - arr[0];
for(int i = 1 ; i < arrSize ; i++)
{
if( ( arr[i] - arr[i-1] ) == diff )
continue;
else
return false;
}
return true;
}

Post Lab – 2 (page No 118)

int* twoSum(int* nums, int numsSize, int target, int* returnSize)


{
int i, j, sum = 0;
int* result = malloc(2 * sizeof(int));
for (i = 0; i < numsSize; i++)
{
for (j = i + 1; j < numsSize; j++)
{
sum = nums[i] + nums[j];
if (sum == target)
{
result[0] = i;
result[1] = j;
*returnSize = 2;
return result;
}
}
}
*returnSize = 0;
free(result);
return NULL;
}

Post Lab – 3 (page No 119)

#include <stdio.h>
#include <stdlib.h>
int calculateHours(int* piles, int n, int k)
{
int totalHours = 0;
for (int i = 0; i < n; i++)
{
totalHours += (piles[i] + k - 1) / k;
}
return totalHours;
}
int minEatingSpeed(int* piles, int n, int h)
{
int low = 1, high = 0;
for (int i = 0; i < n; i++)
{
if (piles[i] > high)
{
high = piles[i];
}
}
int result = high;
while (low <= high)
{
int mid = low + (high - low) / 2;
int hoursNeeded = calculateHours(piles, n, mid);
if (hoursNeeded <= h)
{
result = mid;
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return result;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, h;
scanf("%d %d", &n, &h);
int piles[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &piles[i]);
}
int minK = minEatingSpeed(piles, n, h);
printf("%d\n", minK);
}
return 0;
}
Skill – 1 (page No 120)

int countBits(int x)
{
int count = 0;
while (x > 0)
{
count = count + (x & 1);
x = x >> 1;
}
return count;
}
int compare(const void* a, const void* b)
{
int x = *(int*)a;
int y = *(int*)b;
int bitCountX = countBits(x);
int bitCountY = countBits(y);
if (bitCountX != bitCountY)
{
return bitCountX - bitCountY;
}
return x - y;
}
void sort(int* arr , int arrSize )
{
int temp;
for(int i = 0 ; i < arrSize ; i++)
{
for( int j = 0 ; j < arrSize - 1 - i ; j++)
{
int j1 = countBits(arr[j]);
int j2 = countBits(arr[j + 1]);
if ( j1 > j2 || ( j1 == j2 && arr[j] > arr[j + 1]))
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int* sortByBits(int* arr, int arrSize, int* returnSize)
{
sort(arr,arrSize);
*returnSize = arrSize;
return arr;
}

Skill – 2 (page No 121)

void sort(int* arr , int arrSize )


{
int temp;
for(int i = 0 ; i < arrSize ; i++)
{
for( int j = 0 ; j < arrSize - 1 - i ; j++)
{
if( arr[j] > arr[j+1] )
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int* targetIndices(int* nums, int numsSize, int target, int* returnSize)
{
sort(nums, numsSize);
int* result = (int*)malloc(numsSize * sizeof(int));
int index = 0;
for (int i = 0; i < numsSize; i++)
{
if (nums[i] == target)
{
result[index++] = i;
}
}
*returnSize = index;
if (index == 0)
{
free(result);
result = NULL;
}
return result;
}

Skill Lab – 3 (page No 122)

#include <stdio.h>
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
int a[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
int max1=a[0],max2=-1;
for (int i = 0; i < n; i++)
{
if(a[i] > max1)
{
max1=a[i];
}
}
for(int i=0;i<n;i++)
{
if(a[i] > max2 && a[i] < max1)
{
max2=a[i];
}
}
printf("%d\n", max1 + max2);
}
return 0;
}
Skill Lab – 4 (page No 123)

#include <stdio.h>
#include <stdlib.h>

int main()
{
int num, *arr, i;
scanf("%d", &num);
arr = (int*) malloc(num * sizeof(int));
for(i = 0; i < num; i++)
{
scanf("%d", arr + i);
}
for(i = 0; i < num / 2; i++)
{
int temp = arr[i]; // Store the current element
arr[i] = arr[num - i - 1]; // Swap with the corresponding element from the end
arr[num - i - 1] = temp; // Place the stored element in the new position
}
for(i = 0; i < num; i++)
printf("%d ", *(arr + i));
return 0;
}

Skill Lab – 5 (page No 124)

#include <stdio.h>
int main()
{
int size;
printf("Enter array size:");
scanf("%d",&size);
int a[size],i,j;
printf("Enter array elements ");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
int temp;
for(i=0;i<size-1;i++)
{
for(j=0;j<size -1 – i ;j++)
{
if(a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting array elements are:\n");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
return 0;
}

Skill Lab – 6 – 1 (page No 125)

#include <stdio.h>
int main()
{
int size;
printf("Enter array size:");
scanf("%d",&size);
int a[size],ele,i,flag=0;
printf("Enter array elements ");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("Enter search element");
scanf("%d",&ele);
for(i=0;i<size;i++)
{
if(a[i] == ele)
{
flag=1;
break;
}
}
if(flag == 1)
printf("%d is found at %d in array ",ele,(i+1));
else
printf("%d is not found in array ",ele);
return 0;
}

Skill Lab – 6 – 2 (page No 126)

#include <stdio.h>
int main()
{
int size;
printf("Enter array size:");
scanf("%d",&size);
int a[size],ele,i,flag=0,mid,left,right;
printf("Enter array elements ");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("Enter search element");
scanf("%d",&ele);
left=0,right=size-1;
while(left <= right )
{
mid = left +(right - left)/2;
if(a[mid] == ele)
{
flag=1;
break;
}
else if( a[mid] < ele )
{
left = mid+1;
}
else if(a[mid] > ele)
{
right = mid-1;
}
}
if(flag == 1)
printf("%d is found at %d in array ",ele,(mid+1));
else
printf("%d is not found in array ",ele);
return 0;
}

You might also like