Introduction To Programming In C Week 4 Answers
Q1. Given two arrays of integers output the largest number in the first array not present in the second one.
Code:-
#include<stdio.h>
int main()
{
int n,m,i,j,c=0,temp,f=0;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&m);
int b[m];
for(i=0;i<m;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(i=n-1;i>=0;i--)
{
for(j=0;j<m;j++)
{
if(a[i]==b[j])
{
c++;
}
}
if(c==0)
{
printf("%d",a[i]);
f=1;
break;
}
else
{
c=0;
}
}
if(f==0)
{
printf("0");
}
return 0;
}
Q2. Write a program that replaces the occurence of a given character (say c) in a primary string (say PS) with another string (say s).
Code:-
#include<stdio.h>
int main()
{
char PS[1000], S[10], c;
char *ptr=PS, *ptr2=S;
int i=0;
scanf("%s",ptr);
getchar();
scanf("%c",&c);
scanf("%s",ptr2);
for( i=0;ptr[i]!='\0';i++)
{
if(ptr[i]==c)
{
printf("%s",ptr2);
}
else
printf("%c",ptr[i]);
}
return 0;
}
Q3. Given a threshold floating point number and an array of floating point numbers strictly between 0 and 1. Modify the array with the following rules: If the number is greater than threshold value, change it to 1 and if less than or equal to threshold value, change to 0.
Code:-
#include<stdio.h>
int main()
{
float farray[20];
float tvalue;
int sum;
int n;
scanf("%f", &tvalue);
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%f", &farray[i]);
}
for (int i = 0; i < n; i++)
{
if(farray[i] > tvalue)
{
sum = sum + 1;
}
}
printf("%d", sum);
return 0;
}