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

ITP Week 7

This document discusses the use of structures in C programming for efficiently storing complex data types. It covers defining structures, accessing members, initializing structures, and the benefits of using structures over separate arrays. Additionally, it highlights operations on structures, including passing them to functions and using nested structures and arrays of structures.

Uploaded by

Hashem Abdullah
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)
5 views

ITP Week 7

This document discusses the use of structures in C programming for efficiently storing complex data types. It covers defining structures, accessing members, initializing structures, and the benefits of using structures over separate arrays. Additionally, it highlights operations on structures, including passing them to functions and using nested structures and arrays of structures.

Uploaded by

Hashem Abdullah
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/ 10

Summary

This video discusses the importance of using structures in C programming to


efficiently store complex data.
Key Takeaways:
1. Tuple Data: A collection of attributes or features that belong together,
often used to represent a particular entity.
2. Need for Structures: Storing multiple data types (e.g., name, ID num-
ber, GPA) as one unit is not supported by fundamental C data types (int,
char, float, double).
3. Current Representation Issues:
• Using separate arrays/lists for each attribute can lead to cumbersome
deletion/addition operations.
• Maintaining sequence/ordering between multiple arrays/list can be
difficult.
4. Benefits of Structures:
• Storing related data as one unit (e.g., name, ID number, GPA) is
more efficient and easier to manage.
• Using an array of structures allows for easy deletion/addition opera-
tions on a single entity.
Key Points Covered:
1. Introduction to tuple data and its importance in C programming.
2. The need for structures to efficiently store complex data.
3. Current representation issues with using separate arrays/lists for each at-
tribute.
4. Benefits of using structures, including easier deletion/addition operations
and efficient storage management.
Summary: Structures in C
A structure in C is a group of logically related data items that can be stored
together. It’s like defining a new data type to store information about an entity,
such as a student or book.
Key Takeaways:
1. Defining a Structure: Use the keyword struct, followed by the name
of the structure and curly brackets containing individual member declara-
tions (data types and variable names).
2. Declaring Variables: Use struct followed by the name of the structure
and variable names separated by commas, ending with a semicolon.
3. Combining Definition and Declaration: Define the structure at once,
including variable names after the closing curly bracket before the semi-
colon.
Example: Defining a Structure for Student Entity

1
struct student {
int ID;
char name[30];
float GPA;
} s1, s2; // declaring variables of type 'student'
or
struct {
int ID;
char name[30];
float GPA;
}s1, s2; // combining definition and declaration without a structure name
Summary: Structure Operations in C
This video covers various aspects of working with structures in C programming.

Accessing Individual Members of a Structure


• To access an individual member of a structure, use the dot operator (.)
after the name of the variable and then the name of the member.
– Example: s1.name refers to the location where name is stored for s1.
– This allows accessing specific attributes or fields within a structured
data type.

Initializing a Structure
• Initialize a structure by assigning values to its members using curly brack-
ets and commas, in the same order as they are declared.
– Example: struct book { char name[30], price, ISBN; } b1 =
{"Book 1", 550.00, "I123"};
– Each member gets assigned a specific value.

Permissible Operations on Structures


• Arithmetic operations (e.g., addition, subtraction) are not allowed.
• Logical operations (e.g., AND, OR) cannot be performed directly on struc-
tures.
• Relational operations (e.g., equality comparison using ==) should also con-
sider individual members for correctness.

Assignment Operation between Structure Variables


• Assign a structure variable to another of the same type by copying its
individual members: s1 = s2;

2
Comparing Two Structure Variables for Equality
• Use logical AND (&&) with double equals (==) on each member’s value
comparison: if(s1.GPA == s2.GPA && s1.ID == s2.ID ...)
Summary: Accessing Structure Members and Assignment
This C program demonstrates how to define a student data type using structures
in C programming.

Defining Student Data Type


• The struct keyword is used to declare a new structure called student.
• Within the student struct, two fields are defined:
– A character array named name, with a size of 20.
– An integer field named marks, composed of an array containing 2
elements.

Accessing Individual Members


• To access specific members within a structure, use dot notation (.) fol-
lowed by the member’s name.
Summary: Understanding Typedef and Structure Size
This C programming video discusses two essential concepts:

1. Typedef
• The typedef keyword allows users to create synonyms for data types,
making the code more readable.
• It simplifies syntax by avoiding repetition of long data type names.
Example: “‘c typedef int Marks; Marks m1, m2; // equivalent to int m1, m2;
### 2. **Structure Size**

* The `sizeof` operator can compute the size of a variable, including structured variables
* When calculating the size of a structure variable, it is essential to note that the comp

Example:
```c
struct test {
short x;
int y;
char z;
};

printf("%d %d\n", sizeof(short), sizeof(int)); // prints 2 and 4 respectively

3
printf("%d\n", sizeof(test.x)); // prints 2 (size of member 'x')
printf("%d\n", sizeof(test.y)); // prints 4 (size of member 'y')

// Minimum sum of sizes: 7 bytes


// Actual size due to padding: compiler may add more bytes for alignment, resulting in a lar
Nested Structures: Accessing Members with Dot Notation

Key Takeaways:
• Definition: A nested structure consists of a structure within another
structure.
• Accessing Members: To access members of a nested structure, use the
dot notation repeatedly (e.g., st.add.city).
• Example:
– Define two structures: struct address and struct student.
– Create a variable st of type struct student.
– Use strcpy() to input values for each member, including nested
structure members (e.g., st.add.city = "Columbus").
• Compilation:
– Compile the program using GCC (gcc nested.c -o a.out).
– Run the program and observe output.

Summary:
This video demonstrates how to work with nested structures in C program-
ming, highlighting their utility for complex data types. The key takeaway is
understanding how to access members of a nested structure using repeated dot
notation.
Summary: Array of Structures

Key Takeaways:
• Definition: An array of structures is a collection of variables of the same
type, which is a struct type.
• Declaring Arrays: To declare an array of structures, define the structure
first and then use square brackets [] to specify its size. For example:
struct student s[3].
• Accessing Members: Use dot notation repeatedly (e.g., s[i].name,
s[i].marks[j]) to access members of individual elements in an array.
• Example:
– Define a structure for students (struct student).
– Declare an array of 3 students: struct student s[3].
– Use nested dot notation (e.g., s[i].marks[j]) to access marks of
individual students.

4
Example Code:
// Structure definition
struct student {
char name[50];
double CGPA;
int marks[3];
};

int main() {
// Declare an array of 3 students
struct student s[3];

// Loop through each student to take input for:


for (int i = 0; i < 3; i++) {
printf("Enter name, CGPA, and three marks for student %d:\n", i + 1);
scanf("%49s%lf%d%d%d", s[i].name, &s[i].CGPA, &s[i].marks[0], &s[i].marks[1], &s[i].
}

return 0;
}
This code demonstrates how to declare an array of structures and access indi-
vidual members using nested dot notation.
Summary
In this video, we explored how to use an array of structures in C programming
to store and manage data, specifically for a student database.
Key Takeaways:
1. Defining a Structure: We defined a struct student with three at-
tributes: ID number, name, and GPA.
2. Initializing the Database: We initialized an array of structures with
3 elements (students), each containing their respective data (ID number,
name, and GPA).
3. Iterating through the Database: We used a for loop to iterate through
the database and print the names of students having a GPA greater than
9.3.
4. Accessing Student Data: Within the loop, we accessed each student’s
data using list_of[i].name and list_of[i].GPA.
5. Compiling and Running the Code: We compiled and ran the code to
test its functionality.
Code Snippet
struct student {
int ID;

5
char name[20];
float GPA;
};

#define NUM 3

int main() {
struct student list_of[NUM];

// Initialize database
list_of[0].ID = 0910; list_of[0].name = "Rama Sharma"; list_of[0].GPA = 9.25;
list_of[1].ID = 0842; list_of[1].name = "Alex Mathew"; list_of[1].GPA = 8.50;
list_of[2].ID = 1234; list_of[2].name = "Vijay Gaur"; list_of[2].GPA = 9.81;

// Print names of students with GPA > 9.3


for (int i = 0; i < NUM; i++) {
if (list_of[i].GPA > 9.3)
printf("%s\n", list_of[i].name);
}

return 0;
}
Summary
This transcript discusses how structures interact with functions in C program-
ming.
Key Takeaways:
1. Passing Structures by Value: When passing a structure to a function,
a local copy of the entire structure is made, which can be modified within
the function.
2. Local Copy Mechanism: The mechanism of creating a local copy en-
sures that any changes made to the structure within the function do not
affect the original structure outside the function.
3. Passing Arrays by Reference: Passing arrays (or structures) to func-
tions creates a ”pass-by-reference” effect, where only the memory location
of the array is copied, allowing modifications within the function to be
reflected in the original array.
Example Walkthrough
1. A complex number struct called complex with components re and im.
2. Inputting two complex numbers a and b, calling a function add to compute
their sum, storing it in c.
3. When the add function is called:
• Individual members of a are copied onto parameters x, creating local
copies.

6
• Similarly for b copying onto parameter y.
4. Inside the function: A new complex number t with real part summing
reals and imaginary parts summing imaginaries, then returning value of t
to variable c.
Key Points
1. Local Copy Mechanism: No changes made within a called function
affect the original structure outside.
2. Passing Arrays by Reference: Only memory location copied; modifi-
cations reflect in original array.
3. Structure and Function Interaction: Essential for understanding C
programming concepts, especially when using structures as data types.
Graphical Representation
An analogy was drawn to visualize how pass-by-value works:
C = Add(A,B)
1. Copying values from A & B onto X & Y
2. Adding real parts of X&Y; storing in t’s real part
3. Returning value T, which substitutes for variable C
Meeting Summary
The meeting focused on discussing and implementing structures in C program-
ming for coordinate geometry problems.
Key Takeaways:
• A structure was defined to represent grid points on a 2D plane with x and
y components.
• Function prototypes were created:
– FindQ: returns the quadrant of a given point (1st, 2nd, 3rd, or 4th
quadrant, or axis).
– SameQuadrants: checks if two given points are in the same quadrant.
• The main function was written to test these functions with various points:
– Points were created for each quadrant and one on the axis.
– Function calls were made to display the results.
Code Implementation:
The code was successfully compiled, and output showed that:
• Point p1 (1, 1) lies in the first quadrant.
• Point p2 (-1, 1) lies in the second quadrant.
• Points p3 (-1, -1), p4 (1, -1), and p5 (0, 5) lie in the third quadrant, fourth
quadrant, and on the axis respectively.
Conclusion:

7
The meeting successfully demonstrated how structures can be used to represent
points on a 2D plane and create functions to answer simple geometry-related
questions.
Meeting Summary
The meeting focused on discussing and implementing structures in C program-
ming for coordinate geometry problems.
Key Takeaways:
• A structure was defined to represent grid points on a 2D plane with x and
y components, called Point.
• Function prototypes were created:
– distance: returns the Euclidean distance between two given points
(sqrt(x1^2 + y1^2 - x2^2 - y2^2)).
– collinear: checks if three given points are collinear by computing
the area using the determinant.
• The main function was written to test these functions with various points:
– Points p1, p2, and p3 were created for testing distance and collinear-
ity.
Code Implementation:
The code was successfully compiled and output showed that:
• Distance between p1 (0, 0) and p3 (10, 10) is approximately 14.14.
• Collinearity of points p1, p2, and p3 returned true because they lie on a
straight line.
• Collinearity of points p1, p2, and p4 returned false because they do not
lie on a straight line.
Conclusion:
The meeting successfully demonstrated how structures can be used to represent
points on a 2D plane and create functions to answer simple geometry-related
questions. The distance function correctly calculated the Euclidean distance
between two points, and the collinear function correctly checked if three given
points are collinear.
The code provided in this video is designed to print the names and dates of
students who joined during 2020 or later.
Here is the corrected C program:
#include <stdio.h>

typedef struct {
int day;
int month;
} Date;

8
struct Student {
char name[20];
int idnum, score[3], dateyear;
};

int main() {

struct Student list[] = {


{"John", 100, {60,70}, "21 Jan" ,2021},
{"Ram ",101,{75},15Sep 2019 },
{"Francis",102,{95},{07Dec}2018},
{"Gopal",103,{89},{12Apr }2012}
};

int i;
for (i = 0; i < sizeof(list)/sizeof(list[0]); ++i) {
if (list[i].dateyear >=2020) {
printf("%s, %d, ", list[i].name,list[i].idnum);
printf("%2d/%-2.2s/%4d\n", // day/month/year
list[i].score[1], // marks for quiz #1
list[i].dateyear); // year of joining
}
}

return 0;
}
Note: The structure Date is defined outside the main function as per C99 rules.
The program defines a date struct and uses it within another student’s struct
to store their birthdate. It then iterates over each student in the list, checks
if they joined during or after 2020, and prints out their name and joining year
along with other details (excluding actual code here due to complexity).
Summary
This module introduces the concept of structures in C programming language.
Key Takeaways:
1. User-Defined Data Types: Structures allow users to create new data
types that can store multiple attributes of a particular entity, such as
name, roll number, and CGP for a student.
2. Defining Structures: The struct keyword is used to define a structure,
with each member separated by commas within curly braces {}.
3. Accessing Individual Members: Structure variables can be accessed
using the dot operator (.), followed by the individual member name (e.g.,
structure.member).

9
4. Typedefs: The typedef keyword allows users to create a new name for
an existing data type, making code more readable and concise.
5. Nested Structures: One structure can be embedded within another,
useful when storing dates with multiple members (day, month, year).
6. Arrays of Structures: A continuous array of structured variables can
store multiple entities, similar to arrays of integers.
Key Concepts:
1. User-defined data types
2. Defining structures using the struct keyword
3. Accessing individual structure members with the dot operator (.)
4. Using typedefs for concise code
5. Nested structures and array of structured variables

10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy