Pages

This blog is under construction

Saturday, January 9, 2021

Exercise 1.2

       1.           Which of the following are examples of the null set

                  (i)       Set of odd natural numbers divisible by 2

                  It is a null set because no odd number is divisible by 2

                (ii)       Set of even prime numbers

                     It is not a null set because 2 is an even prime number

              (iii)       { x : x is a natural numbers, x < 5 and x > 7 }

                  It is null set because a number cannot be simultaneously less than 5 and greater than 7.

              (iv)       { y : y is a point common to any two parallel lines}

                It is a null set because parallel lines do not intersect. Hence, they have no common point 

       2.           Which of the following sets are finite or infinite

             (i)       The set of months of a year

              It is a finite set because it has 12 elements.


 (ii)      {1, 2, 3, . . .}

     Infinite set as it has infinite number of natural numbers 

(iii)       {1, 2, 3, . . .99, 100}

It is a finite set because the numbers from 1 to 100 are finite in number

(iv)       The set of positive integers greater than 100

Infinite set because positive integers greater than 100 are infinite in number

(v)        The set of prime numbers less than 99

Finite set because prime numbers less than 99 are finite in number 

3.           State whether each of the following set is finite or infinite:

(i)       The set of lines which are parallel to the x-axis

         Infinite set because lines parallel to the x-axis are infinite in number.


(ii)       The set of letters in the English alphabet

                Finite set because it has 26 elements.


 (iii)       The set of numbers which are multiple of 5

          Infinite set because multiples of 5 are infinite in number


(iv)       The set of animals living on the earth

        Finite set because the number of animals living on the earth is finite   (although it is quite          a big number).


(v)        The set of circles passing through the origin (0,0)

      Infinite set because infinite number of circles can pass through   the origin.


4.           In the following, state whether A = B or not:

(i)       A = { a, b, c, d }      B = { d, c, b, a }  

       A = B


 (ii)   A = { 4, 8, 12, 16 }  B = { 8, 4, 16, 18}

      12 A but 12 B  hence A ≠ B

(iii)  A = {2, 4, 6, 8, 10}    B  =  { x : x is positive even integer and x £ 10}

        A = B

 

(iv)   A = { x : x  is a multiple of 10},          B  =   { 10, 15, 20, 25, 30, . . . }

       AB

 

5.           Are the following pair of sets equal ? Give reasons.

  (i)       A = {2, 3},    B = {x : x is solution of x2 + 5x + 6 = 0}

 The equation x2 + 5x + 6 = 0 can be solved as: x(x + 3) + 2(x + 3) = 0   (x + 2)(x + 3) = 0     x = –2 or x = –3             A = {2, 3}; B = {–2, –3} A ≠ B

          (ii)       A = { x : x is a letter in the word FOLLOW} B = { y : y is a letter in the word WOLF}

The order in which the elements of a set are listed is not significant.

  A = B

 

6.           From the sets given below, select equal sets :

A = { 2, 4, 8, 12}, B = { 1, 2, 3, 4},  C = { 4, 8, 12, 14},    D = { 3, 1, 4, 2}

E = {–1, 1},          F = { 0, a},           G = {1, –1},              H = { 0, 1}

 

Among the given sets, B = D and E = G


Friday, January 8, 2021

C Program to encrypt a string

 A simple encryption system uses a shifting process to hide a message. The value of the shift can be in the range 1 to 26. For example a shift of 20 means that A = U, B = V, C = W, . . .Y = S, Z = T. So, the shift wraps around after Z. Coding happens as follows: First an extra space is added to the end of the string. Spaces within the original text are replaced with QQ before the text is encrypted. Double Q (QQ) was selected because no English word ends in Q or contains QQ. Additionally the coded message is printed in blocks of six characters separated by spaces. The last block might not contain six characters. Write a program that takes the coded text (assume less than 100 characters), the shift value and prints the decoded original message. Your program must reject any non-valid value for shift and display an error message "INVALID SHIFT VALUE". Assume all characters are upper case. 

Here are some sample inputs and outputs:

INPUT: CODED TEXT: "UHINBY LKKQCH HYLKK", SHIFT: 20

OUTPUT DECODED TEXT: "ANOTHER WINNER"

INPUT: CODED TEXT: "DKSMMW NAMMUK QMM", SHIFT: 29

OUTPUT: INVALID SHIFT VALUE.

Solution: 

#include <stdio.h>

#include <ctype.h>

#include <stdlib.h>

/*Assumes words do not end in Q and QQ is not part of any word. */

int main(void) {

  char s[100];

  int len=0, shift;

  char c;

  printf("Give the coded string (<=100 chars) then ENTER = ");

  c=toupper(getchar());

  while (!iscntrl(c) && c!=EOF && len<101) {

    if (c==' ');//skip space. This is the best time to remove it.

    else {

      if (c<'A' || c>'Z') {printf("Invalid char in input\n"); exit(0);}

      s[len++]=c;

    }

    c=toupper(getchar());

  }

  printf("Give the shift (1 to 26) then ENTER = ");

  scanf("%d",&shift);

  if (shift<1 || shift>26){printf("INVALID SHIFT VALUE\n"); exit(0);}

  //First decode using shift

  for(int i=0; i<len; i++){

    int ci=(int)s[i]-((int)'A'+shift);//ASCII of 'A' is the base

    s[i]=(ci<0)?(26+ci+(int)'A'):(char)(ci+(int)'A');

  }

  //Replace QQ by blank

  int i=0;

  while (i<len) {

    if (s[i]=='Q' && s[i+1]=='Q') {putchar(' '); i+=2;}

    else putchar(s[i++]);

  }

  printf("\n");

  return 0;

}

Output:

Give the coded string (<=100 chars) then ENTER = UHINBY LKKQCH HYLKK

Give the shift (1 to 26) then ENTER = 20

ANOTHER WINNER


Press any key to continue . . .

C Program for Scrabble

Scrabble is a word game that many of you may have played. Two or more players can play it. Players make words using letter tiles available with them and those already on the board. Each word has a score based on the value of each letter and the squares covered by the word. In this problem you have to write a program that reads a word and calculates the score of the word using the letter values. Use the following values for the letters.

Value Letters with value

1 All vowels, N, T, L, R, S

2 D, G

3 B, C, M, P

4 F, H, V, W, Y

5 K

8 J, X

10 Q, Z

Your program should work for both lower case and upper case letters in the input word. If the input  contains any non-alphabetic characters then the program should print "Illegal input". Hint: You can use one of the standard library functions to convert all lower case letters to upper case.

If input is Quiz then the program should print 22. If the input is What is the program should print Illegal input since the input contains a space which is not alphabetic.

Solution:

#include <stdio.h>

#include <ctype.h>

#include <stdlib.h>

/*The program does not check if the input word is a legal English word */

int letterValue(char c) {

  switch (c) {

    case 'A':case 'E':case 'I':case 'O':case 'U':case 'N':case 'T':case 'L':

    case 'R':case 'S': {return 1;}

    case 'D':case 'G': {return 2;}

    case 'B':case 'C':case 'M':case 'P': {return 3;}

    case 'F':case 'H':case 'V':case 'W':case 'Y': {return 4;}

    case 'K': {return 5;}

    case 'J':case 'X': {return 8;}

    case 'Q':case 'Z': {return 10;}

    default: {printf("Illegal input\n"); exit(0);}

  }

}

int main(void) {

  char s[50];

  int len=0, score=0;

  char c;

  printf("Give the word (<50 chars) then ENTER = ");

  c=getchar();

  while (!iscntrl(c) && c!=EOF && len<51) {

    s[len++]=c;

    c=getchar();

  }

  for (int i=0; i<len; i++)

    score+=letterValue(toupper(s[i]));

  if (len<1) {printf("Null input\n"); return 0;}

  printf("Score for ");

  for (int i=0; i<len; i++) putchar(s[i]);

  printf(" is %d\n", score);

  return 0;

}

Output:

Give the word (<50 chars) then ENTER = Quiz

Score for Quiz is 22


Press any key to continue . . .

Thursday, January 7, 2021

Test

Exercise 1.1

1.           Which of the following are sets ? Justify.

          i) The collection of all the months of a year beginning with the letter J.

we can easily identify a month beginning with the letter J so this is a well defined collection of objects Hence, this collection is a set.

ii) The collection of ten most talented writers of India.

It is not a well-defined collection because the criteria for determining a talent may vary from person to person. Hence, this collection is not a set.           

     iii) Team of eleven best-cricket batsmen of the world.

 Criteria for determining a batsman’s talent may vary from person to person. Hence, this collection is not a set.

     iv) The collection of all boys in your class.

 The collection of all boys in your class is a well-defined collection because you can easily identify  a boy who belongs to this collection. Hence, Collection is a set.

     v) The collection of all natural numbers less than 100.

it is a well-defined collection because one can definitely identify a number that belongs to this collection. Hence, this collection is a set.

 vi)A collection of novels written by the writer Munshi Prem Chand.

           A collection of novels written by the writer Munshi Prem Chand is a well-defined collection and easily identify a book that belongs to this collection. Hence, this collection is a set.

           vii)The collection of all even integers.

 The collection of all even integers is a well-defined collection because one can definitely identify an even integer that belongs to this collection. Hence, this collection is a set.

viii)The collection of questions in this Chapter.

The collection of questions in this chapter is a well-defined collection because one can definitely identify a question that belongs to this chapter. Hence, this collection is a set.

  ix) A collection of most dangerous animals of the world.

It is not a well-defined collection because the criteria for determining the dangerousness of an animal can vary from animal to animal. Hence, this collection is not a set.

2.           Let A = {1, 2, 3, 4, 5, 6}. Insert the appropriate symbo or  in the blank spaces:

(i)     5. . .A                  (ii)    8 . . . A                  (iii) 0. . .A

 (iv)    4. . . A                 (v)   2. . .A                    (vi) 10. . .A


          (i) 5 A  (ii) 8 A  (iii) 0 A (iv) 4 A (v) 2 A  (vi) 10 A


3.           Write the following sets in roster form:

(i)    A = {x : x is an integer and –3 < x < 7}


             A = {–2, –1, 0, 1, 2, 3, 4, 5, 6}

        (ii)   B = {x : x is a natural number less than 6}

            B = {1, 2, 3, 4, 5}

        (iii)          C = {x : x is a two-digit natural number such that the sum of its digits is 8}

              C = {17, 26, 35, 44, 53, 62, 71, 80}

       (iv)    D = {x : x is a prime number which is divisor of 60}

            D = {2, 3, 5}

        (v)        E = The set of all letters in the word TRIGONOMETRY

            E = {T, R, I, G, O, N, M, E, Y}

       (vi)       F = The set of all letters in the word BETTER

        F = {B, E, T, R}

4.           Write the following sets in the set-builder form :

(i)   (3, 6, 9, 12}            (ii)   {2,4,8,16,32}              (iii) {5, 25, 125, 625}

(iv)    {2, 4, 6, . . .}         (v) {1,4,9, . . .,100}


1)    {x: x = 3n, nN and 1 ≤ n ≤ 4}

2)    {x: x = 2n, nN and 1 ≤ n ≤ 5}

3)    {x: x = 5n, nN and 1 ≤ n ≤ 4}

4)    {x: x is an even natural number}

5)    {x: x = n2, nN and 1 ≤ n ≤ 10}


5.           List all the elements of the following sets :


(i)       A = {x : x is an odd natural number}

{1, 3, 5, 7, 9 …}           

                                                         


(ii)       B = {x : x is an integer, -1/2 < x < 4/2 }

Here x is a integer lies between -.5 and 4.5 hence B={0,1,2,3,4}


(iii)       C = {x : x is an integer, x2 £ 4}

C = {–2, –1, 0, 1, 2}


(iv)       D = {x : x is a letter in the word “LOYAL”}

{L, O, Y, A}


(v)        E = {x : x is a month of a year not having 31 days}

 E= {February, April, June, September, November}


(vi)       F = {x : x is a consonant in the English alphabet which precedes k }.

 {b, c, d, f, g, h, j}


6.           Match each of the set on the left in the roster form with the same set on the right described in set-builder form:

(i)       {1, 2, 3, 6}            (a) {x : x is a prime number and a divisor of 6}

(ii)       {2, 3}                    (b) {x : x is an odd natural number less than 10}

(iii)       {M,A,T,H,E,I,C,S} (c) {x : x is natural number and divisor of 6}

(iv)       {1, 3, 5, 7, 9}         (d) {x : x is a letter of the word MATHEMATICS}.