Showing posts with label XII tutorials computer science. Show all posts
Showing posts with label XII tutorials computer science. Show all posts

Television Class Program

#include<iostream>

using namespace std;

class tv
{

int channel,volume;
int on;
int get_channel()
{
channel=1;
return channel;
}

int get_vol()
{
volume=1;
return volume;
}

public:

tv()
{
channel=get_channel();
volume=get_vol();
cout<<"\n current channel is : "<<channel<<" and volume is : "<<volume;
}

void turn_on()
{
on=1;
}

void turn_off()
{
on=0;
}

void set_channel(int m)
{
channel=m;
cout<<"\n Current channel is"<<channel;
}

void set_volume (int n)
{
volume =n;
cout<<"\n current volume is"<<volume;
}

void channel_up()
{
channel+=1;
cout<<"\n current channel is"<<channel;
}

void channel_down()
{
channel-=1;
cout<<"\n current channel is"<<channel;
}

void vol_up()
{
volume+=1;
cout<<"\n current volume is"<<volume;
}

void vol_down()
{
volume-=1;
cout<<"\n current volume is"<<volume;
}
};

void main()
{
int x,y,z;
tv t1;

do
{
cout<<"do you want to make any changes (1/0)";
cin>>x;
if (x == 1)
{
cout<<"1. turn off \n 2.turn on \n 3. channel  up \n 4. channel down \n 5.set channel \n 6.volume up \n 7.volume down \n 8.set volume \n";
cout<<"enter the number ";
cin>>y;
if (y==1)
 {
 t1.turn_off();
 break;
 }

switch (y)
{
case 2:
t1.turn_on();
break;
case 3:
t1.channel_up();
break;
case 4:
t1.channeldown();
break;
case 5:
 {
 cout<<"\n enter the channel no (1-120)";
 cin>>z;
 t1.set_channel(z);
 }
break;
case 6:
t1.vol_up();
break;
case 7:
t1.vol_down();
break;
case 8:
{
 cout<<"\n enter the volume level(1-7)";
 cin>>z;
 t1.set_volume(z);}
 break;
 }
}
}
while(x== 1);
cout<<"thank you";
}

Data Structures : ARRAYS

Data Types : Data types refers to a group of data which share similar properties and have common behaviour among them.
There are two types of data :
  1. Primitive data type : These data types are those which are not composed of other data types. These are also known as standard or fundamental data types.
    Example : int, float, double, char.
  2. Non-Primitive data type : These data types are composed of primitive data types. These are also known as user defined data types.
    Examples : array, structure , class.
Data Structures : Data structures are very important in a computer system as these not only allow the user to combine various data types in a group but also allow processing of the group as a single unit thereby making things much simplier and easier.
Types of Data Structures :
  1. Simple Data structures : These data structures are built from primitive data types  like int , char , float.
    Example : array , structures.
  2. Compound Data structure : These data structures are formed of simple data structures by combining them into various forms.
    Example : stack , queue , linked list.(linear data structures)
Operations on data structures : The basic operations thatare performed on data structures are :
  1. Insertion : Insertion means addition of new data element in a data structure.
  2. Deletion : Removal of data element.
  3. Searching : Searching of a specified data element.
  4. Traversal : It means processing of all data elements.
  5. Sorting : Arranging of data elements.
  6. Merging : Combining elements of two similar data structures to form a new data structure..
Introduction to Arrays :
It refers to named list of definite number of similar data elements. These are referenced by a set of consecutive numbers like 0,1,2,3.....,n
Arrays can be one dimensional, two dimensional or multi dimensional.

Need of an array :
Arrays are needed because a no. of data values can be stored under a same name and these data elements are stored in sequence.

One Dimensional Array :
It is the simplest form of an array. In C++ , it is denoted as  array_name[size]
where size specifies the number of elements in the array and the subscript value or index number ranges from 0 to (size-1).

Implementation of 1-D Array in memory :
In 1-D arrays data is stored in sequence in a single row or single column.
Address of element with index I= Base address + size of an array element(I- Lower bound of array)
arr[I]=B+w(I-L)

Searching :
Searching of a specific element in the array is known as searching.

Linear Search :
In this type, each element of the array is compared with given item to be searched for, one by one. This method which traverses the array sequentially to locate the given item is called linear search or sequential search.

UDF for linear search :
int linear_search(int ar[], int size, int item )
{
for(int i=0; i<size; i++)
{
if (ar[i]==item)
return i;                          //returns index of item if it is present in the list
}
return -1;;                      // if item is not present in the list
}


Binary search :
the technique of searching a specified element in the sorted array list by dividing the list into two parts and searching only in one half depending on the element to be greater than or less than the middle value.

UDF for binary search :
int binary_search(int ar[], int size , int item)
{
int beg, last, mid;
beg=0;
last= size-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(item==ar[mid])
return mid;
else if(item>ar[mid])
beg=mid+1;
else
last=mid-1;
}
return -1;           // will run only if item is not present
}



Sorting :
Arranging of data either in ascending or descending order is known as sorting.

Selection sort :
In this type, basic idea is to repeatedly select the smallest key in the remaining an unsorted array.

UDF for selection sort
:
void sel_sort(int ar[], int size)
{
int small, pos, temp;
for(int i=0; i<size; i++)
{
small=ar[i];
pos=i;
for(int j=i+1; j<size; j++)
{
if(ar[j]<small)
{
small=ar[j];
pos=j;
}}
temp=ar[i];
ar[i]=ar[pos];
ar[pos]=temp;
cout<<"Array after pass"<<i+1<<"is";
for(j=0;j<size;j++)
cout<<ar[j];
}}


Bubble Sort :
In this type, two adjoining values are compared and then exchanged if are not in proper order.

UDF for bubble sort :
void bubble_sort(int ar[], int size)
{
int temp, ctr=0;
for(int i=0; i<size; i++)
{
for(int j=0; j<(size-1)--i; j++)
{
if (ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}}
cout<<"Array after iteration"<<++ctr<<"is";
for(int k=0;k<size;k++)
cout<<ar[k];
}}


Two Dimensional array :
Two dimensional array is a type of array  which is a one dimensional array in itself. It is written as    array_name[number of rows][number of columns]


Implementation of 2-D array in memory :

Row major implementation :
All elements are stored row wise
add of [I][J]=B+w(C(I-Lr)+(J-Lc))

Column major implementation :
add of [I]{J}=B+w(I-Lr)+(R(J-Lc))

where B is base address
w is size in bytes of one element
Lr-lower bound of rows
Lc-lower bound of columns
R-total number of rows
C-total number of columns

Classes And Objects

Introduction : Class is the user defined data type representing similar objects. It binds data and functions together.
Syntax :
 class class_name
{
private :
//variable declaration
//function declaration
protected :
//variable declaration
//function declaration
public :
//variable declaration
//function declaration
};
Example
class account
{
int accno;
char type[10];
float balance;
float deposit(float amount)
{
balance+=amount;
}
float withdraw(float amount)
{
balance+=amount;
}
};

Definition of function

  • outside the class: Definition is done by using scope resolution operator(::).
  • Syntax:
    class_name ::function_name(parameter list)
    {
    function body
    }
  • Example :
    void display()
    {
    cout<<"This is the example of defining the function outside the class";
    }
  • Inside the class,function is defined normally without any operator.
  • Syntax:
    function_name(parameter list)
    {
    function body
    }
  • Example :
void display()
{
cout<<"this is the example of defining the function inside the class";
}

Some points about classes and objects 
  1. Classes does not occupy any memory unlit and unless objects of that class are made.
  2. The data members and member functions are referred by using dot operator(.) with object of the class.
    Example:
    Student S1;
    S1.marks;
  3. Arrays can also be used as the data member of the class. It can be used to perform operations in the functons but cannot be used directly by objects.
Scope of class and its members :
Class represents a group of data and functions which are divided into :
  1. PUBLIC : These data members can be directly accessed by any function(whether present inside the class or outside the class)
  2. PRIVATE :These data members can be used privately by member function of the class in which it is declared. It use the concept of data hiding.
  3. PROTECTED : These data members can be accessed by member functions of the class and the friends of the class in which it is declared.
Global Class : The class which is defined outside the body of the function. Its object can be declared from anywhere in the program.

Local Class The class which is defined inside the function. Its object can be declared only within the function where class is defined.

Global Object : an object is said to be global if it is declared outside all the functions and is available for all functions of the class.

Local Object : An object is said to be local if it is declared inside a function and this type of object cannot be used outside the function where it is declared.

Nested Class : Class declared within another class. The outer class is known as Enclosing class and the inner one is known as Nested class.

Object Declaration Syntax and example :
class_name object list(separated by comma);
student s1,s2,s3;

Lecture 6: Logic Circuits (1): Building Boolean Expressions


(Following tutorial's examples are taken from book "Computer Fundamentals - Third Edition". Author: Pradeep K Sinha and Preeti Sinha. Publisher: BPB Publications, New Delhi. Latest version of this book is available for purchase at: www.bpbonline.com)

29.06.12

The logic gates, described in this section, are seldom used alone, but are used in combinations. They interconnected to form gating/logic networks, which are known as combinational logic circuits. For these logic circuits, the Boolean Algebra expression can be derived by systematically progressing from input to output on the gates. Few examples are given below:

Example 1: (In book: Example 6.6, Page: 77)

Find the Boolean expression for the output of the logic circuit given below:

This gate is constructed using: 'Logisim'. Download this FOSS from: ozark.hendrix.edu
 Solution:
Input A is fed to the NOT gate, whose ouput will be A .
Inputs B and C are fed to the OD gate, whose output will be B + C.
Now two outputs ( A and  B + C) are fed as input to the AND gate. The output produced by AND gate will be A.(B+C).
Hence, D = A.(B+C), which is required Boolean expression for the output of the given logic circuit.

Example 2: (In book: Example 6.7, Page 78)

Find the Boolean expression for the output of the logic circuit given below: 

This gate is constructed using: 'Logisim'. Download this FOSS from: ozark.hendrix.edu
Solution:
The output of the OR gate is: A + B --------------- (a)
The output of the first AND gate is: A . B -------------- (b)
Since, expression (b) is fed as input to the NOT gate, the output of the NOT gate is: A.B ------------- (c)
Now, expressions (a) and (c) are fed as input to the second AND gate, whose output will be: (A + B) . (A.B)
Hence C = (A + B) . (A.B), which is the desired logic expression for the output produced by the given logic circuit. 

Lecture 5: Designing Circuits and operations and their gates (2)

28.06.12
2. OR GATE

http://sub.allaboutcircuits.com/images/04107.png
OR gate, so-called because the output of this gate will be "high" (1) if any of the inputs (first input or the second input or . . .) are "high" (1). The output of an OR gate goes "low" (0) if and only if all inputs are "low" (0). 

A two-input OR gate's truth table looks like this:
http://sub.allaboutcircuits.com/images/04108.png

3. NOT GATE
[EDITED] http://sub.allaboutcircuits.com/images/04154.png
NOT Gate is also known as Inverter, it reverses or 'invert' the input given. In digital circuits drawing its drawn as 'small circle'. That circle in the NOT-gate symbol is called an inversion bubble. A bubble on the input or output wire of another circuit symbol indicates an inversion of that signal: 0 becomes 1, and 1 becomes 0.

Some Booleen functions have identical truth tables therefore their logic circuits serves identical purposes; but one may be preferable to the other. To do this more useful logic gates are created. The following gates NAND, and NOR were created for this purpose.

4. NAND GATE
A variation on the idea of the AND gate is called the NAND gate. The word "NAND" is a verbal contraction of the words NOT and AND. Essentially, a NAND gate behaves the same as an AND gate with a NOT (inverter) gate connected to the output terminal. To symbolize this output signal inversion, the NAND gate symbol has a inversion bubble on the output line. The truth table for a NAND gate is as one might expect, exactly opposite as that of an AND gate: 

http://sub.allaboutcircuits.com/images/04106.png
As with AND gates, NAND gates are made with more than two inputs. In such cases, the same general principle applies: the output will be "low" (0) if and only if all inputs are "high" (1). If any input is "low" (0), the output will go "high" (1). 

We can see that equivalent AND gate circuit (which works same as NAND gate circuit) can be constructed with one two-input AND gate and then affixing one NOT gate on its output.

5. NOR GATE
The NOR gate is an OR gate with its output inverted, just like a NAND gate is an AND gate with an inverted output.


NOR gates, like all the other multiple-input gates seen thus far, can be manufactured with more than two inputs. Still, the same logical principle applies: the output goes "low" (0) if any of the inputs are made "high" (1). The output is "high" (1) only when all inputs are "low" (0).

We can see that equivalent OR gate circuit (which works same as NOR gate circuit) can be constructed with one two-input OR gate and then affixing one NOT gate on its output. 

  • REVIEW:
  • Rule for an AND gate: output is "high" only if first input and second input are both "high."
  • Rule for an OR gate: output is "high" if input A or input B are "high."
  • Rule for a NAND gate: output is not "high" if both the first input and the second input are "high."
  • Rule for a NOR gate: output is not "high" if either the first input or the second input are "high."
(This portion of tutorial is taken from|allaboutcircuits.com)


Lecture 4: Designing Circuits and operations and their gates (1)

26.06.12
DESIGNING CIRCUITS:

The first step in the design of a circuit is to establish a truth table that shows the output for all possible inputs.
Truth Tables
notAA'
andABA.B orABA+B

01

000
000

10

010
011

10

100
101

10

111
111

With proper input electronic digital circuits (logic circuits) establish logical manipulate paths. By passing binary signals through various combination of logic circuits, any desired information for computing can be operated on; each signal represents a binary carrying one "bit" of information.

The logic circuits or gates perform the logical operations.
(This portion of tutorial is taken from|yale.edu)

OPERATIONS AND THEIR GATES:
1. AND GATE
http://sub.allaboutcircuits.com/images/04100.png

http://sub.allaboutcircuits.com/images/04101.png
We can see, that Multiple input gates (gates with more than two inputs) exist in real world. Inverters and buffers exhaust the possibilities for single-input gate circuits. What more can be done with a single logic signal but to buffer it or invert it? To explore more logic gate possibilities, we must add more input terminals to the circuit(s). Adding more input terminals to a logic gate increases the number of input state possibilities. With a single-input gate such as the inverter or buffer, there can only be two possible input states: either the input is "high" (1) or it is "low" (0). As was mentioned previously in this chapter, a two input gate has four possibilities (00, 01, 10, and 11). A three-input gate has eight possibilities (000, 001, 010, 011, 100, 101, 110, and 111) for input states. The number of possible input states is equal to two to the power of the number of inputs: 


Lets take example of multiple input AND gate:


Now above AND gate have three inputs and applying the formula of 2n, where n is number of inputs, with three inputs it becomes: 23=8, so we will have 8 possible rows in this truth table, in the pattern of (000 (FFF), 001(FFT), 010(FTF), 011(FTT), 100(TFF), 101(TFT), 110(TTF), and 111(TTT)). In this way we can construct truth tables for multiple input gates.
...continued

Lecture 3: The conditional and the Biconditionals Statements and Binary System

25.06.12
The conditional and the Biconditionals Statements.

If the connectors \rightarrow is used between any two statements P and Q to form a compound statement P \rightarrow Q (reads if P then Q), the statement is called a conditional statement.
Example:
Let P = You Passed English
Q = You will graduate

Then this can be recorded in Truth Table as:
PQP\rightarrowQ
TTT
TFF
FTT
FFT

The statement P\rightarrowQ reads if you pass English then you will graduate. This statement is false only when you pass English (true) but you will not graduate. Therefore the final column will be true in every position but the second.

The connective \leftrightarrow is called the biconditional and may be placed between any two statements to form a compound statement P \leftrightarrow Q (reads P if and only if Q).

Then this can be recorded in Truth Table as:
PQP\leftrightarrowQ
TTT
TFF
FTF
FFT


THE BINARY SYSTEM AND BOOLEAN ALGEBRA
The Boolean algebra provides rigorous procedures for deciding whether a statement is true or false;if the statement can be expressed in two variables. In Boolean algebra true is represented by a 1 and false by a 0. With these two digits (0,1) and the three basic operations called “not”, “and” and “or”, digital algebra or switching algebra was developed.

The basic operations and their meaning:

OperationMeaningSymbol
orDetermine a single input bit from the values of two or more input+ (A+B)
andDetermines a single input bit from the value of two or more input. (A.B or AB)
notChange binary bits to its opposite value! (!A or ~A or bar over A)


Any relationship between logical variables are called logical expressions. These expressions can be written as an equation for example the equation A + B + C = F where F is the name of the output variable. The expression A + B + C = F expresses the action of and/or function. Through Boolean Algebra logical analysis can be performed using these three functions.
The electronic representation of these functions are called logic gates. There are the and gate the not and the or gates. These logic gates are basic functional units for both arithmetic and logic operations; to operate they must accept binary numbers, and should have a carry bit of one or 0, (from the adjacent lower power of two), and should produce as outputs a sum bit and a carry bit for the next higher power of two.

 

Lecture 2:Types of Compound statements and their connectives: Boolean Algebra for class XII Computer Science Students

24.06.12
Types of Compound statements and their connectives

1. A negation: formed when we negate a simple statement by "not"

Simple statement: Today is Thursday
Compound statement: negation: today is not Thursday

The sentence "today is not Thursday" is a compound statement called a negation.

2. When we connect two simple statements using "and" the result is a compound statement called a conjunction.
3. If the simple statements are joined by "or" the resulting compound statement is called a disjunction.
4. The If . . . then connector is used in compound statements called conditionals.
5. The if and only if connector is used to form compound statements called biconditionals.

We are familiar with using letters as replacements in algebra; in logic we can also use letters to replace statements. The common letters used to replace statements are P,Q, R: but any letters can be used.
For example:

P = Today is Saturday
Q = I passed my test

but P and Q would read Today is Saturday and I passed my test.
It is also common practice to use symbols for the connective words (or the connectors).

Connectors

Symbols
(a) not

~
(b) and

^
(c) or

(d) if . . . then

\rightarrow
(e) if and only if
\leftrightarrow
TRUTH TABLES:
Since a statement in logic is either true or false, we should be able to determine the truth or falsity of a given statement. [Logic is very precise. There should be no worry about ambiguity] Let P be a statement; then ~ P means "not P" or the negation of P. The negation of P is true whenever the statement P is false and false if P is true. These situations are confusing to write, therefore we can record these statements in a truth table.
Example: 
Let P = this is a hard course.
~P= this is not a hard course.
Then this can be recorded in Truth Table as:
P~P
TF
FT
In the first column, there are two possibilities of P; P is either True or False. Each line in the table represents a case that must be considered. In this case, there are only two cases. The truth table tells us the truth value of p in every case.
Truth Tables with the Connective ^
The Connective ^ may be placed between any two statements P and Q to form the compound statement P^Q.
Example:
Let P = Today is Monday
Q = I have a maths class
 Then this can be recorded in Truth Table as:
PQP^Q
TTT
TFF
FTF
FFF

In the compound statements, the individual statements are called components. In a compound statement with two components such as p ^ q there are four possibilities. These are called logical possibilities. The possibilities are:
1) p is true and q is true
2) p is true and q is false
3) p is false and q is true
4) p is false and q is false.
The four possibilities are covered in the four rows of the truth table. The last column gives values of P ^ Q; This is only true when both p and q are true. Using the examples given, truth tables of a more complicated nature can be built.
Let us consider the situation P∨Q
Example:
Let P = Today is Tuesday
Q = I have a maths class
Hence, P∨Q = Today is Tuesday or I have a maths class
Then this can be recorded in Truth Table as:
PQP∨Q
TTT
TFT
FTT
FFF

Like ^, here are four possibilities which are covered in the four rows of the truth table. The last column gives values of P ^ Q; This is only false when both P and Q are false.

Lecture 1: LOGIC: Boolean Algebra for class XII Computer Science Students

Date: 23.06.12

Introduction to Logic


The main ingredient in the study of logic is the principles and method used to distinguish between arguments that are valid and those that are not. Logic deals with reasoning and the ability to deduce or come to some reasonable conclusions. In everyday life we guess what is going to happen on the basis of past experiences; “It looks like its going to rain” we say meaning that it may rain today. If we wait around long enough then it may rain. This is an example of inductive reasoning
In mathematics we can discover whether or not a guess is correct by checking if our conclusions can be deduced from results already known. This is called deductive reasoning. (This portion of tutorial is taken from|yale.edu)

 Inductive Reasoning v/s Deductive Reasoning

Example of Inductive Reasoning:
All of the swans that all living beings have ever seen are white.
Therefore, all swans are white. 

This type of reasoning is based on general conceptions and tries to draw conclusions on the basis of previously laid down concepts and theories. In the above example, ALL LIVING BEINGS HAVE SEEN EVER SWANS AND FOUND THAT ALL SWANS ARE WHITE, THAT MEANS VARIOUS DIFFERENT EXAMPLES AND INSTANCES (STORIES, EVENTS) ARE BEING RECORDED IN HISTORY WHERE LIVING BEINGS HAVE SEEN WHITE SWANS. SO WE CONCLUDED OUR RESULT THAT ALL SWANS ARE WHITE.

Example of Deductive Reasoning:
All men are mortal.
Socrates is a man.
Therefore, Socrates is mortal.

This type of reasoning in contrast to inductive reasoning is not based on any general principle, rather it takes various examples and then mathematically tried to deduct accurate solutions. 'Mathematical induction' is a type of deductive reasoning, which can be easily seen in the above example, THE FIRST PREMISE STATES THAT ALL OBJECTS CLASSIFIED AS "MEN" HAVE THE ATTRIBUTE "MORTAL". THE SECOND PREMISE STATES THAT "SOCRATES" IS CLASSIFIED AS A "MAN" – A MEMBER OF THE SET "MEN". THE CONCLUSION THEN STATES THAT "SOCRATES" MUST BE "MORTAL" BECAUSE HE INHERITS THIS ATTRIBUTE FROM HIS CLASSIFICATION AS A "MAN".
(example and explanation taken from|wikipedia.org) 

Statements
The starting point of logic is a statement. A statement in the technical sense is declarative and is either true or false, but cannot be both simultaneously. In logic it is irrelevant whether a statement is true or false, the important thing is that it should be definitely one or the other. Logic statements must be either true or false.
A Statement: is a declarative sentence which is either true or false.
Examples of declarative statements:
(a) New Haven is a city in Connecticut.
(b) The month of June has thirty days.
(c) The moon is made of red cheese.
(d) Tomorrow is Saturday.
The following are not statements:
(a) Come to our party!
(b) Is your homework done?
(c) Close the door when you leave.
(d) Good by dear.
Those are not good statements because they cannot be considered true or false.
The basic type of sentence in logic is called a simple statement. A simple statement is one that has only one thought with no connecting word.
Examples of simple statements
(a) Three is a counting number.
(b) Ann is early for class
If we take a simple statement and join them with a connecting word such as and, or, if . . . then, not, if and only if, we form a new sentence called a complex or compound statement.
Compound Statements: are formed from the combination of two or more simple statements.
(a) Ann is early for class and she has her note books.

(b) Three is a counting number and is also a odd number.
(This portion of tutorial is taken from|yale.edu)