Sabtu, 23 November 2013

Mathematical Functions in Turbo C Language

The C language provides a number of library functions that carry out mathematical operations such as square root, sine, cosine, tan, and various other actions. These functions, which are part of C mathematical library are called mathematical library functions. They are also called mathematical built in functions. 
Brief description about each mathematical function is available in your program's header file called “math.h”. Therefore, anyone who uses mathematical functions should include “math.h” in the beginning of the program. The various mathematical functions which are commonly used are provided below:

Function with return type
Operations performed
abs(int i)
Returns absolute value of an integer.
cos(x)
Returns the cosine value of x.
acos(x)
Returns the inverse cosine of an angle where x is in radians.
sin(x)
Returns the sine value of x.
asin(x)
Returns the inverse sine of an angle where x is in radians.
tan(x)
Returns the tangent of an angle where x is in radians.
atan(x)
Returns the inverse tangent of an angle where x is in radians.
exp(x)
Returns the value of e to the power x.
fabs(x)
Returns the absolute value of floating point number x.
log(x)
Returns the natural logarithm of x.
log10(x)
Returns the logarithm of x to the base of 10.
pow(x,y)
Returns the value of x to the power y.
sqrt(x)
Returns the square root of x.

 These functions are given here to just start with programming. In most of the general or mathematical problems such built in functions are regularly used. We will discuss about examples about each of these functions later.

Formatted Input/Output Functions

Formatted Output Function Printf() in Turbo C Language

The data stored in the memory area can be displayed on the monitor or any other output device. This can be performed using printf() function in C. Using this function programmer can display numerical values such as integer, floating point number, character and strings.

Syntax
int printf(“format string”,var1,var2,var3,….varn);
where

  • “format string” consists of one or more format specifiers. Each format specifier start with % sign.
  • var1, var2, var3, ……, varn represent the values of variables in given series

Return Type value: The function will return an integer value. This integer value is the number of items that have been displayed successfully on the screen. But generally in our programs we will not use this return value.

The various format specifiers along with the meaning associated with them are shown below. These format specifiers are same as in earlier post. The following table shows the given values, for the statement to be executed and the output obtained on the screen:

Formatted Output Function Printf() in Turbo C Language

Field width specification for integers:

The printf() function uses the field width to know the number of columns used on the screen while printing a value.
The specification is provided by means of a format string like “%wk” where

  • w indicates total number of columns required to print the value or specifies the width.
  • k is the format given in the above table earlier for int.

The table below shows the given values, the statement and the output obtained on the screen:

Formatted Output Function Printf() in Turbo C Language

Field width specification for float:

The floating point or real number can be displayed using printf() statement with format specification. The syntax is shown below:
“%w.xf” where

  • w indicates total number of columns required to print the value.
  • x is the number of columns used after the decimal point.

The following table shows the given values, the statement to be executed and the output obtained on the screen:

Formatted Output Function Printf() in Turbo C Language

Field width specification for string:

The string (series of character) can be displayed using printf() statement with format specification. The syntax is shown below:
“%w.x” where

  • w indicates the total number of columns reserved to print the string.
  • x is the number of columns used to print the string in right justified way. The rest of the characters are ignored.

The below table shows the given values, the statements and the obtained output on the screen for
str = “Dot Programming”

Formatted Output Function Printf() in Turbo C Language

Note: In the last printf() statement all 15 columns are free and the cursor will be place in the 16th column.

Formatted Input Functions

Kamis, 21 November 2013

Formatted input/output function in Turbo C Language

It is very easy to use unformatted input/output functions as discussed. We can read and display only characters not numbers using unformatted functions. Using formatted input/output functions, we can read integers, floating point number and characters. The formatted input function is scanf() and formatted output functions is printf().

Scanf()

Syntax
int scanf(“format string”, &var1, &var2, &var3,……..&varn);

This function scanf() is used to read any type of data such as int, char, float, double and even string from the keyboard. It accepts the following two parameters:
format string consists of one or more format specifires starts with % symbol.
&var1, &var2, &var3, …., &varn represents the list of variables. Here, symbol ‘&’ refers to the address of the variable. In the memory locations identified by these addresses, the respective values are stored.
Return Type int: The function returns an integer value. This integer value is the number of item that have been read successfully using the keyboard. But, we rarely use this return value in our programs.
The various format specifiers along with the associated meaning are shown in the following table:

Data Type
Format Specifier
Meaning
int
%d
%o
%x
%i
%u
%h
Read a decimal integer value
Read an octal value
Read a hexadecimal value
Read a decimal, octal or hexadecimal value
Read an unsigned integer value
Read a short integer value
float
%e
%f
%g
Read a floating point number
Read a floating point number
Read a floating point number
char
%c
%s
Read a character
Read a string (series of character)
double
%f
Read a long floating point number or double
long int
%ld
Read a long integer value

The following table shows the way the user has to enter the values from the keyboard:

Input statement
Entering value
Input way
scanf(“%d%d%d”,&a,&b,&c);
3 integer value
10 20 30
Where, a=10      b=20       c=30
scanf(“%d,%d,%d”,&a,&b,&c);
3 integer value
10,20,30
Where, a=10      b=20       c=30
scanf(“%f”,&a);
1 floating point value
787.35
Where, a=787.3.5
scanf(“%c”,&a);
1 character value
P
Where, a=p
scanf(“%f,%d,%c”,&a,&b,&c);
1 floating point value
1 integer value
1 character value
787.35 10 P
Where, a=787.35, b=10, c=P
scanf(“%d-%d-%d”,&a,&b,&c);
3 integer values separated by ‘-‘ (hyphen) sign
10-20-30
Where, a=10, b=20, c=30
scanf(“%d%d%d”,&a,&b,&c);
3 integer values separated by ‘/’ (slash) symbol
10/20/30
Where, a=10, b=20, c=30

Note: Do not use any escape sequences such as \t, \n etc. in scanf().

For example: Consider the following statement:
scanf(“%d\n”,&a);
In the above statement, remove ‘\n’, otherwise it may give wrong output.
Now, let us see, some more ways of data reading using scanf() function are given below:
Input statement
Entering value
Input way
scanf(“%2d%2d%2d”,&a,&b,&c);
3 integer values
1 2 3
Where, a=1  b=2  c=3
11 22 33
Where, a=11  b=22  c=33
102030
Where, a=10  b=20  c=30
102 030 4
Where, a=10  b=2  c=3
The remaining two digits 0 and 4 are ignored or can be read by a subsequent scanf()
scanf(“%12s”,&str);
1 string
.Programming
Where, str=.Programming
scanf(“%[^\n]s”,str);
Input the string till the end of line
Wlecome! To the dotprogramming
Where, str= Wlecome! To the dotprogramming
Note:
To read a string with space gets() function will used in place of scanf().
The “scanf(“%[^\n]s”,str);” function read and stored all the character untill user will not press enter key.

Formatted Output Function

Windows Store App : How to use Grid in XAML

Introduction

By-default a Grid element has one row and one column i.e. a single cell, any control placed inside the Grid will be placed in this cell. Lets see the below simple XAML code where a textbox is placed in the first row and first column. Programmer is not specifying here, but it have value of Grid.Row as well as Grid.Column property to zero.

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock Text="Hello World" />
</Grid>

How to Insert multiple Rows and Columns in Grid

Step-1 : Create <Grid.RowDefinitions> and <Grid.ColumnDefinitions > inside the Grid.

  <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>              
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions >             
        </Grid.ColumnDefinitions>

</Grid>
Step-2: Lets create Three rows and three columns, by adding three RowDefinition and ColumnDefinition child as in below code.

<GridBackground="{StaticResourceApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>   
            <RowDefinitionHeight="150"/>
            <RowDefinitionHeight="Auto" />
            <RowDefinitionHeight="*" />
           
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>          
            <ColumnDefinitionWidth="*" />
            <ColumnDefinitionWidth="*" />
            <ColumnDefinitionWidth="*" />
        </Grid.ColumnDefinitions>

</Grid>

Here we have three rows and three column. Every row contains height attribute and column contains width attribute.The first row's height, expressed as an integer value for a pixel count. In the second row's height, described by a literal "Auto" or you can say height will adjust according to the content which will placed inside the grid. In third row's height, described by the asterisk character(*)  means you can say the last row will take remaining space of the grid.

How to place controls in rows and columns 

Generally grid is used when we are placing elements in no. of rows and columns. Suppose we want to move control to the second row and second column. To do that we have to set the value of Grid.Row and Grid.Column attribute of the respective element. Review the following XAML:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>   
            <RowDefinition Height="150"/>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
           
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions >          
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBlock x:Name="firstBlock" Text=" Hello World"Grid.Row="1" Grid.Column="1" />
    </Grid>

Look out the above code, i have set the value of Grid.Row and Grid.Column to 1 for textbox element. By this it will placed in the second row and second column.

Design view of the code

Grid row definition and column definition in xaml: Windows Store App

Here your snap simplifies relation between TextBlock and Grid. Lets take a simple theory to understand how to define TextBlock outside the Grid.RowDefinitions and Grid.ColumnDefinitions.

Suppose you have three fruits apple, grapes and orange now you want to place all of them in individual row and column. Then you can simply place these items in the specified rows and column using Grid.Row and Grid.Column attribute.

Rabu, 20 November 2013

Unformatted Input/Output Functions with Example: C Language

In our earlier post i have discussed about sequential and compound statements. Now in this post we will discuss about unformatted i/o functions. There are several standard library functions available in this category - those that can deal with a single character and those that can deal with a string of characters. The various unformatted input/output functions in C are shown below:


getchar( ) and putchar( )

Even though getchar( ) and putchar( ) looks like functions, they are not. They are the macros that are used to read and display a character. The syntax to read a character shown below:

“ch = getchar( )” will reads a character from the keyboard and copy it into memory area which is identified by the variable ch. No arguments are required for this macro. Once the character is entered from the keyboard, the user has to press Enter key.

“putchar(ch)” outputs a character stored in a variable on the monitor. The variable should be passed as parameter as shown in the above syntax.

Example1
main()
{
 char ch;
 clrscr();
 ch = getchar();
 putchar(ch);
 getch();
}

getch(), getche() and putch()

The functions getch() and getche() are used to read a character from the keyboard, similar to getchar(). Both functions don’t need a return key pressed to terminate the reading of a character. A character entered will itself terminates reading. 

In case of getch(), the character entered is not displayed or echoed on the screen, whereas in getche(), the character entered is echoed or displayed on the screen. Syntax for both the macros
ch = getch();            /*Typed character will not be displayed on the screen*/
ch = getche();            /*Typed character will be displayed on the screen*/

Now, we have to display the inputted character on the screen, putch() macro is introduced.
“putch(ch)” This function outputs a character stored in the memory, on the monitor. The variable should be passed as parameter to the functions. In the above syntax ‘ch’ is used as an argument and its value is displayed on the screen.
Example2:
main()
{
  char ch;
  clrscr();
  ch = getche();
  putch(ch);
  getch();
}

gets() and puts()

These functions are used to read a set of characters (string) from the keyboard and display a set of characters (string) on the screen.
char str[5];
gets(str);
        /* Reads a set of characters into memory area str */
puts(str);         /* Displays a set of characters from memory area str */
Example3
main()
{
  char str[10];
  clrscr();
  gets(str);
  puts(str);
  getch();
}

Formatted I/O Functions

How to Implement Custom Control, Currency TextBox: Windows Forms

Currency Textbox, custom control, as the name implies, will accept only numbers, decimals and control characters. None of other type will be allowed in this textbox. This textbox is basically used to input prices, rates or any measurement including precision.

I have discussed about all the events used to create custom textbox. Open your Visual Studio 2013 Windows Forms project and add a new class named “CurrencyTextBox”, inherit this by System.Windows.Forms.TextBox. Go through the following points, they all together will make this textbox:

Property

Property will be used to get or set the value in the textbox.
private decimal currencyValue;
public decimal CurrencyValue
        {
            get { return currencyValue; }
            set { currencyValue = value; }
        }

Constructor

When the object of this class has been created, by-default it’s value is set to zero, as written in the below c# code.
public CurrencyTextBox()
        {
            currencyValue = 0;
        }

OnKeyPress

What is to be allowed to write in the textbox? Decided in this event. The first line is used to call its base class constructor with specified arguments. After this I have used some if conditions to specify which key is to be accepted or which is not. “e.Handled=true” this line is used to stop the character from being entered into the textbox.
protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            if (e.KeyChar == '.' && this.Text.Contains("."))
            {
                e.Handled = true;
            }
            if (e.KeyChar == '.' && this.Text.Length < 1)
            {
                e.Handled = true;
            }
        }

OnValidated

When the focus is changed, this event will convert the entered value into decimal value. Because currency is used in decimal not string.
protected override void OnValidated(EventArgs e)
        {
            Decimal value = Convert.ToDecimal(this.Text);
            this.Text = value.ToString("C");
        }

OnClick

When the user click on the textbox, it will set the text to currencyValue (property value) and the cursor will be placed at the last of input value.
protected override void OnClick(EventArgs e)
        {
            this.Text = currencyValue.ToString();
            if (this.Text == "0")
                this.Clear();
            this.SelectionStart = this.Text.Length;
        }

OnTextChanged

Change the newly entered value into decimal format.
protected override void OnTextChanged(EventArgs e)
        {
            currencyValue = Convert.ToDecimal(this.Text);
        }

Run your project and in the toolbox a node is added named components followed by project name. You can use this textbox in your project as per your requirements.

Download CurrencyTextBox.cs