Creating and Manipulating Structure of Similar Data Types (Example 1)
Creating and Manipulating Structure of Similar Data Types (Example 1)
#include <math.h>
typedef struct {
double x;
double y;
} Point;
main(){
Point p1,p2;
double dx,dy,d;
p1.x = 5;
p1.y = 4;
printf("Enter first point:\n x: ");
scanf("%lf",&p1.x);
printf("y: ");
scanf("%lf",&p1.y);
printf("Enter second point:\n x: ");
scanf("%lf",&p2.x);
printf("y: ");
scanf("%lf",&p2.y);
dx = p1.x - p2.x;
dy = p1.y - p2.y;
d = sqrt(dx*dx+dy*dy);
printf("Distance: %lf",d);
}
Creating and manipulating structure of similar data types (Example 2)
typedef struct {
int f;
int i;
}height;
main(){
int d;
height p1,p2,r;
printf("Enter height of first person:\n Feet: ");
scanf("%d",&p1.f);
printf("Inch: ");
scanf("%d",&p1.i);
printf("Enter height of second person:\n Feet: ");
scanf("%d",&p2.f);
printf("Inch: ");
scanf("%d",&p2.i);
d = (p1.f - p2.f)*12 + p1.i - p2.i;
if (d < 0) d = -d;
r.f = d/12;
r.i = d%12;
printf("Height difference %d feet %d inch",r.f,r.i);
}
Creating and manipulating structure of dissimilar data types
typedef struct {
char name[40];
double b;
} bankAccount;
main(){
bankAccount b1,b2;
}
Creating structure of dissimilar data types and passing as a
parameter to a function
/*Problem 4: Write down a program that define a structure to store
the information of a student in a university. Then take a student’s
information as user input and find his grade using a user defined
function. */
typedef struct {
int id;
float m;
char g;
} student;
main(){
student s;
parameter to a function
typedef struct{
int id;
float m;
char g;
} student;
main(){
int n,i;
float h;
printf("How Many students?");
scanf("%d",&n);
student s[n];
for(i =0; i <n; i++){
printf("Enter ID: ");
scanf("%d",&s[i].id);
printf("Enter Mark: ");
scanf("%f",&s[i].m);
}
h = findHighest(s,n);
printf("Highest marks: %.2f",h);
}