Showing posts with label Basic Programming. Show all posts
Showing posts with label Basic Programming. Show all posts

## Friday, 25 October 2013

### String Class

In the C++ programming language, the std::string class is a standard representation for a string of text. This class reduces many of the problems introduced by C-style strings by putting the responsibility of memory ownership on the `string` class rather than on the programmer. The class provides some typical string operations like comparison, concatenation, find and replace, and a function for obtaining sub-strings It can be constructed from a C-style string, and a C-style string can also be obtained from it. Here is an example C-program

## Sunday, 20 October 2013

### Basic question -5 (swapping values without using third variable )

How to Swap values of 2 variable without using third variable .
It is to simple If  we use third variable .

Let  variable are 'a' , 'b' , 'temp' . we have swap value  of variable 'a' & 'b'.  & 'temp' is third variable .

temp = a
a = b
b  = temp

Now solve it without using third variabe

a = a + b
b = a - b
a = a - b

### Basic Question -4 (find divisor)

Write a Program to take integer as input & find out all its divisor & sum of  Divisor .

#### Algorithm :

Simply make a for loop & check for each no . Is it divider of given no or not . IF it is divider of given no add to sum.

### Basic question -3 (simple recursion)

This time  we are going to discuss simple recursion .
what is recursion : It is simply a solving technique to solve function which uses its previous volue.
example : Xn = Xn-1 + 2. recursion

It is very  basic example.we can use this technique  in computer science to solve such type of problem very effectivelly . example traversal of tree, find factorial etc.

## Saturday, 19 October 2013

### Basic question -2 (make a simple calculator)

Make a simple calculator .which can do operATION like addition,subtraction, division,multiplication. using Function.

there is another challenge for beginners that there is an error in program (given below) provided for above question.
that is if we divide two no. if answer coming out must be float, if it is not perfectly divisible.
BUT answer print  on screen is integer .

find out error ..
but before that solve this basic problem by yourselves.

### Basic Questions-1 (string operation)

Basically we are going to solve a simple problem :

Write a PRogram to take input 3 student name & there roll no.  after that it ask for a roll no if it is found it print name of student associated with that roll no.

IT is very simple use header file "string.h" & use gets() & puts() function to take input string & print string.
& use strcmp() function to compare strings.

NOTE : strcmp() function return value 0 if comparison successful else it return an integer value that is equal to ASCII value difference between first non matching character encounter.
EXAMPLE : if we compare string "beginner" & "beginer" first none matching character is  'n' & 'e' so
strcmp() return integer value = ASCII value of  'n' - ASCII value of  'e'  .

## Monday, 14 October 2013

### Operation on string

Operation on string is not like operation o integer. for an example
we can't assign data or compare directly
str1=str1+str3
or
str1==str2
BOTh assignment are wrong .So we use new way to do operation on string.

#### comparison of two string : two way to do this one is very obvious i.e compare character by character. using while or for loop.OR second way is too easy using function strcmp()   .

One important property of strcmp() is It has volue 0 if string compare are equal Else it has value equal to numeric difference between first nonmatching character in the string.

### Arithmetic Operation On character

We can do arithmetic operation on character lets see some operation:

#### subtract a integer from a character:

`````` #include<stdio.h>
main()
{
char c='z';  //initialising
char ch;
printf("\n c = %c",c);
ch=c-1;  //subtracting integer from charater
printf("\n c-1=%c",ch);
}
``````
OUTPUT:- is nothing but character corressponding to ASCII volue=ASCII volue of 'z'-1

### HOW to take string as a Input using getchar()

getchar() take one character as input at a time . And taking input using scanf() also has a problem that it stop taking input after encounter any space.
lets SEE an example :-
`````` #include<stdio.h>
main()
{
char str;
printf("\ninsert string press enter at end of string:\n::");
scanf("%s",&str);  //taking string as input
printf("\n\n string input is ::%s",str);  //printing string
}
``````
Copy this code & run you get output like this :

### Solve problem of scanf() when it take char as input using getchar()

getchar() funtion use to take single character at a time .It is mostly use to remove limitations of scanf() funtion to take character as input.
take a example:
`````` #include<stdio.h>
main()
{
char c,ch;
printf("\ninsert volue of c :");
scanf("%c",&c);  // taking input for c
printf("\ninsert volue of ch :");
scanf("%c",&ch);  //taking input for ch
printf("\n c =%c && ch = %c",c,ch);
}
``````
Now run it you see that it does not ask for ch to input because it take '\n' (Enter button) as input . we press enter button after inserting volue for c. that enter button goes to ch as input.
so OUTPUT is :-

## Sunday, 13 October 2013

### Sum of square of series of no.

Write a program using single subscripted variable to evoluate the following expressions:
total=x1^2 + x2^2 +.......................+x10^2 .

This problem is very easy to implement & solve.
just make an array & take input.

``      ` total=0    // initialise total to 0 initially `
``````       i=0;    //you can use for loop instead of using while
while(i<10)
{
total=total+(arr[i]*arr[i]);  // this is main operation
i++;
}``````

C program :

### 2's Complement of a Binary number

2's complement of binary no. is basicaly = 2^n -binary no.
where
n= no of digit before decimal.

#### Easy way to find out 2's complement of binary no :

=> Move from right to left of binary no. Untill fist 1 found .
=>Now change all no (1 by 0 && 0 by 1) before above first node .

#### Examples:

binary no =10010010              2's complement=01101110
binary no=0011001                 2's complement=1100111
binary no=10000                     2's complement= 10000
binary no=0001                       2's complement=0001

## Friday, 11 October 2013

### Check Palindrome using double link list

Palindrome  is A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction.(wiki answer).
Two check given string is palindrome or not.It is very easy if we use double link list.

### Procedure :-

::-  insert each char of string in double link list with each node contain only one char.

::- intialise "HEAD" to first node & "TELL" to last node.

::- check data at first node & last node then increment head & decrement in tell than check     again untill any mismatch found or they reaches to mid of list

## Wednesday, 9 October 2013

### Generate random no & push it stack

This is very basic program .We have to generate few random no. & check no is even or odd. If even than push it to stack 1 else IF no is odd push it to stack 2. Than print stack 1 & stack 2.

C Program :

We use pointer to pass array & for push operation.

### TO Print all ASCII value

ASCII (American Standard Code for Information Interchange) .Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as 'a' or '@' or an action of some sort. ASCII was developed a long time ago and now the non-printing characters are rarely used for their original purpose.
THis picture contain ASCII values  only correspond to 0 to 127 :
BUT in our program we show all possible ASCII values & corresponding character.

C code :

## Saturday, 5 October 2013

### Creating dynamic array

In this programm we Creating array of given size dynamically   & doing operation of insertion & deletion
Must include header " malloc.h ".
For better description see " C program " carefully.

C code :

### Convert decimal to binary number

This is very basic program .:: just follow
::- see how to convert decimal number to binary..

Note:- we consider only integer decimal number.

Procedure :- Just simply divide decimal number by 2 as shown in figure & write remainder in reverse order .

C code :-

## Tuesday, 1 October 2013

### Bubble sort in c

Bubble sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.

C code ::

Blogger Widgets