Week 4 Assignment
Week 4 Assignment
Input Specification:
The first line contains the size N1 of the first array.
Next line give the contents of the first array.
Next line contains the size N2 of the second array.
Next line give the contents of the second array.
Output Format:
Output must be a single number which is the smallest number occurring
in the first array that does not occur in the second. In case there is
no such number, output NO.
Variable Constraints:
The sizes of the arrays are smaller than 20.
Each array entry is an integer which fits an int data type.
Example:
Input:
3
234
4
1357
Output: 2
Input
1
1
2
12
Output: NO
Sol. #include<stdio.h>
#define MAX 20
int read_array(int arr[])
{
int i, n;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
return n;
}
Input Format:
First line contains the integer 'n' denoting the total number of ants s.t. 1 <= n <= 1,000
Second line contains 'n' space separated numbers (either '1' or '-1') denoting the initial
directions of the ants.
Output Format:
Output a single integer which is the index (lower index in case two ants are falling
simultaneously in the end) of the last ant to fall off the table.
Sol. #include <stdio.h>
int main(){
// Number of ants
int n;
scanf("%d", &n);
int a[n];
int i;
// Directions of ants
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
int left = 0, right = 0;
// Number of contiguous ants at left edge and moving towards left
for(i=0; i<n; i++){
if(a[i] == -1){
left ++;
}
else{
break;
}
}
for(i=n-1; i>=0; i--){
if(a[i] == 1){
right++;
}
else{
break;
}
}
// Number of ants between 'left' and 'right' position and moving towards left
int leftgoingants = 0;
for(i=left; i <= n-right-1; i++){
if(a[i] == -1){
leftgoingants ++;
}
}
if(left >= right){
printf("%d", left + leftgoingants);
}
else{
printf("%d", left + leftgoingants + 1);
}
return 0;
}
3. Write a program that replaces the occurence of a given character (say c)
in a primary string (say PS) with another string (say s).
Input:
The first line contains the primary string (PS)
The next line contains a character (c)
The next line contains a string (s)
Output:
Print the string PS with every occurence of c replaced by s.
NOTE:
- There are no whitespaces in PS or s.
- Maximum length of PS is 100.
- Maximum length of s is 10.
Sol. #include<stdio.h>
int length(char *s)
{
int i=0;
while(s[i]!='\0')
{
i++;
}
return i;
}
void Replace(char *str, char *sub, int start , int end)
{
int i=-1;
int len = length(str);
char temp[100];
do
{
i++;
temp[i]=str[i];
} while(i!=len);
int sublen=length(sub);
int j = start;
for(i=0;i<sublen;i++)
{
str[j++]=sub[i];
}
for(i=end+1;i<=len;i++)
{
str[j++]=temp[i];
}
}
int charCheck (char * str , char a ,int i )
{
if(str[i]==a)
return i;
else
return -1;
}
int main()
{
char str[100] = {0};
int i =-1,start=0,end=0;
scanf("%s",str);
char c1;
scanf("\n%c",&c1);
char subs1[15];
scanf("%s",subs1);
for(i=0;i<length(str);i++)
{
int val=charCheck(str,c1,i);
if(val>=0)
{
start=i;
end=val;
Replace(str,subs1,start,end);
i=start+length(subs1);
}
}
printf("%s",str);
return 0;
}