Showing posts with label Example. Show all posts
Showing posts with label Example. 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


Sunday, April 7, 2013

Image Zoom Effect Eample

A camera shot that changes smoothly from a long shot to a close-up or vice versa.

A zoom lens is a mechanical assembly of lens elements for which the focal length (and thus angle of view) can be varied, as opposed to a fixed focal length (FFL) lens (see prime lens).

A true zoom lens, also called a parfocal lens, is one that maintains focus when its focal length changes. A lens that loses focus during zooming is more properly called a varifocal lens.











Saturday, April 6, 2013

युवा वैज्ञानिक रिजन कार्कीले अमेरिकी अन्तरिक्ष केन्द्रमा नेपालको गौरव बढाए


काठमाडौँ, नेपालका युवा वैज्ञानिक रिजन कार्कीले विश्वका प्रख्यात वैज्ञानिकहरू जमघट हुने अमेरिकी अन्तरिक्ष केन्द्र (नासा) मा नेपालको गौरव बढाउने अत्यन्त सराहनीय कार्य गरेका छन् । विज्ञानमा न्युटनको सिद्धान्तबाट प्रभावित कार्कीले विना बिजुली र विना इन्धन पहाडमा रेल कुदाउन सकिने आफ्नो विचार अमेरिकामा आयोजित एक ऊर्जा सम्मेलनमा विश्वका विशिष्ट वैज्ञानिक एवम् प्राध्यापकहरूसामु प्रस्तुत गरेर ‘स्टुडेन्ट एचिभमेन्ट एवार्ड’ हात पार्न सफल भएका थिए । बीस वर्षको युवावस्थामै विज्ञान र प्रविधिका क्षेत्रमा विशेष चाखका साथ क्रियाशील रही आफ्नो प्रतिभालाई विश्वमञ्चमा प्रस्तुत गर्न सफल युवा अन्वेषक रिजनले हालै यहाँ रु.२५ हजार १०१ राशीको समी युवा प्रोत्साहन पुरस्कार–२०६९ पनि प्राप्त गरेका छन् । रिजनले सन् २००९ मा अमेरिकी अन्तरिक्ष केन्द्र (नासा)बाट १६ वर्षको उमेरमा विद्यार्थी प्रतिभा अन्तर्राष्ट्रिय स्टुडेन्ट अचिभमेन्ट एवार्ड जितेका थिए । उनले नासामा गएर गुरुत्वाकर्षणसम्बन्धी नियमका बारेमा कार्यपत्र प्रस्तुत गरी सम्मेलनमा सहभागी वैज्ञानिकहरूलाई चकित पारेका थिए । सानोमा छ कक्षामा अध्ययन गर्दा डाक्टर र इन्जिनियर बन्ने सपना सजाएका उनलाई विज्ञानमा न्युटनको सिद्धान्तले आकर्षित बनाएपछि उनी अहिले चर्चित युवा वैज्ञानिक तथा अनुसन्धानकर्ता बन्न सफल भएका छन् । राष्ट्रिय स्तरका थुप्रै सम्मान हात पारिसकेका कार्कीको भविष्यमा नेपालमै बसेर केही गरौँ भन्ने सोच रहेको छ । ‘बाउन्सिङ कार’, ‘आरकिज सिम्पल मेसिन’ (कार्कीको नामाकरणबाट बनाइएको मेसिन च्प्भ्भ्क् जसले ठूलो वजनदार चिजलाई धकेल्न मद्दत गर्छ र इनर्जीलाई स्टोर गर्छ) जस्ता अनुसन्धान र अन्वेषणमा संलग्न उनले पानी, माटो, चट्टानजस्ता पदार्थलाई इन्धनका रूपमा प्रयोग गर्नसकिने तर्क राख्दै आएका छन् । आफ्नै वरिपरिका वस्तुलाई वैज्ञानिक प्रयोगको माध्यमबाट अमेरिका पुगेर स्टुडेन्ट एचिभमेन्ट एवार्ड हात पार्न सफल कार्कीले काठमाडौँको बुढानिलकण्ठ स्कुलबाट विद्यालय तह र बानेश्वरको नोबेल एकेडेमीबाट दश जोड दुई तह उत्तीर्ण गरेका थिए । कार्की हाल अन्तर्राष्ट्रिय एक नेपाली संस्था र अमेरिकाको वेल्स् माउन्टेण्ड फण्डको सहयोगबाट काठमाडौँ विश्वविद्यालयमा अध्ययनरत छन् । फण्डले चार वर्षका लागि वार्षिक एक हजार २०० अमेरिकी डलर उपलव्ध गराएको छ । भविष्यमा भौतिकशास्त्रमा अनुसन्धान गर्ने सोच राखेका कार्की सो विश्वविद्यालयमा एप्लाइड फिजिक्समा बिएससी पहिलो वर्षमा अध्ययनरत छन् ।
विज्ञानमा न्युटनको गतिसम्बन्धी तीन वटा नियम पढेपछि आफू अध्ययनप्रति लगनशील बन्दै गएको बताउने कार्कीले हालसम्म (हिलट्रेन) पहाडी रेल बनाएर सफलतापूर्वक परीक्षण गरिसकेका छन् । उनले पछिल्लो समय गत १० महिनादेखि बाउन्सिक कार (उफ्रेर हिँड्ने कार) को डिजाइनबारे अध्ययन गरिरहेका छन् ।
सानो छँदा पढाइमा खासै चासो नराख्ने कार्कीले पिताको शिक्षाको प्रेरणाबाट क्रमशः आफ्नो अध्ययनलाई खार्दै लगे । कार्कीका बाबु, आमा र एक दाईसहित चार जनाको परिवार छ । धेरै बोलेरभन्दा पनि केही गरेरै देखाउने सोच बोकेका कार्कीले भने–‘अब गफ गरेर हैन, काम गरेर देखाउने जमाना छ, हामीजस्ता युवा वैज्ञानिक देशको विकासमा समर्पित हुनुपर्छ ।’ राम्रो काम गर्न चाहने योग्य मान्छे र आफ्नोभन्दा अरूका लागि सोच्ने व्यक्ति अहिलेको मुलुकको आवश्यकता हो भन्दै कार्की आफूजस्ता धेरै युवा वैज्ञानिक आफ्नै देशमा बसेर केही गर्न चाहेता पनि अध्ययन–अनुसन्धान गर्ने स्रोत र पूर्वाधार अभाव भएको बताउँछन् । विश्व ब्रह्माण्डका बारेमा समेत अन्वेषणरत कार्कीको हरेक प्रयास र सफलतामा आम नेपालीले साथ दिएमा विश्वसामु नेपालको पहिचान बढाउन थप प्रेरणा मिल्ने विश्वास गरिएको छ ।
हुनतः वैज्ञानिक सफलताका लागि विकसित मुलुक नै धाउनुपर्ने नेपाली सोच पछिल्लो समयमा आएर फेसनकै रूपमा अघि बढिरहेको पाइन्छ । प्रतिभावान व्यक्तिले आफ्नो सीप र क्षमतालाई उपलब्ध स्रोत र साधनलाई पछ्याउँदै सही ढङ्गले प्रयोग गर्नसके आफ्नै देशमा पनि केही गर्न सकिँदोरहेछ भन्ने दरिलो उदाहरण बनेका छन्–रिजनजस्ता युवा वैज्ञानिक ।
राज्यले नै युवा वैज्ञानिकका प्रतिभालाई प्रस्फुटन गर्ने उचित वातावरण बनाउनसके भविष्यमा विज्ञान र प्रविधिका क्षेत्रमा नेपालले पनि ठूलो उपलब्धि हासिल गर्नसक्ने सम्भावना देखिएको छ । युवा वैज्ञानिकलाई प्रोत्साहन दिई स्वदेशमै बस्ने वातावरण बनाउनका लागि सरकारी एवम् सरोकारवाला निकायबाट विभिन्न प्रयास भने नभएका हैनन् तर त्यसले सार्थकता पाउनसकेको छैन । सम्बद्ध मन्त्रालय स्वयम् आफँैमा अन्योल हुने र विज्ञान तथा प्रविधिको आधिकारिक निकाय नेपाल विज्ञान तथा प्रविधि प्रज्ञा–प्रतिष्ठान (नास्ट)को सुस्तताले पनि विज्ञान र प्रविधिका क्षेत्रमा उपलब्धिमूलक कार्य हुननसकिरहेको महसुस गरिएको छ ।
विज्ञान, प्रविधि तथा वातावरण मन्त्रालयका सह–सचिव एवम् प्रवक्ता गोकर्णमणि दुवाडीले विज्ञान र प्रविधिको विकास तथा युवा प्रतिभाको पलायनलाई रोक्न मन्त्रालय लागिपरेको बताउनुभयो । मन्त्रालयले हाल त्रिवर्षीय सुधार योजना बनाई कार्यान्वयन चरणमा रहेको र योजनामा प्रतिभा पलायनको रणनीति तर्जुमा गर्ने र भइरहेका प्रतिभाको खोजी गरी विद्धत वृत्ति प्रदान गर्नेलगायतका कार्यक्रम रहेकाले आगामी आर्थिक वर्षदेखि यसलाई प्रभावकारी कार्यान्वयनमा ल्याइने दृढता व्यक्त गर्नुभयो ।
नेपालमा विज्ञान र प्रविधिका क्षेत्रमा काम गर्ने नेपाल विज्ञान प्रविधि तथा प्रज्ञा–प्रतिष्ठान (नास्ट) ले बजेटलगायत युवा वैज्ञानिकहरूलाई स्वदेशमै रहेर काम गर्ने वातावरण बनाउनका लागि केही कार्यक्रम त चलाएकै छ तर त्यसले धेरैजसो वैज्ञानिकलाई समेट्न सकेको छैन । नास्टले हाल विज्ञान विषयमा स्नातकोत्तर र प्राविधिक विषयमा स्नातक उपाधि गरेका युवा वैज्ञानिकहरूलाई परिणाममुखी अनुसन्धानका लागि हरेक वर्ष रु.एक लाख सहयोग गर्दै आएको छ भने स्नातकोत्तर उत्तीर्ण गरेका ताजा युवा वैज्ञानिकहरूलाई नास्टकै प्रयोगशालामा एकदेखि दुई वर्षसम्म आवश्यक सुविधासहित अनुसन्धान गर्ने अवसर प्रदान गर्दै आएको बताइएको छ ।
नास्टका उपकुलपति प्राडा.सुरेन्द्रराज काफ्ले विज्ञान र प्रविधिको विकासका साथसाथै युवा वैज्ञानिकलाई प्रोत्साहित गरी स्वदेशमै रहने वातावरण बनाउन नास्टले विभिन्न योजना मन्त्रालयमा पेस गरे पनि आवश्यक बजेट उपलब्ध नभएका कारण काम गर्ननसकिएको बताउनुहुन्छ ।
एकातर्फ मन्त्रालयले नास्टले आवश्यक योजना ल्याउननसकेको बताउने गर्छ भने अर्कातर्फ नास्टले चाहीँ योजना तयार गरेर मन्त्रालयमा पेस गरे पनि आवश्यक बजेट निकासा नभएका कारण काम गर्न नसकेको दाबी गर्दैआएको छ । समग्रमा जे होस् परिवर्तित विश्व परिवेशमा मुलुकको समग्र विकासका लागि विज्ञान र प्रविधिलाई अगाडि बढाउन राज्यले ठोस कदम चाल्नु अति जरुरी भइसकेको छ ।
नास्टले चार वर्षअघि प्रस्तुत गरेको विवरणअनुसार नेपालमा २५ हजार वैज्ञानिक रहेकामा हाल करिब ३५ हजारको हाराहारीमा पुगेको र त्यसमा ८० प्रतिशत युवा वैज्ञानिक छन् । - रासस

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