Introduction to programming in C Week 4
Course Name: Introduction to programming in C
Course Link: Click Here
These are Introduction to programming in C Assignment 4 Answers
Question 1
Description
Given two arrays of positive integers, output the largest number in the first array not present in the second one.
Input
The first line contains the size of the first array.
The second line contains the contents of first array.
The third line contains the size of the second array.
The fourth line contains the contents of second array.
Output
Output the smallest number occurring in the first array that does not occur in the second.
In case there is no such number, output 0.
Note : The sizes of the arrays are smaller than 20.
Example
Input:
3
2 3 4
4
1 4 5 2
Output: 3
Code:-
#include<stdio.h>
#define size 20
int main()
{
int n,m,a[size],b[size];
int i,j,result=0,f=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
f=0;
for(j=0;j<m;j++)
{
if(a[i]==b[j])
{
f=1;
break;
}
}
if(!f)
{
if(result==0 || a[i]>result)
{
result=a[i];
}
}
}
printf("%d", result);
return 0;
}
These are Introduction to programming in C Assignment 4 Answers
Question 2
Given a sequence of integers, find the number of distinct numbers in the sequence. The sequence need not be sorted.
Input
The input consists of two lines.
The first line consists of a positive number N (N is at most 1000).
The second line consists of N integers separated by spaces.
Output
The number of distinct elements in the sequence.
Code:-
#include<stdio.h>
#define size 1001
int main()
{
int n,i,j,k, a[size],b[size],c=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[i]=0;
}
for(i=0;i<n;i++)
{
if(b[i]==0)
{
c++;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
b[j]=1;
}
}
}
}
printf("%d",c);
return 0;
}
These are Introduction to programming in C Assignment 4 Answers
Question 3
Write a program that replaces the occurrence 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.
Code:-
#include<stdio.h>
#include<string.h>
void replace_occurence(char *ps , char c,char *s){
int len_ps = strlen(ps);
int len_s = strlen(s);
int i,j;
for(i=0; i < len_ps; i++){
if (ps[i] == c){
for(j= len_ps; j > i; j--){
ps[j +len_s - 1] = ps[j];
}
for (j=0; j<len_s;j++){
ps[i+j]= s[j];
}
i += len_s -1;
len_ps += len_s -1;
}
}
}
int main(){
char ps[110];
char c;
char s[20];
scanf("%s" , ps);
scanf(" %c", &c);
scanf("%s" , s);
replace_occurence(ps ,c ,s);
printf("%s" , ps);
return 0 ;
}
These are Introduction to programming in C Assignment 4 Answers
More Weeks of Introduction to programming in C: Click Here
More Nptel courses: https://progiez.com/nptel
These are Introduction to programming in C Assignment 4 Answers

This content is uploaded for study, general information, and reference purpose only.