# Practice questions on Structure

## Level 1

1.
Understand the code of playing card game given in the chapter.

## Level 2

1.
Write a program to store and print the roll no., name , age and marks of a student using structures.
```#include <stdio.h>
int main()
{
struct student
{
int roll_no;
char name[30];
int age;
int marks;
};
struct student p1 = {1,"Brown",14,78};
printf("%d %s %d %d\n",p1.roll_no,p1.name,p1.age,p1.marks);
return 0;
}
```

2.
Write a program to store the roll no. (starting from 1), name and age of 5 students and then print the details of the student with roll no. 2.
```#include <stdio.h>
int main()
{
int i;
struct student
{
int roll_no;
char name[30];
int age;
};

struct student stud[5];

for(i=0; i<=4; i++)
{
printf("Student %d\n",i+1);
stud[i].roll_no = i+1;
printf("Enter name :\n");
scanf("%s",stud[i].name);
printf("Enter age :\n");
scanf("%d", &stud[i].age);
}
for(i=0; i<=4; i++)
{
if(stud[i].roll_no == 2)
{
printf("Student %d\n",i+1);
printf("Roll no. : %d\n", stud[i].roll_no);
printf("Name : %s\n", stud[i].name);
printf("Age. : %d\n", stud[i].age);
}
}
return 0;
}
```

3.
Write a program to store and print the roll no., name, age, address and marks of 15 students using structure.

4.
Write a program to add two distances in inch-feet using structure. The values of the distances is to be taken from the user.
```#include <stdio.h>

struct dist
{

int feet;
int inch;
};

struct dist add(struct dist a, struct dist b)
{
struct dist d;
d.feet = a.feet+b.feet;
if(a.inch+b.inch >= 12)
{
d.feet = d.feet+((a.inch+b.inch)/12);
d.inch = (a.inch+b.inch)-(((a.inch+b.inch)/12)*12);
}
else
{
d.inch = a.inch+b.inch;
}
return d;
}

int main()
{

struct dist d1 = {12,2};
struct dist d2 = {14,11};

struct dist d = add(d1,d2);

printf("%d feet and %d inch\n",d.feet,d.inch);

return 0;
}
```

5.
Enter the marks of 5 students in Chemistry, Mathematics and Physics (each out of 100) using a structure named Marks having elements roll no., name, chem_marks, maths_marks and phy_marks and then display the percentage of each student.

6.
Write a program to add, subtract and multiply two complex numbers using structures to function.
```#include <stdio.h>

struct complex
{

int real;
int imag;
};

struct complex add(struct complex a, struct complex b)
{
struct complex d;
d.real = a.real+b.real;
d.imag = a.imag+b.imag;
return d;
}

struct complex sub(struct complex a, struct complex b)
{
struct complex d;
d.real = a.real-b.real;
d.imag = a.imag-b.imag;
return d;
}

struct complex multiply(struct complex a, struct complex b)
{
struct complex d;
d.real = (a.real*b.real)-(a.imag*b.imag);
d.imag = (a.real*b.imag)+(a.imag*b.real);
return d;
}

int main()
{

struct complex d1 = {12,2};
struct complex d2 = {14,11};

struct complex d = add(d1,d2);
struct complex e = sub(d1,d2);
struct complex f = multiply(d1,d2);

printf("SUB - %d+%di\n",e.real,e.imag);
printf("MUL - %d+%di\n",f.real,f.imag);

return 0;
}
```

7.
Write a structure to store the roll no., name, age (between 11 to 14) and address of students (more than 10). Store the information of the students.
1 - Write a function to print the names of all the students having age 14.
2 - Write another function to print the names of all the students having even roll no.
3 - Write another function to display the details of the student whose roll no is given (i.e. roll no. entered by the user).

8.
Write a structure to store the name, account number and balance of customers (more than 10) and store their information.
1 - Write a function to print the names of all the customers having balance less than \$200.
2 - Write a function to add \$100 in the balance of all the customers having more than \$1000 in their balance and then print the incremented value of their balance.
Use array of structures.
Functions will take the array of structures

9.
Write a program to compare two dates entered by user. Make a structure named Date to store the elements day, month and year to store the dates. If the dates are equal, display "Dates are equal" otherwise display "Dates are not equal".

## Level 3

1.

2.

### Let us work on the menu of a library. Create a structure containing book information like accession number, name of author, book title and flag to know whether book is issued or not. Create a menu in which the following can be done. 1 - Display book information 2 - Add a new book 3 - Display all the books in the library of a particular author 4 - Display the number of books of a particular title 5 - Display the total number of books in the library 6 - Issue a book (If we issue a book, then its number gets decreased by 1 and if we add a book, its number gets increased by 1)

```#include <stdio.h>
#include <string.h>

struct book
{
int an;
char title[30];
char author[30];
int issued;
};

void display(struct book b)
{
printf("Accession number-\t%d\nBook-\t%s\nAuthor-\t%s\n",b.an,b.title,b.author);
if(b.issued == 0)
{
printf("Issued-\tNo\n");
}
else
{
printf("Issued-\tYes\n");
}
}

{
//Do yourself
//issued will be 0 by defalut
}

//passing array
void book_by_author(struct book *b,int number_of_books,char auth[])
{
int i;
for(i=0;i<number_of_books;i++)
{
if(strcmp((b+i)->author,auth))
{
display(*(b+i));
}
}
}

void book_by_title()
{
//do it yourself
}

void issue_a_book(struct book b)
{
b.issued =1;
}

int main()
{
//write yourself
return 0;
}
```

3.