Home | | Computer Science 11th std | Answer the following questions

Arrays and Structures | Computer Science - Answer the following questions | 11th Computer Science : Chapter 12 : Arrays and Structures

Chapter: 11th Computer Science : Chapter 12 : Arrays and Structures

Answer the following questions

Short Answers, Explain in Brief, Explain in detail, Important Questions - Computer Science : Introduction of C++: Arrays and Structures

Introduction of C++

Arrays and Structures


Arrays


Part – II

Answer to all the questions (2 Marks):


1. What is Traversal in an Array?

Answer: Accessing each element of an array at least once to perform any operation is known as "Traversal". Displaying all the elements in an array is an example of "traversal".


2. What is Strings?

Answer: A string is defined as a sequence of characters where each character may be a letter, number or a symbol.


3. What is the syntax to declare two – dimensional array.

Answer: The declaration of a 2-D array is data-type array_name [row-size] [col-size];

 

Part – III

Answer to all the questions(3 Marks):


1. Define an Array ? What are the types?

Answer: "An array is a collection of variables of the same type that are referenced by a common name".

There are different types of arrays used in C++. They are:

(i) One-dimensional arrays

(ii) Two-dimensional arrays

(iii) Multi-dimensional arrays


2. With note an Array of strings.

Answer: An array of strings is a two-dimensional character array. The size of the first index (rows) denotes the number of strings and the size of the second index (columns) denotes the maximum length of each string. Usually, array of strings are declared in such a way to accommodate the null character at the end of each string. For example, the 2-D array has the declaration:

char Name[6][10];


3. Write a C++ program to accept and print your name?

Answer:

#include<iostream>

using namespace std;

int main()

{

char name[20];

cout<<"Enter your name"<<endl;

cin.get(name,20);

cout<<"Your name:"<<name<<endl;

return 0;

}

output:

Enter your name : xxxx xxxx

My name : xxxx xxxx

 

Part – IV

Answer to all the questions (5 Marks):


1. Write a C++ program to find the difference between two matrix.

Answer:

#include<iostream.h>

 using namespace std;

 int main()

{

int i,j, A[10][10], B[10][l0], m,n;

cout<<"Enter number of rows"<<endl;

 cin>>m;

cout<<"Enter number of columns"<<endl;

cin>>n;

cout<<"Enter the elements of A matrix"<<endl;

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

{

for (j = 0; j<n; j++)

{

Cin>>A[i][j];

}

}

Cout<<"Enter the elements of B matrix" <<endl;

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

{

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

{

for (j = 0; j<n; j++)

{

cin>>B[i][j];

}

}

count cc 'The difference between the matrices" <<endl;

}

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

{

for (j = 0; j<n; j++)

{

cout<<(A[i][j] −B[i][j])<<"s/t"

}

cout <<"/n";

}

}

 


2. How will you pass two dimensional array to a function explain with example.

Answer: In C++, arrays can be passed to a function as an argument. To pass an array to a function in C++, the function needs the array name as an arugment. Passing a two-dimensional array to a function

 Example: C++ program to display marks of 5 students by passing one dimensional array to a function.

#include<iostream>

 using namespace std;

 void display (in m[5]);

int main()

{

int marks[5]={88, 76, 90, 61,69};

display(marks);

return 0;

}

void display (int m[5])

{

Cout<<"\n Display Marks:"<<endl;

for(int i=0; i<5; i++)

{

Cout<<"Student"<<i+l<<":"<<m[i]<<endl;

}

}

Output:

Display Marks:

Student 1: 88

 Student 2: 76

 Student 3: 90

Student 4: 61

Student 5: 69

 

CASE STUDY

 

1. Write a program to accept the marks of 10 students and find the average, maximum and minimum marks.

Answer:

#include<iostream>

int main()

{

int m[10], i, max=0, min=0, sum=0;

 float avg = 0;

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

{

Cout<<"Enter Marks of student"<<i+l<<endl;

Cin>>m[i];

sum=sum+m[i];

}

avg=sum/10;

cout<<" Average="<<avg<<endl;

 max=min=m[0];

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

{

if (m[i]>=max)

max = m[i];

if(m[i]<=min)

min = m[i];

}

Cout<<"maximum mark ="<<max<<endl;

Cout<<"minimum mark ="<<min<<endl;

return 0;

}

 

2. Write a program to accept rainfall recorded in four metropolitan cities of India and find the city that has the highest and lowest rainfall.

Answer:

#include<iostream>

int main()

{

int m[10], i, min=0, max=0, posl, pos2;

char n[4][10]={"CHENNAI", "MUMBAI", "DELHI", "KOLKATTA"};

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

{

Cout<<"Enter rainfall recorded in \t"<<n[i]<<endl;

cin>>m[i];

}

min=max=m[0];

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

{

if (m[i]>=max)

{ max = m[i];

posl = i;

}

if (m[i]<=min)

{min = m[i];

pos2 = i;

}

}

Cout<<"Highest Rainfall\t"<<n[pos1]<<endl;

Cout<<"Lowest Rainfall\t"<<n[pos2]<<endl;

return 0;

}

 

3. Survey your neighboring shops and find the price of any particular product of interest and suggest where to buy the product at the lowest cost.

Answer:

#include<iostream>

 using namesapce std;

#include<stdlib.h>

#include<math.h>

int main ()

{

int m[10], i, j min=0, max=0;

char n[4][10]={"a2b", "sangeetha", "nilgris", "spencers" },pos2;

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

{

Cout<<"Enter the amount of product in \t"<<n[i]<<endl;

Cin>>m[i];

}

min=m[0];

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

{

if (m[i]<=:min)

{

min = m[i];

pos2=i;

}

 }

cout<<"Lowestpriceat\t"<<n[pos2];

return 0;

}

 

 

Structures

 

Part – II 

Answer to all the questions (2 Marks):


1. Define structure .What is its use?

Answer: Structure is a user-defined which has the combination of data items with different data types. This allows to group of variables of mixed data types together into a single unit.


2. To store 100 integer number which of the following is good to use?

Array or Structure

State the reason.

Answer: Array Because Array is a collection of variables of same datatype, (staring 100 integer number)


3. What is the error in the following structure definition.

struct employee{ inteno;charename[20];char dept;}

Employee e1,e2;

Answer:

(i) Structure not terminated with;

(ii) Structure tag should be in uppercase

(iii) Space between datatype and variable

struct Employee

{

int eno;

char ename[20];

char dept;

};

employee el, e2;

 

4. Write a structure definition for the structure student containing examno, name and an array for storing five subject marks.

Answer:

struct student

{

int exam no;

char name[50];

int marks[5];

};

 

5. Why for passing a structure to a function call by reference is advisable to us?

Answer: Structures are usually passed by reference method because it saves the memory space and executes faster.


Q. What is the size of the following highlighted variable in terms of byte if it is compiled in dev c++

struct A{ float f[3]; char ch[5];long double d;};

struct B{ A a; int arr[2][3];}b[3]

Q. Is the following snippet is fully correct. If not identify the error. struct sum1{ int n1,n2;}s1;

struct sum2{int n1,n2}s2;

cin>>s1.n1>>s1.n2;

s2=s1;

Q. Differentiate array and structure.

Q. What are the different ways to initialize the structure members?

Q. What is wrong with the following C++ declarations?

A. struct point ( double x, y )

B. struct point { double x, double y };

C. struct point { double x; double y }

D. struct point { double x; double y; };

E. struct point { double x; double y; }

 

PART – III

Answer to all the questions (3 Marks):


1. How will you pass a structure to a function ?

Answer: (i) A structure variable can be passed to a function in a similar way of passing any argument that is of built-in data type.

(ii) if the structure itself is an argument, then it is called "call by value".

(iii) If the reference of the structure is passed as an argument then it is called, "call by reference".


2. The following code sums up the total of all students name starting with ‘S’ and display it.Fill in the blanks with required statements.

struct student {int exam no,lang,eng,phy,che,mat,csc,total;char name[15];};

int main()

{

student s[20];

for(int i=0;i<20;i++)

{

…………………….. //accept student details

}

for(int i=0;i<20;i++)

{

…………………….. //check for name starts with letter “S”

……………………. // display the detail of the checked name

}

return 0;

}

Answer:

//accept Student details

Cout<<" Enter Exam number"<<endl;

 cin>> s[i].examno;

Cout<<" Enter student name"<<endl;

cin>> s[i].name;

Cout<<" Enter language mark"<<endl;

cin>> s[i].lang;

Cout<<" Enter English mark"<<endl;

cin>> s[i].eng;

Cout<<" Enter Physics mark"<<endl;

cin>> s[i].phy;

Cout<<" Enter Chemisty mark"<<endl;

cin>> s[i].che;

Cout<<" Enter Maths mark"<<endl;

cin>> s[i].mat;

Cout<<" Enter Comp.sci mark"<<endl;

cin>> s[i].csc;

//check for name starts with letter 'S'

if (s[i].name = = ‘S')

{

Cout<<"Exam number:"<<s[i].exam<<endl;

Cout<<"Name:"<<s[i].name<<endl;

Cout<<"Language:"<<s[i].lang<<endl;

Cout<<"English:"<<s[i].eng<<endl;

Cout<<"Physics:"<<s[i].phy<<endl,

Cout<<"Chemistry:"<<s[i].che<<endl;

Cout<<"Maths:"<<s[i].mat<<endl;

Cout<<"Csc:"<<s[i].csc<<endl; 

total=s[i].lans+s[i].eng+s[i].phy+s[i].che+s[i].mat+s[i].csc;

cout<<"Total:"<<total<<endl;

}

 

Q. What is called nested structure. Give example

Q. Rewrite the following program after removing the syntactical error(s),if any.

Underline each correction.

struct movie

{

charm_name[10];

charm_lang[10];

float ticket cost =50;};

Movie;

void main()

{

gets(m_name);

cin>>m_lang;

return 0;

}

Q. What is the difference among the following two programs?

(a) #include <iostream.h>

struct point { double x; double y; };

int main() {

struct point test;

test.x = .25; test.y = .75;

cout<<test.x<<test.y;

return 0;

}

(b) #include <iostream.h>

struct { double x; double y; } Point;

int main(void) {

Point test={.25,.75};

return 0;

}


3. How to access members of a structure?Give example.

Answer: A structure object can also be assigned to another structure object only if both the objects are of same structure type.

Example : (i) s.name (ii) s.rollno


4. Write the syntax and an example for structure.

 Answer:

struct structure name {

type member_namel;

type mamber_name2;

} reference_name;

Example:

struct Student

 {

long rollno;

 int age;

float weight;

};

 

Q. For the following structure definition write the user defined function to

accept data through keyboard.

struct date{ int dd,mm,yy};

struct item { int item id;char name[10];float price;date date_manif;}


5. What is called anonymous structure .Give an example

Answer:

A structure without a name/tag is called anonymous structure.

Example:

struct

{

long rollno;

int age;

float weight;

} student;

The student can be referred as reference name to the above structure and the elements can be accessed like student.rollno, student.age and student.weight


Q. Write a user defined function to return the structure after accepting value through keyboard.The structure definition is as follows

struct Item{int item no;float price;};

 

Part – IV

Answer to all the questions (5 Marks):


1. Explain array of structures with example

Answer: An array of structures is declared in the same way as declaring and array with built-in data types like int or char. A class may contain many students. So, the definition of structure for one student can also be extended to all the students. If the class has 20 students, then 20 individual structures are required. For this purpose, an array of structures can be used

 Example: The following program read the details of 20 students and prints the same.

#include<iostream>

using namespace std;

struct Student

{

int age;

float height, weight;

char name[30];

};

void main()

{

Student std[20];

int i;

cout<<"Enter the details for 20 student”<<endl;

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

cout<< “ Enter the details of student”<<i+l<<endl;

cout<< “ Enter the age:”<<endl;

cin>>std[i].age;

cout<< “Enter the height:”<<endl;

cin>>std[i].height;

cout<< “Enter the weight:”<<endl;

cin>>std[i]. weight;

}

Cout<<"The values entered for Age, height and weight are"<<endl;

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

cout<<"Student"<<i+l<<"\t"<<std[i].age<<"\t"<<std[i].height<<"\t"<<std[i].weight;

}

 

2. Explain call by value with respect to structure.

Answer: Call by value : When a structure is passed as argument to a function using call by value method, any change made to the contents of the structure variable inside the function to which it is passed do not effect the structure variable used as an argument.

Consider this example:

#include<iostream>

using namespace std;

struct Employee

{

char name[50];

 int age;

float salary;

};

void printData(Employee);         //Function declaration

int main()

{

Employee p;

Cout<< "Enter Full name: ";

Cin>>p.name;

Cout<< "Enter age:";

Cin>>p.age;

Cout<< "Enter salary:";

Cin>>p.salary;

//Function call with structure variable as an argument

PrintData(P);

return 0;

}

void printData(Employee q)

}

Cout<<"\nDisplaying Information."<<endl;

cout<<"Name:"<<q.name<<endl;

cout<<"Age:"<<q.age<<endl;

 cout<<"Salary:"<<q. salary;

}

Output:

Enter Full name: Kumar

Enter age : 55

Enter salary : 34233.4

Displaying Information.

Name: Kumar

Age: 55

Salary: 34233.4

 

Q. How call by reference is used to pass structure to a function .Give an Example


3. Write a C++ program to add two distances using the following structure definition

struct Distance{

int feet;

float inch;

}d1 , d2, sum;

Answer:

#include<iostream>

using namespace std;

struct Distance

 {

int feet;

float inch;

} d1, d2, sum;

int main ( )

{

Cout<< "Enter first distance:"<<endl;

cout<< "Enter feet:"<<endl;

cin>>dl.feet;

cout<< "Enter inch:"<<endl;

cin>>dl.inch;

cout<<"Enter second distance" <<endl;

cout<< "Enter feet;" endl;

cin>>d2.feet;

cout<< "Enter inch : "<<endl;

cin >>d2.inch;

sum.feet = dt.feet+d2.feet;

sum.inch = dl.inch+d2.inch;

 if (sum.inch>12)

{

++sum.feet;

sum.inch − =12;

}

Cout<<"sum of the distances:"<<sum.feet<<'\t'<<sum.inch<<endl;

return 0;

}

 

Q. Write a C++ Program to Add two Complex Numbers by Passing Structure to a Function for the following structure definition

struct complex

{

float real;

float imag;

};

The prototype of the function is

complex add Complex Numbers(complex, complex);

Q. Write a C++ Program to declare a structure book containing name and author as character array of 20 elements each and price as integer. Declare an array of book .Accept the name ,author,price detail for each book.Define a user defined function to display the book details and calculate the total price. Return total price to the calling function.


4. Write a c++ program to declare and accept an array of professors.Display the details of the department=”COMP.SCI” and the name of the professors start with ‘A’. The structure “college” should contain the following members .

prof_id as integer

name and Department as character array

Answer:

#include<iostream>

 using namespace std;

#include<string.h>

 struct college

 {

int prof.id;

 char pname[20];

 char dept[20];

} P[10];

 int main()

{

int i;

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

{

Cout<<"Enter professor.id"<<endl;

cin>>p[i].prof_id;

cout<<"Enter proferssor name"<<endl;

cin>>p[i].pname;

cout<<"Enter department"<<endl;

cin>>p[i].dept;

}

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

{

int flag = 1;

if(p[i].dept=="COMP.SCI" && p[i].pname=='A')

cout<<p[i].prof_id<<endl;

cout<<p[i].pname<<endl;

cout<<p[i].dept<<endl;

}

if(flag==0)

cout<<"The given details not found"<<endl;

 return 0;

} 


5. Write the output of the following c++ program

#include<iostream>

#include<stdio>

#include <string>

#include<conio>

using namespace std;

struct books {

char name[20], author[20];

} a[50];

int main()

{

clrscr();

cout<< "Details of Book No " << 1 << "\n";

cout<< "------------------------\n";

cout<< "Book Name :"<<strcpy(a[0].name,"Programming ")<<endl;

cout<< "Book Author :"<<strcpy(a[0].author,"Dromy")<<endl;

cout<< "\nDetails of Book No " << 2 << "\n";

cout<< "------------------------\n";

cout<< "Book Name :"<<strcpy(a[1].name,"C++programming" )<<endl;

cout<< "Book Author :"<<strcpy(a[1].author,"BjarneStroustrup ")<<endl;

cout<<"\n\n";

cout<< "================================================\n";

cout<< " S.No\t| Book Name\t|author\n";

cout<< "====================================================";

for (int i = 0; i < 2; i++) {

cout<< "\n " << i + 1 << "\t|" << a[i].name << "\t| " << a[i].author;

}

cout<< "\n=================================================";

return 0;

}

Answer:

Output:

Details of Book No 1

………………………………………………….

Book Name : Programming

 Book Author : Dromy

 Details of Book No 2

……………………………………………………..

Book Name : C++ Programming

Book Author : BjarneStroustrup

=================================

S.No. |   Book Name     | author

=================================

1       | Programming     | Dromy

 2      |  C++ Programming | BjarneStroustrup

=================================


6. Write the output of the following c++ program

#include <iostream>

#include <string>

using namespace std;

struct student

{

 introll_no;

 char name[10];

 long phone_number;

};

int main(){

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

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;

cout<< "First Student" <<endl;

cout<< "roll no : " << p1.roll_no <<endl;

cout<< "name : " << p1.name <<endl;

cout<< "phone no : " << p1.phone_number <<endl;

cout<< "Second Student" <<endl;

cout<< "roll no : " << p2.roll_no <<endl;

cout<< "name : " << p2.name <<endl;

cout<< "phone no : " << p2.phone_number <<endl;

cout<< "Third Student" <<endl;

cout<< "roll no : " << p3.roll_no <<endl;

cout<< "name : " << p3.name <<endl;

cout<< "phone no : " << p3.phone_number <<endl;

return 0;

}

Answer:

Output:

First Student

roll no : 1

 name : Brown

phone no : 123443

 Second Student

roon no : 2

name : Sam

Phone no: 1234567822

Third Student

roll no : 3

name : Addy

Phone no : 1234567844

 

7. Debug the error in the following program

#include <istream.h>

structPersonRec

{

  charlastName[10];

  chaefirstName[10];

int age;

}

  PersonRecPeopleArrayType[10];

voidLoadArray(PeopleRecpeop);

void main()

{

PersonRecord people;

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

{

cout<<people.firstName<< ‘ ‘ <<people.lastName

<<setw(10) <<people.age;

}

}

LoadArray(PersonRecpeop)

{

for (int i = 0; i < 10; i++)

{

cout<< "Enter first name: ";

cin<<peop[i].firstName;

cout<< "Enter last name: ";

cin>>peop[i].lastName;

cout<< "Enter age: ";

cin>> people[i].age;}

Answer:

1. #include<iostream.h>

2. struct PersonRec

{

3. char last Name[ 10];

4. char first Name[ 10];

5. };

6. PersonRec People[10];

7. void LoadArray(persoRec op)

8. PersonRec op; int i;

9. cout<<people[i].firtName<<'\t'<<people[i].lastName<<setw(10)<<people[i].age;

10. void Load Array( PersonRec op)

11. cin>> op[i].firstName;

12. cin>>op[i].lastname;

13. cin>>op[i].age;

14. }

Correct Program :

#include<iostream.h>

 struct PersonRec

{

char lastName[10];

char firstName[10];

int age;

};

PersonRec people[ l0];

void LoadArray (People op);

void main()

{

int i;

PersonRec op;

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

{

Cout<<people[i].firstName<<'\t'<<people[i].lastName<<setw( 10)<<people[i].age;

}

}

void LoadArray(personRec op)

{

for(int i=0; i< 10; i++)

{

Cout<<"Enter firstName:";

Cin>>op[i].firstName;

Cout<<"Enter LastName:";

cin>>op[i].lastName;

cout<<"Enter age:";

cin>>op[i].age;

}

}

 

Tags : Arrays and Structures | Computer Science , 11th Computer Science : Chapter 12 : Arrays and Structures
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 12 : Arrays and Structures : Answer the following questions | Arrays and Structures | Computer Science


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.