logo codesdope
Level 1
1.
Write a program to store and print the roll no., name , age and marks of a student using structures.
Answer
#include<iostream>

int main()
{
  struct student
  {
    int roll_no;
    char name[30];
    int age;
    int marks;
  };
  struct student p1 = {1,"Brown",14,78};
  cout << p1.roll_no << " " << p1.name << " " << p1.age << " " << p1.marks << "\n";
  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.
Answer
#include<iostream>

int main()
{
  struct student
  {
    int roll_no;
    string name;
    int age;
  };

  struct student stud[5];

  for(int i=0; i<=4; i++)
  {
    cout << "Student " << i+1 << endl;
    stud[i].roll_no = i+1;
    cout << "Enter name :\n";
    cin >> stud[i].name;
    cout << "Enter age :\n";
    cin >> stud[i].age;
  }

  for(int i=0; i<=4; i++)
  {
    if(stud[i].roll_no == 2)
    {
      cout << "Student " << i+1 << endl;
      cout << "Roll no. : " << stud[i].roll_no << endl;
      cout << "Name : " << stud[i].name << endl;
      cout << "Age : " << stud[i].age << endl;
    }
  }

  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.
Answer
#include<iostream>

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

  cout << d.feet << " feet and " << d.inch << " inch" << endl;
  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.
Answer
#include<iostream>

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

  cout << "ADD - " << d.real << "+" << d.imag << "i\n";
  cout << "SUB - " << e.real << "+" << e.imag << "i\n";
  cout << "MUL - " << f.real << "+" << f.imag << "i\n";

  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.
Hint
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 2
1.
Write a structure to store the names, salary and hours of work per day of 10 employees in a company. Write a program to increase the salary depending on the number of hours of work per day as follows and then print the name of all the employees along with their final salaries.
Hours of work per day 8 10 >=12
Increase in salary $50 $100 $150

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)
Hint
#include<iostream>

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


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

void add()
{
  //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.
Create a structure named Date having day, month and year as its elements. Store the current date in the structure. Now add 45 days to the current date and display the final date.

Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.