0% found this document useful (0 votes)
25 views8 pages

LAB PROGRAMS Updated - 1.6.1 - 1.6.7

The document contains multiple C programming lab exercises focusing on various algorithms and data structures. Key tasks include calculating sums of positive and negative integers, checking for duplicates, summing subarrays, and implementing frequency sorting and binary search. Each program is structured with input/output operations and loops to process arrays based on specific requirements.
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)
25 views8 pages

LAB PROGRAMS Updated - 1.6.1 - 1.6.7

The document contains multiple C programming lab exercises focusing on various algorithms and data structures. Key tasks include calculating sums of positive and negative integers, checking for duplicates, summing subarrays, and implementing frequency sorting and binary search. Each program is structured with input/output operations and loops to process arrays based on specific requirements.
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/ 8

LAB PROGRAMS

1.6.1

#include<stdio.h>

void main(){

int n;

int a[10];

scanf("%d",&n);

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

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

int p=0;

int s=0;

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

if(a[i]>0){

p=p+a[i];

if(a[i]<0){

s=s+a[i];

printf("%d\n",p);

printf("%d\n",s);

}
1.6.2

#include<stdio.h>

void main(){

int n;

int a[10];

scanf("%d",&n);

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

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

int pos=0;

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

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

if(a[i]==a[j]){

pos=pos+1;

if(pos==2){

break;

if(pos==2){

printf("true\n");

break;

else{

pos=0;

if(pos==0){

printf("false\n");
}

1.6.3

#include<stdio.h>

int main(){

int m,n;

int sum = 0;

scanf("%d",&m);

scanf("%d",&n);

int arr[m][n];

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

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

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

int x1,y1,x2,y2;

scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

x1--;y1--;x2--;y2--;

for(int i=x1;i<=x2;i++){

for(int j=y1;j<=y2;j++){

sum = sum+arr[i][j];

printf("%d\n",sum);

1.6.4

#include<stdio.h>
void main(){

int n,a[10],p=1;

scanf("%d",&n);

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

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

int fear[n];

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

int x=0;

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

if(a[j]<a[i]){

x=j;

break;

if(x==0)

fear[i]=1;

else

fear[i]=x+1-i;

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

p*=fear[i];

printf("%d\n",p);

}
1.6.5

#include <stdio.h>

int main() {

int T;

scanf("%d", &T); // Read number of test cases

while (T--) {

int N;

scanf("%d", &N);

int A[N], result[N], totalSum = 0;

// Read array and compute total sum

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

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

totalSum += A[i];

// Compute the result array

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

result[i] = totalSum - A[i];

// Print the result array

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

printf("%d ", result[i]);

printf("\n");

}
return 0;

1.6.6.

#include <stdio.h>

#include <stdlib.h>

void FreqSorting(int a[], int N, int M)

int freq[M+1],firstOcc[M+1];

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

freq[i] = 0;

firstOcc[i] = -1;

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

freq[a[i]]++;

if(firstOcc[a[i]] == -1){

firstOcc[a[i]] = i;

for(int i = 0;i<N-1;i++){

for(int j = i+1;j<N;j++){

if(freq[a[i]]<freq[a[j]] || ( freq[a[i]] == freq[a[j]] && firstOcc[a[i]] >firstOcc[a[j]])){

int temp = a[i];

a[i] = a[j];

a[j] = temp;

}
for(int i = 0;i<N;i++){

printf("%d ",a[i]);

int main()

int N, M;

scanf("%d %d", &N, &M);

int a[N];

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

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

FreqSorting(a,N,M);

return 0;

1.6.7

//Type Content here...

#include <stdio.h>

// Function to perform binary search

int searchInsert(int arr[], int n, int target) {

int left = 0, right = n - 1;

// Perform binary search

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == target) {

return mid; // If target is found, return the index

} else if (arr[mid] < target) {


left = mid + 1; // Target is on the right side

} else {

right = mid - 1;

return left;

int main() {

int n;

scanf("%d", &n);

int arr[n], target;

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

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

scanf("%d", &target);

// Call the function and print the result

int index = searchInsert(arr, n, target);

printf("%d\n", index);

return 0;

You might also like