Chandan 20238005 Assign 6
Chandan 20238005 Assign 6
20238005
ECM branch
Assignment 06
Q1. Given an array of integers, find the first repeating element in it. We
need to find the element that occurs more than once and whose index of
first occurrence is smallest.
Code: #include<stdio.h>
int main(){
int num;
printf("Enter the number of elements in array: ");
scanf("%d",&num);
int a,b=__INT16_MAX__;
int arr[num];
for(int i=0;i<num;i++){
printf("Enter the %d index element: ",i+1);
scanf("%d",&arr[i]);
for(int j=0;j<i;j++){
if(arr[i]==arr[j] && j<b){
a=arr[i];
b=j;
}
}
}
for(int i=0;i<num;i++){
printf("%d ",arr[i]);
}
printf("\nThe repeating element is %d and it first occured at %d
position.",a,b+1);
}
Q2. Write a C program that traverses a doubly linked list of positive &
negative integers and deletes all nodes whose keys are negative.
Code:
#include<stdio.h>
#include<stdlib.h>
struct Node{
int val;
struct Node* prev;
struct Node* next;
};
i++;
}
return head;
}
Q3. You are given an unsorted array with both positive and negative
elements. You have to find the smallest positive number missing from the
array. Examples
CODE:
#include<stdio.h>
int main(){
printf("Enter the Number of element in array: ");
int num;
scanf("%d",&num);
int arr[num];
int i,max=1;
for(i=0;i<num;i++){
scanf("%d",&arr[i]);
int k=i-1;
int l=arr[i];
while(arr[k]>l&&k>=0){
arr[k+1]=arr[k];
k--;
}
arr[k+1]=l;
}
for(int i=0;i<num;i++) printf("%d ",arr[i]);
i=0;
while(arr[i]<=0&&i<num) i++;
if(i==num){
printf("%d",1);
return 0;
}
int k=1;
while(i<num){
if(k<arr[i]){
printf("\n The smallest positive missing number is %d.",k);
return 0;
}
else if(k==arr[i]){
k++;
i++;
}
else{
i++;
}
}
if(i==num){
printf("\n The smallest positive missing number is %d.",k);
}
return 0;
}
Q4. Write a C program to create a linked list of size at least 5 and
increment the data part of every node present in a linked list by 100.
Display the data both before and after increment.
Code: #include<stdio.h>
#include<stdlib.h>
struct Node{
int val;
struct Node* next;
};