Computer Science, asked by cidexom240, 7 months ago

A Que , "roll_no" needs to be maintained for roll numbers of students . write the functions in python interesting and deleting a role number from Roll_No. Python help pls

Answers

Answered by fortniteamanid
0

Explanation:

We have already dealt with arrays. Arrays are used to store similar type of data. Have you ever thought if there is any way to store dissimilar data?

Answer is yes. We use structures to store data of different types. For example, you are a student. Your name is a string and your phone number and roll_no are integers. So, here name, address and phone number are those different types of data. Here, structure comes in picture.

Defining a Structure

The syntax for structure is:

struct structure_name

{

data-type member-1;

data-type member-2;

data-type member-3;

data-type member-4;

};

In our case, let's name the structure as 'student'. The members of the structure in our case are name, roll_no and phone_number.

So, our structure will look like:

struct student

{

int roll_no;

char name[30];

int phone_number;

};

Declaration of Structure Variable

Just as we declare variables of type int, char etc, we can declare variables of structure as well.

Suppose, we want to store the roll no., name and phone number of three students. For this, we will define a structure of name 'student' (as declared above) and then declare three variables, say 'p1', 'p2' and 'p3' (which will represent the three students respectively) of the structure 'student'.

struct student

{

int roll_no;

char name[30];

int phone_number;

};

main()

{

struct student p1, p2, p3;

}

Here, p1, p2 and p3 are the variables of the structure 'student'.

We can also declare structure variables at the time of defining structure as follows.

struct student

{

int roll_no;

char name[30];

int phone_number;

}p1, p2, p3;

Now, let's see how to enter the details of each student i.e. roll_no, name and phone number.

Suppose, we want to assign a roll number to the first student. For that, we need to access the roll number of the first student. We do this by writing

p1.roll_no = 1;

This means that use dot (.) to use variables in a structure. p1.roll_no can be understood as roll_no of p1.

If we want to assign any string value to a variable, we will use strcpy as follows.

strcpy(p1.name, "Brown");

Now let's store the details of all the three students.

#include <stdio.h>

#include <string.h>

int main()

{

struct student

{

int roll_no;

char name[30];

int phone_number;

};

struct student p1 = {1,"Brown",123443};

struct student p2, p3;

p2.roll_no = 2;

strcpy(p2.name,"Sam");

p2.phone_number = 1234567822;

p3.roll_no = 3;

strcpy(p3.name,"Addy");

p3.phone_number = 1234567844;

printf("First Student\n");

printf("roll_no : %d\n", p1.roll_no);

printf("name : %s\n", p1.name);

printf("phone_number : %d\n", p1.phone_number);

printf("Second Student\n");

printf("roll_no : %d\n", p2.roll_no);

printf("name : %s\n", p2.name);

printf("phone_number : %d\n", p2.phone_number);

printf("Third Student\n");

printf("roll_no : %d\n", p3.roll_no);

printf("name : %s\n", p3.name);

printf("phone_number : %d\n", p3.phone_number);

return 0;

}

The reason for this is that in C, we cannot equate two strings (i.e. character arrays). If we had written ' p1.name="Brown"; ', that would have given an error. Therefore, by writing strcpy(p1,"Brown"), we are copying the string 'Brown' to the string variable p1.name.

struct student p1 = {1,"Brown",123443}; → This is also one of the ways in which we can initialize a structure. In next line, we are just giving values to the variables and printing it.

Structures use continuous memory locations.

Array of Structures

We can also make array of structures. In the first example in structures, we stored the data of 3 students. Now suppose we need to store the data of 100 such children. Declaring 100 separate variables of structure is definitely not a good option. For that, we need to create an array of structures.

Let's see an example for 5 students.

#include <stdio.h>

struct student

{

int roll_no;

char name[30];

int phone_number;

};

int main()

{

struct student stud[5];

int i;

for(i=0; i<4; i++)

{

printf("Student %d\n",i+1);

printf("Enter roll no. :\n");

scanf("%d", &stud[i].roll_no);

printf("Enter name :\n");

scanf("%s",stud[i].name);

printf("Enter phone number :\n");

scanf("%d", &stud[i].phone_number);

}

for(i=0; i<4; i++)

{

printf("Student %d\n",i+1);

printf("Roll no. : %d\n", stud[i].roll_no);

printf("Name : %s\n", stud[i].name);

printf("Phone no. : %d\n", stud[i].phone_number);

}

return 0;

}

We just wrote p2 = p1 and that's it. All the elements of p1 got copied to p2.

Pointers to Structures

Like we have pointers for int, char and other data-types, we also have pointers pointing to structures. These pointers are called structure pointers.

Similar questions