# Introduction to programming in C Week 4

Session: JAN-APR 2024

Course Name: Introduction to Programming in C

#### These are Introduction to Programming in C Assignment 4 Answers

Question 1

Description
Given an array of positive integers, write a C Program to output the sum of the elements that are above the mean.
Given an array [a1,a2…an], the mean of the array is defined as μ=∑ni=1ain.
Input
The first line contains the size of the array.
The second line contains the contents of the array.
Output
Output the sum of elements in the array which are greater than or equal to the mean.
Note: The size of the array is always smaller than 20.

Solution:

``````#include <stdio.h>

int main() {
int size, i;
double sum = 0, mean;
scanf("%d", &size);
int arr[size];
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
mean = sum / size;
int sumAboveMean = 0;
for (i = 0; i < size; i++) {
if (arr[i] >= mean) {
sumAboveMean += arr[i];
}
}

printf("%d", sumAboveMean);

return 0;
}``````

These are Introduction to Programming in C Assignment 4 Answers

Question 2

Description
Given two arrays of positive integers, write a C Program to output the smallest number in the first array that is also 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 also occurs in the second.
In case there is no such number, output -1.
Note: The sizes of the arrays are smaller than 20.

Solution:

``````#include <stdio.h>

int main() {
int size1, size2, i, j;

scanf("%d", &size1);

int arr1[size1];
for (i = 0; i < size1; i++) {
scanf("%d", &arr1[i]);
}

scanf("%d", &size2);

int arr2[size2];
for (i = 0; i < size2; i++) {
scanf("%d", &arr2[i]);
}

int smallest = -1;
for (i = 0; i < size1; i++) {
for (j = 0; j < size2; j++) {
if (arr1[i] == arr2[j] && (smallest == -1 || arr1[i] < smallest)) {
smallest = arr1[i];
}
}
}

if (smallest != -1) {
printf("%d", smallest);
} else {
printf("-1");
}

return 0;
}``````

These are Introduction to Programming in C Assignment 4 Answers

Question 3

Anagram Checking
Given two strings(character arrays), write a C Program to check if one of them is an anagram of the other.
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.
Examples are LISTEN and SILENT , KNEE and KEEN.

Input
The first line contains the size of the character array.
The second line contains the contents of first array.
The third line contains the contents of the second array.
Note: The maximum size of the character array can be assumed to be 20.
The contents of both arrays are only upper case alphabets.
Output
1 If the contents of the arrays are anagrams
0 If the contents of the arrays are not anagrams
Note: The sizes of the both the arrays can be assumed to be the same.

Solution:

``````#include <stdio.h>
#include <string.h>

int areAnagrams(char str1[], char str2[]) {
int len1 = strlen(str1);
int len2 = strlen(str2);

if (len1 != len2) {
return 0;
}

for (int i = 0; i < len1 - 1; i++) {
for (int j = i + 1; j < len1; j++) {
if (str1[i] > str1[j]) {
char temp = str1[i];
str1[i] = str1[j];
str1[j] = temp;
}

if (str2[i] > str2[j]) {
char temp = str2[i];
str2[i] = str2[j];
str2[j] = temp;
}
}
}

for (int i = 0; i < len1; i++) {
if (str1[i] != str2[i]) {
return 0; // Not anagrams
}
}

return 1; // Anagrams
}

int main() {
int size;

scanf("%d", &size);

char str1[size];
scanf("%s", str1);

char str2[size];
scanf("%s", str2);

int result = areAnagrams(str1, str2);

if (result) {
printf("1"); // Anagrams
} else {
printf("0"); // Not anagrams
}

return 0;
}``````

These are Introduction to Programming in C Assignment 4 Answers

Session: JAN-APR 2023

Course Name: Introduction to Programming in C

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

These are Introduction to Programming in C Assignment 4 Answers

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.

These are Introduction to Programming in C Assignment 4 Answers

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