0% found this document useful (0 votes)
61 views6 pages

Structures: 1: P. P. Chakrabarti

The document discusses using structures in C programming to store and process student record data. It defines a structure with fields for student name, roll number, semester GPAs, and overall CGPA. It shows how to input student data, calculate CGPAs by averaging GPAs, print student records, and sort records by CGPA. The examples demonstrate declaring arrays of structures to store multiple student records and manipulating structure fields and elements.
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)
61 views6 pages

Structures: 1: P. P. Chakrabarti

The document discusses using structures in C programming to store and process student record data. It defines a structure with fields for student name, roll number, semester GPAs, and overall CGPA. It shows how to input student data, calculate CGPAs by averaging GPAs, print student records, and sort records by CGPA. The examples demonstrate declaring arrays of structures to store multiple student records and manipulating structure fields and elements.
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/ 6

Structures: 1

P. P. Chakrabarti

06-03-03

P.P.Chakrabarti, IIT Kharagpur

A first example:
main()
{
struct {
float real;
float imag;
} a,b,c;
scanf("%f%f", &a.real, &a.imag);
scanf("%f%f", &b.real, &b.imag);
c.real = a.real + b.real;
c.imag = a.imag + b.imag;
printf(" a = %.2f+ i %.2f \n", a.real, a.imag);
printf(" b = %.2f+ i %.2f \n", b.real, b.imag);
printf(" c = %.2f+ i %.2f \n", c.real, c.imag);
}

06-03-03

This defines the type of


variables a, b and c as
a composite of two floats

[ppchak]$ ./a.out
2.3
4.5
1.9
6.7
a = 2.30+ i 4.50
b = 1.90+ i 6.70
c = 4.20+ i 11.20

P.P.Chakrabarti, IIT Kharagpur

A slight variation:

Each element a[i] contains


a real and an imag.

main()
{ struct { float real; float imag; } a[10], c;
int i,n;
scanf("%d",&n);
for (i=0; i<n; i++)
scanf("%f%f", &a[i].real, &a[i].imag);
c.real = 0;
c.imag = 0;
for (i=0; i<n; i++)
{ c.real += a[i].real;
c.imag += a[i].imag;
}
for (i=0; i<n; i++)
printf("a[%d] = %.2f +i %.2f\n", i, a[i].real, a[i].imag);
printf(" c = %.2f+ i %.2f \n", c.real, c.imag);
}

06-03-03

[ppchak]$ ./a.out
4
2.3 4.5
1.7 8.9
2.1 3.1
4.5 6.1
a[0] = 2.30 +i 4.50
a[1] = 1.70 +i 8.90
a[2] = 2.10 +i 3.10
a[3] = 4.50 +i 6.10
c = 10.60+ i 22.60

P.P.Chakrabarti, IIT Kharagpur

Student Information:

char
charname[20];
name[20];
int
introll;
roll;
float
floatSGPA[8];
SGPA[8];
float
floatCGPA;
CGPA;

main()
{
struct{
for (i=0; i<n; i++) {
char name[20];
printf("%d. %s ", i, student[i].name);
int roll;
printf("%d\n", student[i].roll);
float SGPA[8];
printf(" SGPA = ");
float CGPA;
for(j=0;j<8;j++)
} student[10];
printf("%.2f, ", student[i].SGPA[j]);
int i,j,n;
printf("\n CGPA = %.2f\n", student[i].CGPA);
scanf("%d", &n);
}
for (i=0; i<n; i++) {
}
scanf("%s", student[i].name);
scanf("%d", &student[i].roll);
for(j=0;j<8;j++) scanf("%f", &student[i].SGPA[j]);
}
for (i=0; i<n;i++) {
student[i].CGPA = 0;
for (j=0; j<8;j++) student[i].CGPA += student[i].SGPA[j];
student[i].CGPA /= 8;
}

06-03-03

P.P.Chakrabarti, IIT Kharagpur

Run Output:
[ppchak]$ ./a.out
3
Rakesh
123
9.1 8.2 9.5 8.7 8.8 9.6 8.9 9.4
Anita
134
9.0 9.0 9.0 9.0 8.0 8.0 9.0 9.0
Prasenjit
145
7.5 7.5 7.5 6.5 6.5 6.5 5.5 5.5
0. Rakesh 123
SGPA = 9.10, 8.20, 9.50, 8.70, 8.80, 9.60, 8.90, 9.40,
CGPA = 9.03
1. Anita 134
SGPA = 9.00, 9.00, 9.00, 9.00, 8.00, 8.00, 9.00, 9.00,
CGPA = 8.75
2. Prasenjit 145
SGPA = 7.50, 7.50, 7.50, 6.50, 6.50, 6.50, 5.50, 5.50,
CGPA = 6.62

06-03-03

P.P.Chakrabarti, IIT Kharagpur

Sorting the records:


struct{
char name[20];
int roll;
float SGPA[8];
float CGPA;
} student[10], temp;

for(i=0; i<n;i++)
for(j=i; j<n; j++)
if (student[i].CGPA < student[j].CGPA)
{
temp = student[i];
student[i] = student[j];
student[j] = temp;
}

06-03-03

P.P.Chakrabarti, IIT Kharagpur

You might also like