give me solutions for chapter 13 structures ( C++) from class 11
Answers
Answered by
0
Class 11 Structure Sumita Arora Solved Assignment
Uploaded by Sathiya Ramesh
CHAPTER-13 STRUCTURES SHORT ANSWER QUESTIONS
1. Discuss the similarity and differences of a structure as compared to an array and a class. Ans. Similarities array and Structure
Both structures and arrays are used to hold more than 1 elements under single name.
Difference array structure
Arrays bring together a group of items of the same data type
Structures bring together a group of related data items of any data type.
Similarities class and structure
Both Class and Structures can have methods, variables and objects.
Both can have constructor.
Both are user defined types.
Difference class structure
Declared with the keyword “class”
Declared with the keyword “struct”
By default, all members are “private” in a class
By default, all members are “public” in a structure
2. Define a structure to store information about a fish. The structure should include the kind, the weight in grams, and the length in inches. Declares variables of this structure type and discuss various methods of initializing them. Ans.
struct Fish_Info { char kind[50]; int gram; int length; }; Fish_Info fish;
first method of initializing
The structure elements of a structure can be initialized separately, using separate assignment statement. fish.kind=”abc”; fish.gram=300; fish.length=5;
second method of initializing
The structure element of a structure can be initialized jointly, using the notation similar to array initialization. Fish_Info fish = { “abc”, 300, 5);
3. Write definition for a structure EMPREC that stores information about an employee such as empno, name, address, salary, and joining_date. The address member of EMPREC stores the information houseno, area, and city. The joining_date member of EMPREC stores information day, month, and year. Ans.
struct date { int day; int month; int year; }; struct addr
1
{ int houseno; char area[31]; char city[31]; }; struct EMPREC { int empno; char name[41]; addr address; float salary; date joining_date; };
4. What is structure? Declare a structure in C++ with name, roll number and total marks as components. Ans.
A structure is a collection of variables having different data types. The declaration as: struct student { char name[20]; int roll_no,marks; };
5. Rewrite the following program after removing the syntactical error(s), if any. Underline each correction. #include<iostream.h> int main( ) { struct STUDENT { char stu_name[20]; char stu_sex; int stu_age=17; }student; gets(stu_name); gets(stu_sex); return 0; } Ans.
#include<iostream.h> #include<stdio.h> int main( ) { struct STUDENT { char stu_name[20]; char stu_sex; int stu_age; //Initialization of variables inside a structure is not allowed. }student; gets(student.stu_name); cin>>student.stu_sex; //A single character cannot be read using gets return 0; }
6. What are Nested Structures? Give an example. Ans.
Nested structures are structures as member of another structure. For example,the date of birth is astructure within the structure of a student as shown below. These types of structures are known as nested structures.
2
Uploaded by Sathiya Ramesh
CHAPTER-13 STRUCTURES SHORT ANSWER QUESTIONS
1. Discuss the similarity and differences of a structure as compared to an array and a class. Ans. Similarities array and Structure
Both structures and arrays are used to hold more than 1 elements under single name.
Difference array structure
Arrays bring together a group of items of the same data type
Structures bring together a group of related data items of any data type.
Similarities class and structure
Both Class and Structures can have methods, variables and objects.
Both can have constructor.
Both are user defined types.
Difference class structure
Declared with the keyword “class”
Declared with the keyword “struct”
By default, all members are “private” in a class
By default, all members are “public” in a structure
2. Define a structure to store information about a fish. The structure should include the kind, the weight in grams, and the length in inches. Declares variables of this structure type and discuss various methods of initializing them. Ans.
struct Fish_Info { char kind[50]; int gram; int length; }; Fish_Info fish;
first method of initializing
The structure elements of a structure can be initialized separately, using separate assignment statement. fish.kind=”abc”; fish.gram=300; fish.length=5;
second method of initializing
The structure element of a structure can be initialized jointly, using the notation similar to array initialization. Fish_Info fish = { “abc”, 300, 5);
3. Write definition for a structure EMPREC that stores information about an employee such as empno, name, address, salary, and joining_date. The address member of EMPREC stores the information houseno, area, and city. The joining_date member of EMPREC stores information day, month, and year. Ans.
struct date { int day; int month; int year; }; struct addr
1
{ int houseno; char area[31]; char city[31]; }; struct EMPREC { int empno; char name[41]; addr address; float salary; date joining_date; };
4. What is structure? Declare a structure in C++ with name, roll number and total marks as components. Ans.
A structure is a collection of variables having different data types. The declaration as: struct student { char name[20]; int roll_no,marks; };
5. Rewrite the following program after removing the syntactical error(s), if any. Underline each correction. #include<iostream.h> int main( ) { struct STUDENT { char stu_name[20]; char stu_sex; int stu_age=17; }student; gets(stu_name); gets(stu_sex); return 0; } Ans.
#include<iostream.h> #include<stdio.h> int main( ) { struct STUDENT { char stu_name[20]; char stu_sex; int stu_age; //Initialization of variables inside a structure is not allowed. }student; gets(student.stu_name); cin>>student.stu_sex; //A single character cannot be read using gets return 0; }
6. What are Nested Structures? Give an example. Ans.
Nested structures are structures as member of another structure. For example,the date of birth is astructure within the structure of a student as shown below. These types of structures are known as nested structures.
2
Similar questions