# Introduction to Programming in C Week 2

Session: JAN-APR 2024

Course Name: Introduction to Programming in C

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

Question 1

Parity Checker
You are given a sequence of bits (1’s and 0’s).
The sequence is said to have even parity if and only if the number of 1’s in the sequence if even.
Write a C program to that outputs 1 if the sequence has even parity and 0 otherwise.

Input
A sequence of bits (0’s and 1’s) ending with a -1.
(Note : -1 is not a part of input. It only signifies that input has ended)
Output
1 if the number of ones in the sequence is even.
0 if the number of ones in the sequence is odd.

Solution:

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

int main() {
int bit;
int onesCount = 0;

while (1) {
scanf("%d", &bit);

if (bit == -1) {
break;
}

if (bit == 0 || bit == 1) {
onesCount += bit;
} else {
return 1;
}
}

if (onesCount % 2 == 0) {
printf("1");
} else {
printf("0");
}

return 0;
}``````

These are Introduction to Programming in C Assignment 2 Answers

Question 2

Identify Distinct elements in a sorted sequence
You are given sequence of non-negative integers, sorted in the non decreasing order.
That is if the sequence is a1,a2,…,an,then  aiai+1 for all i from 1 to n-1.
You can assume that are at least two numbers in the sequence.
Write a C program to output the number of distinct elements in the sorted sequence.

Input
A non decreasing sorted sequence of non-negative integers, ending with a -1.
(Note : -1 is not a part of input. It only signifies that input has ended)
Output
The number of distinct elements in the sequence.

Solution:

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

int main() {
int prevNum = -1;
int currentNum;
int distinctCount = 0;

while (1) {
scanf("%d", &currentNum);

if (currentNum == -1) {
break;
}

if (currentNum != prevNum) {
distinctCount++;
}

prevNum = currentNum;
}

printf("%d", distinctCount);

return 0;
}
``````

These are Introduction to Programming in C Assignment 2 Answers

Question 3

Count the Number of 0’s Between the First and Last 1
You are given a binary sequence.
Write a C program to count the number of 0’s between the first and last 1 in the sequence.

Input
A sequence of bits (0’s and 1’s) ending with a -1.
(Note : -1 is not a part of input. It only signifies that input has ended)
Output
The number of 0’s Between the First and Last 1 in the sequence.

Note : Make no assumptions about the data in the sequence.
For instance if there is no starting and ending 1 ( say the sequence is all 0), you have to output 0.

Solution:

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

int main(){
int bit, count = 0, flag = 0, sum = 0;
scanf ("%d", &bit);
while (bit!= -1){
if (bit == 1){
flag = 1;
count+=sum;
sum = 0;
scanf ("%d", &bit);
}
if (flag)
if (bit == 0)
sum++;
scanf ("%d",&bit);
}
printf("%d",count);
return 0;
}``````

These are Introduction to Programming in C Assignment 2 Answers

Session: JAN-APR 2023

Course Name: Introduction to programming in C

These are Introduction to Programming in C Assignment 2 Answers

Question 1

#### You are given a non-negative sequence of numbers, ending with a -1. You can assume that there are at least two numbers before the ending -1.You have to output the second largest element of the sequence.If there is no second largest element in the sequence then output 0.Note : -1 is not a part of input. It only signifies that input has ended.

These are Introduction to Programming in C Assignment 2 Answers

Solution:

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

int second_largest(int n, int numbers[n])
{
int largest = numbers[0];
for (int i = 1; i < n; i++){
if (numbers[i] > largest){
largest = numbers[i];
}
}

int second_largest = 0;
for (int i = 0; i < n; i++) {
if (numbers[i] < largest && numbers[i] > second_largest) {
second_largest = numbers[i];
}
}

return second_largest;
}

int main() {
int numbers[100];
int n = 0;

while (1) {
int number;
scanf("%d", &number);
if (number == -1) {
break;
}
numbers[n++] = number;
}

int result = second_largest(n, numbers);
printf("%d", result);

return 0;
}``````

These are Introduction to Programming in C Assignment 2 Answers

Question 2

You are given a non decreasing sorted sequence of non negative integers, ending with -1.
That is if the sequence is a1,a2,…,an,−1 then ai≤ai+1 for all i from 1 to n-1.
You can assume that are at least two numbers before the ending -1.
You have to output the number of distinct elements in the sorted sequence.

Solution:

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

int main(){
int num, m, n, counter=0;
scanf("%d",&num);
while(num!=-1){
m=num;
scanf("%d",&num);
n=num;
if((m!=n) && (num!=-1))
counter++;
}
if(counter>=0)
printf("%d",counter+1);
else
printf("0");
return 0;
}``````

These are Introduction to programming in C Assignment 2 Answers

Question 3

In this assignment, you will be given an N×N matrix, with N>1
You have to determine whether the matrix is an upper triangular matrix.
A matrix is upper triangular if every entry below the diagonal is 0. The following is an example of an upper triangular matrix:
Note: The diagonal itself, and the entries above the diagonal can be zeroes or non-zero integers.
Input
First, you will be given N, which is the size of the matrix.
Then you will be given N rows of integers, where each row consists of N integers separated by spaces.
Output
If the input matrix is upper triangular, then print 1. Otherwise, print 0.

These are Introduction to Programming in C Assignment 2 Answers

Solution:

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

int main(){
int n,i,j,a,up=0,lp=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a);
if(i>j)
{
if(a!=0)
up=1;
}
else if(i<j)
{
if(a!=0)
lp=1;
}
}
}
if(up==0|lp==0)
printf("1");
else
printf("0");
return 0;
}``````

These are Introduction to Programming in C Assignment 2 Answers