×
 Introduction
Introduction to Newbie Programmer Series Introduction to Computer The Computer Programming Language Setting up the Computer for Programming
 Lets Get Started !
Beginning C Programming Variables a Handbag ! Basic Variable types in C Basic Formatted Output Basic Formatted Input
 Loops
Looping Repeating Concept Different Kinds of Loops Advanced concepts and trick with Loops
 Operators
Operators, An Introduction Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Miscellaneous Operators and Operators Precedence
 Controlling the Flow
Flow Charts Conditional Statements Using Goto and Label Break and Continue the loops Switch the Cases Some Practical Applications of Control Flow
 The program structure
The program Structure Functions More Concepts of Functions Scope of the Variables across functions How the C language itself Works ? Scope of the Variables across files Static Variables Auto and Register Variables #define PreProcessor Some more PreProcessors
 Arrays and Structures
Introduction to Arrays Arrays, some more Concepts Arrays, Cool Examples Structures Introduction Structure, Some more Concepts Structure, Some Examples
 File Management
Basics of File management in C Steps Involved with File Handling in C
 Miscellaneous
Inside Logic Gates – The Electronic Logic Binary Number System Mathematical Reasoning
Newbie Programmer
full book coming soon
by Shubham Ramdeo

That's how we play with structures Practically !

structureWelcome to the Newbie Programmer Series. In the last part, we have covered everything important to know about structure. So I am quite confused what to discuss here ! Well let us do two examples explaining all the usage of structure so that you catch if you are having some trouble. So if you are new here, please check out all the previous parts (click here) to easily understand what is going down below.


Finding distance between 2 points

[caption id="" align="aligncenter" width="288"]distance fomula The Distance Formula[/caption]

Let us make a simple program but with many old concepts revised. Say we have 2 points p1 and p2. To find the distance between two points, we use the formula :In our programming way, we can write :

d = sqrt( pow( (x2 - x1), 2) + pow( (y2 - y1), 2);
NOTE : If you cannot get it, don't worry. Do not concentrate and get scared by the formula. Do not think what that is. That's the work of mathematicians. Our work is about  how to implement that in programming. Here we have used two functions, sqrt to find square root and pow to find power. Both of this are available in "math.h" library. So we will simply include them. Now check the complete code below.
#include "stdio.h" 
#include "math.h" #define ADDPOINT(NAME, X, Y) point NAME; NAME.x=X; NAME.y=Y; /*First, let us create a global definition of point outside allbthe functions so that it can be accessed by all the functions.*/ typedef struct { int x; int y; } point;
/*our distance formula */ float distance( point p1, point p2 ) { float d = sqrt( pow( (p2.x - p1.x), 2) + pow( (p2.y - p1.y), 2); return d; }
main () { ADDPOINT (A, 2, 3); ADDPOINT (B, 5, 7); float dist = distance (A, B); printf("The distance between points A and B is %f", dist); printf("press enter to exit"); getchar(); }

If you found it hard to understand what we have a done here with functions, please revise the functions and variables parts again so that you can understand how easy the above code is. I have simply include the required library files, then made the ADDPOINT macro to make the points easily. Then I described what a "point" is. Then I created a function called "distance". It calculate the distance and return it. I have used floats values here so that we can get the exact values in decimals. And then the main function use all of them to print the answer. Simple.

So many nested structures

Let us do an example to illustrate the use of multiple nested structs. Let us put a lot of real life objects, like GTA !
#include "stdio.h"
main()
{

/* These below are the interconnected structs.*/

typedef struct {
char name[80];
int birth;
} person;

typedef struct {
int price;
int pages;
person author;
} book;

typedef struct {
int year;
book textbooks[10];
person librarian;
} library;

/* Now using them */

//Making a Library
library schoolLibrary;

//Now giving it details
schoolLibrary.year = 2016;
schoolLibrary.librarian.name = "John the Great";
schoolLibrary.textbook[1].name = "Plane Trigonometry";
schoolLibrary.textbook[1].author.name = "S L Loney";

//using them, instead of writing long struct, assign a variable
int bdate = schoolLibrary.textbook[1].author.birth = 1860;
printf("%d", bdate);

//Not only limited to those big structs, make smaller as
person john;
john.birth = "1990";
}
The above example is self-explanatory, I have used a lot of comments in them. I think that two are enough as we have discussed a lot of examples in the previous posts so need of more. And here the topic of structure ends. From the next post we will start something new. So stay connected.

Please share this as much as you can so that we can get connected with more and more people who want to learn programming.

© Shubham Ramdeo

Site Map
with
© Shubham Ramdeo