OOP Lab 1 Manual
OOP Lab 1 Manual
Lab 01
● Array
Lab Description:
This lab is basically design for the revision of character array and C-String. You will also learn importance
of file output and how to write into a text file.
In previous lab we already discuss integer array, let’s start with character array.
Character array:
An array is a collection of a fixed number of components (also called elements) all of the same data type
and in contiguous (that is, adjacent) memory space. An array whose components are of type char.
char list[10];
1
Above statement is used for creating an array. But as we discussed above when a memory location is
reserved for this array it holds some values which are not assigned by user. So those values are
considered as garbage value. In order to avoid garbage value, it is a good practice to assign values at the
time of creation of array.
Assigning value at the time of declaring of array or variable is called initialization statement. There are
multiple ways of initialize an array.
char list[5]={‘a’,’b’,’c’,’d’,’e’};
char list[5]={‘a’,’b’};
on remaining indexes null will be assigned as initial value in case of partial array initialization.
Array input:
We can take input from user into an array. We can take input index wise one by one and we can also
take input at specific index number.
cin>> list[0]; //taking input on index 0 which is the first element of an array.
We use repetition statement(loops) for taking input in whole array. We can take input one by one as
mention above but it is not considered as a good practice.
2
for(int i=0;i<size;i++)// i is used as index number and size is the number element you want to enter.
{
cin>>list[i]; // taking input at specific index i. After every iteration value of i will be updated.
}
Array output:
We can display the values of array on console as output to user. We can display index wise one by one
and we can also take input at specific index number.
cout<< list[0]; //display the value of index 0 which is the first element of an array.
We use repetition statement(loops) for display whole array. We can display elements one by one as
mention above but it is not considered as a good practice.
for(int i=0;i<size;i++)// i is used as index number and size is the number element you want to display.
{
cout<<list[i]; // display specific index i. After every iteration value of i will be updated.
}
Array index out of bound:
Index of an array is in bounds if index is between 0 and ARRAY_ SIZE - 1, that is, 0 <= index <=
ARRAY_SIZE - 1. If index is negative or index is greater than ARRAY_SIZE - 1, then we say that the index is
out of bounds.
C-String:
A character array which is terminated at null is called C-String. C-String is quite simmilar with character
array but, some of the working is a bit different. We can declare a C-String same as we declare a
character array. But initialization is a bit different.
char list[5]={“abcd”};
In case of character array we write character in single qoute which are comma seprated in case of
character array. In case of C-String we write a string in double qoutes. One more differecne is we need
an extra index for storing null in case of c-string. Because a null character will be placed at the end of the
c-string.
Acessing a c-string:
We can access the element of a c-string same as we did in case of character array. But input and output
of a c-string is a bit different than a character array.
3
Input a c-string:
We can take the input into c-string same as we discuss in character array. But there is an issue if we
need to read a full name using this method it falis. Because, extraction operator, >>, skips all leading
whitespace characters and stops reading data into the current variable as soon as it finds the first
whitespace character. So we need something else in order to take complete input (with whitespaces)
into a c-string.
cin.get():
Using this method, we can take whitespaces as input from user. This method helps us to take input as
character by character and as a whole string as well.
Output of c-string:
There is a limitation with insertion operator, <<, it displays the data till null character. So if we are
displays character array with this method it may shows some garbage values after the data.
File Write:
You want to take admission in any university. You need to fill the admission form. Let suppose after
getting admission in university they ask your information no daily basis because, they did not store your
information anywhere.
Is it a good practice to ask your students about their information on daily basis?
Obviously not, it’s not a good practice. So, what’s the solution of this problem?
Simple solution is you must store the data somewhere in order to perform different operations. We can
create a text file in order to store our data. An area in secondary storage used to hold information is
4
called file. The standard I/O header file, iostream, contains data types and variables that are used only
for input from the standard input device and output to the standard output device. In addition, C++
provides a header file called fstream, which is used for file output. Among other things, the fstream
header file contains the definitions of a data type ofstream, which means output file stream and is
similar to ostream. The variables cin and cout are already defined and associated with the standard I/O
devices. In addition, <<, setfill, and so on can be used with cout. These same operators and functions are
also available for file output, but the header file fstream does not declare variables to use them. You
must declare variables called file stream variables, which include ofstream variables for output. You then
use these variables together with <<, or other functions for output. Remember that C++ does not
automatically initialize user-defined variables. Once you declare the fstream variables, you must
associate these file variables with the output source.
3. Associate the output file stream variables with the output sources (text file).
//fout.open(“fileName.txt”);
5. Use the output file stream variables with <<, or other output functions. //fout<<”Hello World”<<endl;
Sample code:
#include<fstream>
int main() {
ofstream fout;
if(fout.is_open())// this function return true if file is open and return false if file is not open.
5
//Code for file writing.
fout<< statements;
return 0;
There are different file opening modes. For output purpose we will discuss two modes app and out.
Output(out):
While we open a file for writing purpose default mode of file opening is out. In this mode pervious
content of file will be overwritten with new content.
Syntax:
ofstream fout;
fout.open(“fileName.txt”,ios::out); or fout.open(“fileName.txt”);
Append(app):
This mode will be helpful if you want to add new data into a file. In this mode control of writing will be
transferred at the end of file. All new data will be added at the end of all previously added records.
Syntax:
ofstream fout;
fout.open(“fileName.txt”,ios::app);
6
Lab Tasks
Find a word problem:
Book reading is a good practice. Almost all type of books is available in soft form as well. We often need
to find some keywords in order to read about it from books. It is quite difficult to find a keyword from
hard copy. But on other hand we can easily find any keyword form a soft copy.
Find a keyword:
In order to find a keyword let’s try to find a character first. Compare the entered character with each
character of array.
Keyword:
Now apply same logic which we discussed above and try to find a keyword from a character array.
Count keyword:
After successful attempt of finding key word let’s try to count that how many times that keyword is
found.
Marks problem:
As we solve a problem of identify the position holders in previous lab. We need to store the position
holder information in a file. Store each record on each line of file.
We have multiple grades (A, B, C, D, F) in a character array so, calculate the count of each grade and
store it into a file.
7
Admission form problem:
Thousands of students got admission in UCP. It is difficult to store their admission form. So, UCP want to
store the record of their student in soft form for later use. Each entity of form is stored as comma
separated value and each line stores record of each student.