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

Monday, January 13, 2014

C - Structure. What is structure in C Language?

A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. Structures help to organize complicated data, particularly in large programs, because they permit a group of related variables to be treated as a unit instead of as separate entities.

The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces. An optional name called a structure tag may follow the word struct (as with point here). The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces.

The variables named in a structure are called members. A structure member or tag and an ordinary variable can have the same name without conflict, since they can always be distinguished by context. Furthermore, the same member names may occur in different structures, although as a matter of style one would normally use the same names only for closely related objects.

Uses of C structures:

    1. 1. C Structures can be used to store huge data. Structures act as a database.
    2. 2. C Structures can be used to send data to the printer.
    3. 3. C Structures can interact with keyboard and mouse to store the data.
    4. 4. C Structures can be used in drawing and floppy formatting.
    5. 5. C Structures can be used to clear output screen contents.
    6. 6. C Structures can be used to check computer’s memory size etc.

Difference between C variable, C array and C structure:

    • 1. A normal C variable can hold only one data of one data type at a time.
    • 2. An array can hold group of data of same data type.
    • 3. A structure can hold group of data of different data types
    • 4. Data types can be int, char, float, double and long double etc. 
Datatype

C variable

C array

C structure

Syntax Example Syntax Example Syntax Example
int int a a = 20 int a[3] a[0] = 10
a[1] = 20
a[2] = 30
a[3] = ‘\0′
struct student
{
int a;
char b[10];
}
a = 10
b = “Hello”
char char b b=’Z’ char b[10] b=”Hello”

Below table explains following concepts in C structure.

    1. 1. How to declare a C structure?
    2. 2. How to initialize a C structure?
    3. 3. How to access the members of a C structure?
Type Using normal variable Using pointer variabe
Syntax struct tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};
struct tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};
Example struct student
{
int  mark;
char name[10];
float average;
};
struct student
{
int  mark;
char name[10];
float average;
};
Declaring structure variable struct student report; struct student *report, rep;
Initializing structure variable struct student report = {100, “Mani”, 99.5}; struct student rep = {100, “Mani”, 99.5};  report = &rep;
Accessing structure members report.mark
report.name
report.average
report  -> mark
report -> name
report -> average

 Example program for C structure:

           This program is used to store and access “id, name and percentage” for one student. We can also store and access these data for many students using array of structures. You can check “C – Array of Structures“ to know how to store and access these data for many students.

#include <stdio.h>
#include <string.h>

struct student
{
           int id;
           char name[20];
           float percentage;
};

int main()
{
           struct student record = {0}; //Initializing to null

           record.id=1;
           strcpy(record.name, "Caroline");
           record.percentage = 86.5;

           printf(" Id is: %d \n", record.id);
           printf(" Name is: %s \n", record.name);
           printf(" Percentage is: %f \n", record.percentage);
           return 0;
}

Output:

Id is: 1
Name is: Caroline
Percentage is: 86.500000

Example program – Another way of declaring C structure:

           In this program, structure variable “record” is declared while declaring structure itself. In above structure example program, structure variable “struct student record” is declared inside main function which is after declaring structure.
#include <stdio.h>
#include <string.h>

struct student
{
            int id;
            char name[20];
            float percentage;
} record;

int main()
{

            record.id=1;
            strcpy(record.name, "Raju");
            record.percentage = 86.5;

            printf(" Id is: %d \n", record.id);
            printf(" Name is: %s \n", record.name);
            printf(" Percentage is: %f \n", record.percentage);
            return 0;
}

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000

C structure declaration in separate header file:

In above structure programs, C structure is declared in main source file. Instead of declaring C structure in main source file, we can have this structure declaration in another file called “header file” and we can include that header file in main source file as shown below.
Header file name – structure.h

Before compiling and executing below C program, create a file named “structure.h” and declare the below structure.

struct student
{
int id;
char name[20];
float percentage;
} record;
Main file name – structure.c:

           In this program, above created header file is included in “structure.c” source file as #include “Structure.h”. So, the structure declared in “structure.h” file can be used in “structure.c” source file.
// File name - structure.c
#include <stdio.h>
#include <string.h>
#include "structure.h"   /* header file where C structure is
                            declared */

int main()
{

   record.id=1;
   strcpy(record.name, "Raju");
   record.percentage = 86.5;

   printf(" Id is: %d \n", record.id);
   printf(" Name is: %s \n", record.name);
   printf(" Percentage is: %f \n", record.percentage);
   return 0;
}

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000


Saturday, January 4, 2014

What is Compiler ?

Definition of Compiler :

  1. A computer program which reads source code and outputs assembly code or executable code is called compiler.
  2. A program that translates software written in source code into instructions that a computer can understand Software used to translate the text that a programmer writes into a format the CPU can use.
  3. A piece of software that takes third-generation language code and translates it into a specific assembly code. Compilers can be quite complicated pieces of software.

Compiler Overview :

Consider process of Executing Simple C Language Code –

We have opened C Code editor and wrote simple C Program , Whenever we try to compile code and try to execute code our code is given to the compiler.
int main()
{
printf("Hello");
return(0);
}
Now our main intention is to convert program into lower level language which can be read by Machine. Compiler have different phases , and program undergoes through these 6 phases. After Passing through all the compilation Phases our code is converted into the machine level language code which can be read by machine.


Sunday, March 31, 2013

QBasic - Checking Palindrome Word

Description:
This tutorial will show how find if a word or string is a palindrome. A palindrome is a word or string that is the same printed forwards and backward.

Examples:
bob
noon
1991
redder

Note: the Oxford English Dictionary states that the longest palindromic word is tattarrattat.

The Guinness Book of Records states that detartrated is the longest word and a soap dish vender saippuakuppinippukauppias is a long palindrome name. (Don’t ask how to pronounce that name).


Note: The program uses LCASE$ instead of UCASE$. LCASE$ converts strings to lowercase.

Code:
DIM Wrd AS STRING
DIM RevWrd AS STRING
DIM x AS INTEGER

CLS

INPUT "Enter Word: ", Wrd

FOR x = LEN(Wrd) TO 1 STEP -1
    RevWrd = RevWrd + MID$(Wrd, x, 1)
NEXT x

PRINT
PRINT "Original Word: "; LCASE$(Wrd)
PRINT "Reverse Word: "; LCASE$(RevWrd)

IF LCASE$(Wrd) = LCASE$(RevWrd) THEN
    PRINT "The Word Is A Palindrome"
ELSE
    PRINT "The Word Is Not A Palindrome"
END IF

Monday, March 25, 2013

QBASIC, Draw a circle

Noun
A round plane figure whose boundary (the circumference) consists of points equidistant from a fixed center.
Verb
Move all the way around (someone or something), esp. more than once: "the two dogs circle each other"; "we circled around the island".
Synonyms
noun.      ring - round - cycle - sphere
verb.      revolve - surround - encircle - wheel - compass
 
NAME
  CIRCLE Statement

SYNOPSIS
  CIRCLE [STEP] (x!,y!),radius![,[color%] [,[start!] [,[end!] [,aspect!]]]]
      o STEP       Specifies that coordinates are relative to the current
                   graphics cursor position.
      o (x!,y!)    The coordinates for the center of the circle or ellipse.
      o radius!    The radius of the circle or ellipse in the units of the
                   current coordinate system, determined by the most recent
                   SCREEN, VIEW, and WINDOW statements.
      o color%     A color attribute that sets the circle's color. The
                   available color attributes depend on your graphics adapter
                   and the screen mode set by the most recent SCREEN statement.
      o start!     The starting angle for the arc, in radians.
      o end!       The ending angle for the arc, in radians.
      o aspect!    The ratio of the length of the y axis to the length of the
                   x axis, used to draw ellipses.
      o To convert from degrees to radians, multiply degrees by (PI / 180).

DESCRIPTION
  Draws a circle or ellipse on the screen.

  Example:
      'This example requires a color graphics adapter.
      SCREEN 2
      CIRCLE (320, 100), 200
      CIRCLE STEP (0,0), 100

Sunday, February 24, 2013

QBASIC Control Statements. QBASIC Looping

QBasic is a programming language, created by Microsoft. It is based upon the original language BASIC (Beginners All-purpose Symbolic Instruction Code).

Control Statements
A program consists of a number of statements which are usually executed in sequence. Control statements allow you to change the computer's control from automatically reading the next line of code to reading a different one. The Control Statements are used for controlling the Execution of the program

There are three types of loops for QBASIC.
  1. While..Wend 
  2. Do Loop 
  3. For Loop
The While Loop’s syntax
WHILE [condition]
[Statement Block]
WEND

Example
CLS
B=1
WHILE B<=9
PRINT B
B=B+1
WEND
END

Thursday, January 31, 2013

Maximum Number Calculator

This program lets the user input three hidden numbers and the program can calculate the maximum number among the three numbers. To type the password in a hidden mode, you have to set the PasswordChar property to alphanumeric symbols such as * .

You can create a function called calMax that consists of three arguments x,y, z .You also need to write a procedure to call this function. This procedure should employs the  If Then ElseIf statements and the conditional operators to determine the maximum number. The function Str is used to convert a numeric to string.

The Code

Function calMax(x, y, z As Variant)
If x > y And x > z Then
calMax = Str(x)
ElseIf y > x And y > z Then
calMax = Str(y)
ElseIf z > x And z > y Then
calMax = Str(z)
End If
End Function

Private Sub Command1_Click()
Dim a, b, c
a = Val(Txt_Num1.Text)
b = Val(Txt_Num2.Text)
c = Val(Txt_Num3.Text)
Lbl_Display.Caption = calMax(a, b, c)
End Sub