CS201 Introduction To Programming Solved Subjective For Midterm Exam Preparation
CS201 Introduction To Programming Solved Subjective For Midterm Exam Preparation
Question: 20 Can you use an assignment operator to assign the value of one C-string to
another? (3)
Answer: repeat
Question: 21 The statement int Name [2][2]; define a 2x2 array, Write the code which read
data
from keyboard for this array. (5)
Answer:
for(int i=o; i<2; i++)
{
for (w=0; w<2; w++)
{
cin >>Name[i][w];
}
cout <<endl;
}
for(int i=o; i<2; i++)
{
for (w=0; w<2; w++)
{
cout<<Name[i][w]<<” ”;
}
cout <<endl;
}
Question: 22 Write a program which reads a text file “PlayersInfo.txt” residing in the current
directory.
Open the file PlayersInfo.txt in read mode and assign these values to the struct Player; assume
order of the
data in the file to be exactly the same as the order of struct attributes. The struct Player has following
attributes
i) Name
ii) Height
iii) Age
OR
Structure
In structures, the data members are public by default. It means that these are visible to all and anyone
can
change them. Is there any disadvantage of this? Think about the date.
syntax
struct student
{
char name[60];
char address[100];
float GPA;
};
Unions We have another construct named union. The concept of union in C/C++ is: if we have
something
in the memory, is there only one way to access that memory location or there are other ways to
access it.
We have been using int and char interchangeably in our programs. We have already developed a
program
that prints the ACSII codes. In this program, we have stored a char inside an integer. Is it possible to
have a
memory location and use it as int or char interchangeably? For such purposes, the construct union is
used.
The syntax of union is:
union intOrChar
{
int i;
char c;
};
Question: 22 Differentiate between C and c++.
(10)
1.
C was the C++ predecessor. As it's name implies, alot of C remains
in
C++.
Although
not actually being more powerful than C.
2.
C++ allows the programmer to more easily manage and operate with Objects, using an OOP (Object
Oriented Programming) concept
3.
C++ allows the programmer to create classes, which are somewhat similar to C
structures.
However, to a class can be assigned methods, functions associated to