0% found this document useful (0 votes)
24 views

Creating and Manipulating Structure of Similar Data Types (Example 1)

The document discusses 5 problems involving defining and manipulating structures in C programming. Problem 1 defines a Point structure to store x and y coordinates and calculates distance between two points. Problem 2 defines a structure to store height and finds difference between two heights. Problem 3 defines a BankAccount structure to store name and balance and determines the richer account. Problem 4 defines a Student structure, takes a student's info as input, and uses a function to determine their grade. Problem 5 defines a Student structure, takes n students' info as input, and uses a function to find the highest mark.

Uploaded by

Asif Bin Latif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Creating and Manipulating Structure of Similar Data Types (Example 1)

The document discusses 5 problems involving defining and manipulating structures in C programming. Problem 1 defines a Point structure to store x and y coordinates and calculates distance between two points. Problem 2 defines a structure to store height and finds difference between two heights. Problem 3 defines a BankAccount structure to store name and balance and determines the richer account. Problem 4 defines a Student structure, takes a student's info as input, and uses a function to determine their grade. Problem 5 defines a Student structure, takes n students' info as input, and uses a function to find the highest mark.

Uploaded by

Asif Bin Latif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Creating and manipulating structure of similar data types (Example 1)

/*Problem 1: Write down a program that defines a structure to store


x and y coordinate of a point in 2-dimensional space. Then take x, y
coordinates of TWO points as user input and find Euclidian distance
between them. */

#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)

/* Problem 2: Write down a program that defines a structure to store


height of a person. Then take two persons’ height as input and find
their height difference. */

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

/*Problem 3: Write down a program that defines a structure to store


information of a Bank Account. Then take name and balance of two
persons as user input and find the richer person. */

typedef struct {
char name[40];
double b;
} bankAccount;

main(){
bankAccount b1,b2;

printf("Enter name of first Person: ");


gets(b1.name);
printf("Enter balance: ");
scanf("%lf%*c",&b1.b);
printf("Enter name of second Person: ");
gets(b2.name);
printf("Enter balance: ");
scanf("%lf",&b2.b);

if(b1.b > b2.b)


printf("%s is richer",b1.name);
else if (b2.b > b1.b)
printf("%s is richer",b2.name);
else
printf("They are equally rich!",b2.name);

}
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;

char findGrade(student p){


char g;

if(p.m >= 90) g = 'A';


else if (p.m >= 80) g = 'B';
else if (p.m >= 70) g = 'C';
else if (p.m >= 60) g = 'D';
else g = 'F';
return g;
}

main(){
student s;

printf("Enter ID: ");


scanf("%d",&s.id);
printf("Enter Mark: ");
scanf("%f",&s.m);
s.g = findGrade(s);

printf("ID: %d Mark: %f Grade: %c", s.id, s.m, s.g);


}
Creating arrays of structure and passing the array as a

parameter to a function

/*Problem 5: Write down a program that defines a structure to store a


student’s information in a university. Then take n students’
information as user input and find highest mark using a user defined
function. */

typedef struct{
int id;
float m;
char g;
} student;

float findHighest(student p[],int n){


float h;
int i;
h = p[0].m;
for(i = 1; i < n; i++){
if (h < p[i].m)
h = p[i].m;
}
return h;
}

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);
}

You might also like