You are on page 1of 110

COMPUTER LABORATORY MANUAL

PROGRAMMING FUNDAMENTALSLAB
(CSC102L)
Version 2.0

Student Name:
Class / Section:
Roll Number:
CGPA:
Email ID:

Department of Software Engineering (DSE)


FOUNDATION UNIVERSITY RAWALPINDI CAMPUS (FURC)
PREFACE
This Lab Manual has been specially designed with the idea of improving and enhancing
the problem-solving skills of students through consistent practice and creativity. TheLab
Manual covers all the topics taught in the BS Software Engineering core course
Programming Fundamentals. Each lab activity has been divided into three parts;
Learning Objectives, Practice Questions and Exercises. Learning Objectives indicate the
specific areas of C++ Language programming, in which a student will become proficient
after successfully completing the lab activity. These questions have been carefully
devised with the idea of keeping the students interested as well as motivated to complete
the designated tasks. There are about 1 to 3 exercise questions included in each lab
activity. These questions are intended to refine a student’s advanced level programming
skills. After the completion of a lab activity, students will take the POST-Lab tasks
home, complete these tasks and submit the source code files to courseinstructor.

PREPARED BY
Lab manual is prepared by Mr. Muhammad Usman, Mr. M. Ishtiaq, Dr. Shariq Hussain
under the supervision of Head of Department in the year 2014. Revised in Sep 2017 by
adding more experiments and done some corrections.

GENERAL INSTRUCTIONS
a. Students are required to maintain the lab manual with them till the end of the
semester.
b. All readings, answers to questions and illustrations must be solved on the place
provided. If more space is required, then additional sheets may be attached.
c. It is the responsibility of the student to have the manual graded before deadlines as
given by the instructor
d. Loss of manual will result in re submission of the complete manual.
e. Students are required to go through the experiment before coming to the lab session.
Lab session details will be given in training schedule.
f. Students must bring the manual in each lab.
g. Keep the manual neat clean and presentable.
h. Plagiarism is strictly forbidden. No credit will be given if a lab session is plagiarized
and no re submission will be entertained.
i. Marks will be deducted for late submission.

VERSION HISTORY
Date Update By Details
Sep 2014 Muhammad Usman Version 1.0. Initial draft prepared and
Muhammad Ishtiaq experiments outlined.
Dr. Shariq Hussain
Sep 2017 Dr. Shariq Hussain Version 1.1. Addition of Introduction to IDE,
Asma Naveed String and File processing chapters
Sep 2019 Dr. Imran Daud Version 2.0: Revision of content, updating
Muhammad Asif topics and their content
CSC102LProgrammingFundamentalsLabs Page2of110
MARKS

LAB Date Max. Marks Marks Instructor


NO Conducted Obtained Sign
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Grand Total

CSC102LProgrammingFundamentalsLabs Page3of110
TABLE OF CONTENTS
LAB 1 – INTROUDCTION TO DEVC++IDE ................................................................. 5
LAB 2 – C++BUILDING BLOCKS-I ............................................................................. 12
LAB 3 – C++BUILDING BLOCKS-II............................................................................ 21
LAB 4 – C++CONTROLSTRUCTURES-I..................................................................... 28
LAB 5 – C++CONTROLSTRUCTURES-II ................................................................... 35
LAB 6 – C++CONTROLSTRUCTURES-III .................................................................. 46
LAB 7 – C++ LANGUAGEANDFUNCTIONS-I ........................................................... 54
LAB 8 – C++ LANGUAGEANDFUNCTIONS-II ......................................................... 62
LAB 9 – C++ LANGUAGEANDFUNCTIONS-III ........................................................ 66
LAB 10– ARRAYS-I ....................................................................................................... 71
LAB 11 –2-DIMENSIONALARRAYS-I ........................................................................ 78
LAB 12 –2-DIMENSIONALARRAYS-II....................................................................... 83
LAB 13– STRINGS ......................................................................................................... 88
LAB 14– STRUCTURES ................................................................................................ 94
LAB 15– POINTERS ....................................................................................................... 98
LAB 16 – POINTERSAND ARRAYS .......................................................................... 103
LAB 17 –FILE PROCESSING ...................................................................................... 107
APPENDIX –ASCIITABLE .......................................................................................... 110

CSC102LProgrammingFundamentalsLabs Page4of110
LAB 1 – INTROUDCTION TO DEV C++ Version 5.11 IDE

Dev-C++ Version 5.11 is a full-featured Integrated Development Environment (IDE) for


the C/C++ programming language. As similar IDEs, it offers to the programmer a simple
and unified tool to edit, compile, link, and debug programs. It also provides support for
the management of the files of a program in “projects” containing all the elements
required to produce a final executable program.

Dev-C++ Version 5.11 is a Free Software distributed under the terms of the GNU
General Public License (GPL). The IDE can be downloaded here:

http://prdownloads.sourceforge.net/dev-cpp/devcpp-4.9.9.2_setup.exe

Dev-C++ Version 5.11 can be installed on any Windows machine with Windows
XP/7/8/10. This tutorial uses Dev-C++ 5.11 on Windows 10 (configuration in the
computer labs as of course 2016-2017). For the sake of simplicity, use the default
installation options in your home computer.

The application development process encompasses the following steps:

1. Create a project/source file


The type of application and the programming language to be used are specified.
2. Write source code
Write the program in C++ and save the source code file.
3. Compile and link the code
The source code is compiled and linked to generate a running program. Other
files of the project may be created if required.
4. Fix compilation errors, if any
If the syntax of the program is not correct, the compilation fails and the compiler
generates a message related to the error/s. The programmer must correct the
errors.
5. Run the program
Run the program to validate the functioning.
6. Fix execution errors, if any
If the actions performed by the program are not as expected, it is necessary to
correct the source code. It may be also convenient to use the debugger to find
complex errors.

CSC102LProgrammingFundamentalsLabs Page5of110
Start Dev C++ Version 5.11

Start the IDE from the Program folder Dev-C++ or Bloodshed Dev-C++.

Figure 1. Running Dev-C++ in the computer lab

Create a Source File


The source code will be written in a file called source code file. It is necessary to create a
source code file by using menu (File > New > Source File).

Figure 2. Creating new source code file

CSC102LProgrammingFundamentalsLabs Page6of110
The IDE generates a basic source code file (by default, Untitled1.cpp). These files are
not saved in the project folder until the programmer saves or compiles the program (see
below).

File
SourceCodeEditor
Explorer

ResultsWindow

Figure 3. Three sub windows in IDE

The IDE window includes three sub-windows: the Project Files Explorer, the Result
Tabs, and the Source Code Editor. These windows can be resized and minimized.

The Files Explorer window shows the name of the project and the included files. The
Project tab usually contains a single file with the source code of the program. In this
pane, we can find two additional tabs: Classes and Debug. Classes tab shows the
functions of the program. Debug tab shows watched variables in the debugging
process.

The Results window is used to present the results of the actions of the IDE:
compilation errors, compiling directives, debugging commands etc.

Writing Source Code

Once the Source File has been created, we can start writing our C++ program.

It is recommended to use the classic color configuration of the editor (Tools > Editor
Options > Syntax > Color Speed Settings > Classic) and to activate the support for
opening and closing brackets (Tools > Editor options > General > Highlight matching
braces / parenthesis).

The editor highlights with different colors keywords and other elements of the C++
language. The classic scheme uses:

CSC102LProgrammingFundamentalsLabs Page7of110
 Light blue for comments
 Green for included libraries
 Red for text strings
 Bold black for C++keywords

For instance, the Hello World program is shown as follows:

Figure 4. The Hello World program

Compilation and Link

To run a program, the source code must be compiled and linked. Dev-C++ performs the
complete process by clicking the Compile button or from menu (Execute > Compile
or pressing F9key).

CSC102LProgrammingFundamentalsLabs Page8of110
Compile Button

Figure 5. Compiling the source code

The editor displays the File Save Dialogue Box to save the file before compilation and
execution as shown.

Figure 6. Save the source code file

Select appropriate folder and specify the filename. Then click Save to store the file
permanently on the local drive. Once the compilation process is finished successfully the
log will be displayed in Results Window as shown.

CSC102LProgrammingFundamentalsLabs Page9of110
Figure 7. Compilation successful as shown in Compile Log window

Execute the Program

Then execute/run the program by clicking Run button or from menu (Execute >Run
or pressing F10key).

Figure 8. Executing the program

It is possible to compile and run the program in a single step by clicking Compile &
Run button or from menu (Execute > Compile & Run or pressing F11key).

CSC102LProgrammingFundamentalsLabs Page10of110
Figure 9. Compile and Run in one-step

Assuming you did not make any syntax errors on your code, you should see a similar
output window running your program.

Figure 10. Displaying the output window

CSC102LProgrammingFundamentalsLabs Page11of110
LAB 2 – C++ BUILDING BLOCKS-I

Objectives
1. Basic program structure
2. Declaring variables (int, double and char).
3. Input and Output using cin and cout with/without using variables.
4. Using basic arithmetic operators available in C++ ( +, - ,* , /).
5. Using escape sequences in cout statements.
Task 2.1 Program structure
Define basic structure of C/C++ program that include prototype, main function, cin/cout
etc
Task 2.2 Declaring Variables
Write a C++ program, which declares integer ‘a’, float ‘b’ and character ‘c’. Ask user
for an integer value and store it in ‘a’, for a float value and store it in ‘b’ and for a
character value and store it in ‘c’. At the end, print the values of these three variables
on screen. Your output should be as below. Indent your code and include comments
for improving the readability of your code.

Enter Integer Value: 448


Enter Float Value: 32.65
Enter Character Value: A
***** You have entered the following values *****
Integer Value is:448
Float Value is: 32.65
Character Value is:A

CSC102LProgrammingFundamentalsLabs Page12of110
Task2.3 Arithmetic Operations on Variables +, -, *, / ,%
Write a C++ program which takes two int values from user and computes their sum,
difference, product, quotient and remainder. At the end, print these results on screen
with a beep. Your output should be as below. Indent your code and include
comments for improving the readability of your code.

Enter Value 1:50


Enter Value 2:50
Sum : 50 + 50 = 100
Difference : 50 – 50 =0
Product : 50 * 50 =2500
Quotient : 50 / 50 = 1
Remainder : 50 % 50 = 0

CSC102LProgrammingFundamentalsLabs Page13of110
Task2.4 cout Statement with EscapeSequences
By using different escape sequences, write a C++ program, which displays the
following output. After every line, your program must generate a beep. If you already
do not know the escape sequence for generating a beep, find it out for yourself.
Indent your code and include comments for improving the readability of your code.

BCSE
BCSE
BCS
E
BCSE

CSC102LProgrammingFundamentalsLabs Page14of110
Task2.5 cout Statement with Escape Sequences
Write a C++ program, which generates the following output. After displaying the
output, 5 beeps must be generated. Indent your code and include comments for
improving the readability of your code.

This is a test.
He asked, “How are you doing?”
“Education is the most powerful weapon which
you can use to ‘CHANGE’ the world”.

CSC102LProgrammingFundamentalsLabs Page15of110
Exercise # 2.1(Arithmetic, Smallest and Largest)
Write a program that inputs three integers from the keyboard and prints the sum,
average, product, smallest and largest of these numbers. The screen dialog should appear
as follows:

Input three different integers: 13 27 14


Sum is 54 Average is 18 Product is 4914
Smallest is 13 Largest is 27

CSC102LProgrammingFundamentalsLabs Page16of110
Exercise # 2.2 (Displaying Shapes with Asterisks)
Write a program that prints a box, an oval, an arrow and a diamond as follows:

CSC102LProgrammingFundamentalsLabs Page17of110
Exercise # 2.3 (Table)
Using the techniques of this chapter, write a program that calculates the squares and
cubes of the integers from 0 to 10. Use tabs to print the following neatly formatted table
of values:

CSC102LProgrammingFundamentalsLabs Page18of110
Exercise # 2.4
The formulas for calculating BMI are or Create a BMI calculator application that reads the user’s
weight in pounds and height in inches (or, if you prefer, the user’s weight in kilograms and
height in meters), then calculates and displays the user’s body mass index. Also, the application
should display the following information from the Department of Health and Human
Services/National Institutes of Health so the user can evaluate his/her BMI:

CSC102LProgrammingFundamentalsLabs Page19of110
Web Resources

http://www.cprogramming.com/
http://www.mycplus.com/featured-articles/top-10-applications-written-in-c-
cplusplus/http://www.cplusplus.com/doc/tutorial/program_structure/http://www.cplus
plus.com/doc/tutorial/variables/http://www.cplusplus.com/doc/tutorial/constants/http:
//www.cs.wustl.edu/~schmidt/C++/

CSC102LProgrammingFundamentalsLabs Page20of110
LAB 3 – C++ BUILDING BLOCKS-II

Objectives
1. Taking a single character input from user using different built-in functions.
2. Displaying the ASCII values of different characters.
3. Evaluating arithmetic expressions using rules of precedence.
4. Using increment and decrement operators.

Task 3.1 ASCII Values in C++


Imp Note: Find out the differences between get(), getch(), getche() and getchar()
before lab session.
Every character has an ASCII value. Write a C++ program, which takes a character
input from user and prints its ASCII value on screen. Your must use all built-in
functions for taking character input from user (like get(), getch() , getche(), getchar()
etc), one by one. ASCII Table is included in Appendix A for your reference. Indent
your code and include comments for improving the readability of your code. Your
program should have the following interface.

Enter a Character and I will tell you it’s ASCII value : B


ASCII value is : 66

CSC102LProgrammingFundamentalsLabs Page21of110
Task3.2 Arithmetic Operations and Rules of Precedence
Write a C++ program which prompts the user to enter a float value of temperature in
Centigrade, converts it into Fahrenheit and displays the result on screen. Formula for
conversion is C = (F - 32) / 1.8. You must declare minimum number of variables. Do
you think you can avoid using parentheses? Indent your code and include comments
for improving the readability of your code. Your program should have the following
interface.

Enter the temperature in Centigrade: 37


37 Degrees Centigrade is equal to 98.6 Fahrenheit.

CSC102LProgrammingFundamentalsLabs Page22of110
Task3.3 Arithmetic Operators
Write a C++ program which reads in a three digit number from user and separates it’s
digits. The output of your program should be as below. Indent your code and include
comments for improving the readability of your code.

Enter a three digit number : 547


There are 5 Hundreds, 4 Tens and 7 Ones in 547.

CSC102LProgrammingFundamentalsLabs Page23of110
Task3.4 Arithmetic Operators
Write a C++ program which takes two integer values from user and displays one as
the percentage of other. The output of your program should be as below. Indent your
code and include comments for improving the readability of your code.

Enter the first number: 30


Enter the second number :40
30 is equal to 75.00% of40.

CSC102LProgrammingFundamentalsLabs Page24of110
Task3.5 Increment and Decrement Operators
Write a C++ program which prompts the user to enter an integer value, stores it into a
variable called ‘num’, evaluates the following expressions and displays results on
screen.
num+5, num-3, (num+3)–2),((num+5)*2/(num+3))
For performing addition and subtraction, you are allowed to use ONLY the increment
and decrement operators (both prefixing and postfixing are allowed). You must
remember that using increment/decrement operators changes the original value of a
number. Indent your code and include comments for improving the readability of
your code. Your program should have the following interface.

Enter a number : 20
20 + 5 =25
20 – 3 =17
(20 + 3)- 2 = 21
((20 + 5)*2 / (20 +3))= 2

CSC102LProgrammingFundamentalsLabs Page25of110
Excercise # 3.1
Write a C++ program which prompts the user to enter two integer values, stores them in
variables named ‘a’ and ‘b’, calculates the following expressions and displays the results
on screen. Output of your program must be presentable. You must use minimum possible
number of parentheses for evaluating these expressions.

1. (a + b)2 = a2 + 2ab + b2
2. (a - b)2 = a2 - 2ab + b2

CSC102LProgrammingFundamentalsLabs Page26of110
Web Resources

http://www.cplusplus.com/doc/tutorial/

CSC102LProgrammingFundamentalsLabs Page27of110
LAB 4 – C++ CONTROL STRUCTURES-I

Learning Objectives
1. Using one-way selection structure(if).
2. Using two-way selection structure(if/else).
3. Using multiple selection structure(switch).

Task4.1 ‘if’ Statement


Write a C++ program which takes two numbers from the user and tells which number
is greater. Your output should be as below.

Enter the first number : 25


Enter the second number : 32
First number is 25 and second number is 32.
Second number is greater.

CSC102LProgrammingFundamentalsLabs Page28of110
Task4.2 ‘if/else’ Statement
Write a C++ program which takes five numbers from the user and finds the largest of
these numbers. Your output should be as below.

Enter the first number : 25


Enter the second number : 32
Enter the third number : 12
Enter the fourth number : 85
Enter the fifth number : 53
The largest of the 5 numbers is 85.

CSC102LProgrammingFundamentalsLabs Page29of110
Task4.3 ‘if/else’ Statement
Write a C++ program which generates Grades for students depending on their Scores.
Here is the criterion for awarding Grades.
 If score is between85 -100, Grade is‘A’
 If score is between 72 - 84, Grade is‘B’
 If score is between 60 - 71, Grade is‘C’
 If score is between 50 - 59, Grade is‘D’
 If score is between 0 - 49, Grade is ‘F’(Fail)
For all other values of Score entered by user, your program should print “Invalid
Score was entered. No Grade can be generated”. The output of your program should
be as below.

Enter student’s score : 67


The student passed in C Grade.

Enter student’s score : 43


The student has failed with F Grade.

CSC102LProgrammingFundamentalsLabs Page30of110
Task4.4 ‘switch’ Statement
Repeat Task 4.3 using ‘switch’ statement.

CSC102LProgrammingFundamentalsLabs Page31of110
Exercise # 4.1
Write a C++ program which performs five basic arithmetic operations (+, -, *, /, %).
Your program should prompt the user to enter two integers and once character, where the
character variable will indicate the specific arithmetic operation to be performed. Your
program should have the following interface. In the following example, 5 and 7 are the
two integers and + is the character value entered by the user. Based on the value of this
character variable, your program should perform the appropriate arithmetic operation.
You have to write this program using both ‘if/else’ and ‘switch’ statements, one by one.
Output of your program must be presentable.

Enter the arithmetic expression to be evaluated:


5
+
7
5 + 7 = 12

CSC102LProgrammingFundamentalsLabs Page32of110
Exercise # 4.2
Company ‘CarsNLoans’ offers car loans to its customers. Every year, the company
spends thousands of rupees for printing the tariff brochures. It is now planning to cut
down on this expenditure by making a small computer application available to its
customers using which the customers can calculate the monthly installment rates. You
have been asked to write a C++ code for this application. Your program should prompt
the user to enter values of the following parameters.
 Vehicle Price (Total cost of car)
 Down Payment (First payment made while purchasing the car)
 Installment Months (Number of months for which loan is required)

Here is the method to calculate “Monthly Installments”.


 Payable Amount = Vehicle Price – Down Payment
 New Payable Amount = (Payable Amount + (Payable Amount * InterestRate))
 Monthly Installment = New Payable Amount / Installment Months

Interest Rate is applicable as below;


Installment Months Interest Rate
Less than or equal to 12 10% of Payable amount
Greater than 12 and less than or equal to 24 15% of Payable amount
Greater than 24 and less than or equal to 36 20% of Payable amount
Greater than 36 and less than or equal to 48 25% of Payable amount
Greater than 48 and less than or equal to 60 30% of Payable amount

Your program should have the following interface.

Enter the Vehicle Price in rupees : 800000


Enter the Down Payment in rupees : 300000
For how many months, do you want to lease the car : 36
Your Monthly Installment is : Rs.16666.7

CSC102LProgrammingFundamentalsLabs Page33of110
Web Resources

http://math.about.com/od/businessmath/ss/Interest_2.htm
http://www.tutorialspoint.com/cplusplus/

CSC102LProgrammingFundamentalsLabs Page34of110
LAB 5 – C++ CONTROL STRUCTURES-II

Objectives
1. Using ‘for’ repetition structure.
2. Using ‘while’ repetition structure.
3. Using ‘do/while’ repetition structure.

Task 5.1 ‘for’ Statement


Write a C++ program which takes an integer input from the user, prints all even and
odd integers less than or equal to this number and greater than or equal to1,
calculates the sum of these even and odd integers separately and displays results on
the screen. Your program should have the following interface.

Enter a number : 7
Odd numbers less than or equal to 7 are :
1 , 3 , 5, 7
Even numbers less than or equal to 7 are :
2 , 4 , 6

Sum of odd numbers : 16


Sum of even numbers : 12

CSC102LProgrammingFundamentalsLabs Page35of110
Task5.2 ‘while’ Statement
Write a C++ program which prompts the user to enter a float value, repeatedly prints
the value of this floating point variable, decreasing it by 0.5 each time, as long as its
value remains greater than or equal to 0. Your program should have the following
interface.

Enter any float value : 2.5


2.5
2.0
1.5
1.0
0.5
0

CSC102LProgrammingFundamentalsLabs Page36of110
Task5.3 ‘do/while’ Statement
Write a C++ program which takes an integer input from the user, calculates the
factorial of this number using ‘do/while’ statement and displays the result on screen.
Your program should have the following interface.

Enter any value to calculate its Factorial: 5


Factorial of 5 is : 120

CSC102LProgrammingFundamentalsLabs Page37of110
Task5.4 ‘for’ Statement
Write a C++ program which takes an integer input from user in the variable named
‘num’, calculates the sum of all integers from 1 to ‘num’ and displays this sum on
screen. Your program should have the following interface.

Enter any value : 10


Sum of integers from 1 to 10 is : 55

CSC102LProgrammingFundamentalsLabs Page38of110
Task5.5 ‘while’ Statement
The Greatest Common Divisor (GCD) of two integers is the largest integer that
evenly divides both the numbers. Write a C++ program that returns the GCD of two
numbers entered by the user. Your program should have the following interface.

Enter the first number : 35


Enter the second number : 45
GCD of 35 and 45 is : 5

CSC102LProgrammingFundamentalsLabs Page39of110
Exercise # 5.1
Repeat Task 5.1 using ‘while’ and ‘do/while’ statements. Repeat Task 5.2 using ‘for’ and
‘do/while’ statements. Repeat Task 5.3 using ‘for’ and ‘while’ statements. Repeat Task
5.4 using ‘while’ and ‘do/while’ statements. Repeat Task 5.5 using ‘for’ and ‘do/while’
statements.

CSC102LProgrammingFundamentalsLabs Page40of110
Exercise # 5.2 (Gas Mileage)
Drivers are concerned with the mileage obtained by their automobiles. One driver has
kept track of several trips by recording miles driven and gallons used for each trip.
Develop a C++ program that uses a while statement to input the miles driven and gallons
used for each trip. The program should calculate and display the miles per gallon
obtained for each trip and print the combined miles per gallon obtained for all tank full
up to this point.

CSC102LProgrammingFundamentalsLabs Page41of110
Exercise # 5.3 (Sales Commission Calculator)
A large company pays its salespeople on a commission basis. The salespeople each
receive $200 per week plus 9% of their gross sales for that week. For example, a
salesperson who sells $5000 worth of chemicals in a week receives $200 plus 9% of
$5000, or a total of $650. Develop a C++ program that uses a while statement to input
each salesperson’s gross sales for last week and calculates and displays that
salesperson’s earnings. Process one salesperson’s figures at a time.

CSC102LProgrammingFundamentalsLabs Page42of110
Exercise # 5.4 (Salary Calculator)
Develop a C++ program that uses a while statement to determine the gross pay for each
of several employees. The company pays “straight time” for the first 40 hours worked by
each employee and pays “time-and-a-half” for all hours worked in excess of 40 hours.
You are given a list of the employees of the company, the number of hours each
employee worked last week and the hourly rate of each employee. Your program should
input this information for each employee and should determine and display the
employee’s gross pay.

CSC102LProgrammingFundamentalsLabs Page43of110
Exercise # 5.5 (Find the Largest)
The process of finding the largest number (i.e., the maximum of a group of numbers) is
used frequently in computer applications. For example, a program that determines the
winner of a sales contest inputs the number of units sold by each salesperson. The
salesperson who sells the most units wins the contest. Write a C++ program that uses a
while statement to determine and print the largest number of 10 numbers input by the
user. Your program should use three variables, as follows: counter: A counter to count to
10 (i.e., to keep track of how many numbers have been input and to determine when all
10 numbers have been processed). number: The current number input to the program.
largest: The largest number found so far.

CSC102LProgrammingFundamentalsLabs Page44of110
Web Resources

http://www.tutorialspoint.com/cplusplus/
http://www.learncpp.com/

CSC102LProgrammingFundamentalsLabs Page45of110
LAB 6 – C++ CONTROL STRUCTURES-III

Objectives
1. Using nested ‘for’ repetition structure.
2. Using nested ‘while’ repetition structure.
3. Using nested ‘do/while’ repetition structure.
4. Using nesting of different control structures.

Task 6.1 Nested ‘for’ Statement (Drawing Patterns with Nested for Loops)
Write a program that uses for statements to print the following patterns separately,
one below the other. Use for loops to generate the patterns. All asterisks (*) should
be printed by a single statement of the form cout << '*'; (this causes the asterisks to
print side by side). [Hint: The last two patterns require that each line begin with an
appropriate number of blanks. Extra credit: Combine your code from the four
separate problems into a single program that prints all four patterns side by side by
making clever use of nested for loops.]

CSC102LProgrammingFundamentalsLabs Page46of110
Task6.2 Nested ‘while’ Statement
Repeat Task 6.1 using nested ‘while’ loop.

CSC102LProgrammingFundamentalsLabs Page47of110
Task6.3 Nested ‘for’ Statement
Write a C++ program to implement and generate a 5-digit number using nested for
loops and displays each number from 00000 to 99999. You will have to use 5 nested
‘for’ loops to generate the output.

CSC102LProgrammingFundamentalsLabs Page48of110
Task6.4 Nested ‘do/while’ Statement
Repeat Task 6.3 using nested ‘while’ loop.

CSC102LProgrammingFundamentalsLabs Page49of110
Exercice # 6.1
Write a C++ program to read an odd integer from the user. If the user enters an even
number, your program should decrement it by one to generate an odd number. Using this
number as the number of rows, your program should then display a diamond of *s on
screen. Your program should have the following interface.

Enter an odd number : 5


*
***
****
*
***
*

CSC102LProgrammingFundamentalsLabs Page50of110
Exercise # 6.2
Write a C++ program to produce the following multiplication table. You must use
nesting of control structures.

1 2 3 4 5 6 7 8 9 10 11 12

2 4 6 8 10 12 14 16 18 20 22 24

3 6 9 12 15 18 21 24 27 30 33 36

4 8 12 16 20 24 28 32 36 40 44 48

5 10 15 20 25 30 35 40 45 50 55 60

6 12 18 24 30 36 42 48 54 60 66 72

7 14 21 28 35 42 49 56 63 70 77 84

8 16 24 32 40 48 56 64 72 80 88 96

9 18 27 36 45 54 63 72 81 90 99 108

10 20 30 40 50 60 70 80 90 100 110 120

11 22 33 44 55 66 77 88 99 110 121 132

12 24 36 48 60 72 84 96 108 120 132 144

CSC102LProgrammingFundamentalsLabs Page51of110
Exercise # 6.3
Write a C++ program which reads in a number from user, stores it in a variable named
‘N’ and calculates the sum of powers using the following formula.

Sum = 11 + 22 + 33 +……+NN

Your program should have the following interface.

Enter a number : 4
Sum = 1^1 + 2^2 + 3^3 + 4^4 = 288

CSC102LProgrammingFundamentalsLabs Page52of110
Web Resources

http://www.tutorialspoint.com/cplusplus/
http://www.learncpp.com/

CSC102LProgrammingFundamentalsLabs Page53of110
LAB 7 – C++ LANGUAGE AND FUNCTIONS-I

Objectives
1. Writing functions which take no arguments.
2. Writing functions which take one or more arguments.
3. Writing functions which do not return a value.
4. Writing functions which return a value.

Task 7.1 Functions with No Arguments/One Argument


Write a function called ‘separator’ in C++ language which prints 15 *s in a single
line. Write another function named ‘multiples’ that takes an integer as an argument
and prints its multiples up to ten terms, separated by spaces, in a single line. Write a
C++ program which takes an integer ‘ N’ from user and calls these two functions to
generate tables of all numbers between 1 to N. Your program should have the
following interface.

Enter the value of N : 4


The tables from 1 to 4 are :
1 2 3 4 5 6 7 8 9 10
* * * * * * * * * * * * * * *
2 4 6 8 10 12 14 16 18 20
* * * * * * * * * * * * * * *
3 6 9 12 15 18 21 24 27 30
* * * * * * * * * * * * * * *
4 8 12 16 20 24 28 32 36 40

CSC102LProgrammingFundamentalsLabs Page54of110
Task7.2 Functions with a Return Value
An integer is said to be a Perfect Number if the sum of its divisors, including 1 (but
not itself), is equal to that number. For example, 6 is a perfect number, because 6 =
1+2+3. Write a C++ function called ‘IsPerfect’ that takes an integer as parameter and
returns 1 if the passed integer is a perfect number and 0 if it not a perfect number.
Use this function in a C++ program that reads in an integer value from user into a
variable named ‘N’ and prints all perfect numbers between 1 and N. Print the divisors
of each perfect number to confirm that the number is indeed a perfect number. Your
program should have the following interface.

Enter a value for N : 30


Perfect numbers between 1 and 30 are :
6 – Divisors are 1,2,3
28 – Divisors are 1,2,4,7,14

CSC102LProgrammingFundamentalsLabs Page55of110
Task7.3 Functions with more than One Arguments
Write a function in C++ language that takes four integers as arguments and returns
the largest of the four numbers. Use this function in a C++ program which takes four
integers from user and displays the largest on screen. Your program should have the
following interface.

Enter four integers :


25
32
57
41
The largest among 25,32,57,41 is 57.

CSC102LProgrammingFundamentalsLabs Page56of110
Task7.4 Functions with Arguments and a Return Value
Write a function in C++ language that takes an integer ‘num’ as argument and returns
the sum of all numbers between 1 to num. Use this function in a C++ program which
takes an integer from user, stores it in a variable named ‘N’ and displays the result of
the following function.

F(N) = 1 + (1+2) + (1+2+3) + ......+ (1+2+...N)

Your program should have the following interface.

Enter the value of N : 5


F(5)= 1 + 3 + 6 + 10 + 15 = 35

CSC102LProgrammingFundamentalsLabs Page57of110
Exercise # 7.1
Write functions in C++ language for calculating the area of a square, circle and
rectangle. Use these functions in a C++ program which takes a character input (‘s’ for
square, ‘c’ for circle and ‘r’ for rectangle) from user and displays the area of the
corresponding shape. Your program should prompt the user to enter lengths of sides
based on the specific character entered by user. Your program should have the following
interface.

Enter the first letter of the shape : S


Enter the length of its sides in cms : 5
Area of this square is 25 sq.cms
Perimeter of this square is 20 cms

CSC102LProgrammingFundamentalsLabs Page58of110
Exercise 7.2 (Reverse Digits)
Write a function that takes an integer value and returns the number with its digits
reversed. For example, given the number 7631, the function should return 1367.

CSC102LProgrammingFundamentalsLabs Page59of110
Exercise # 7.3 (Guess-the-Number Game)
Write a program that plays the game of “guess the number” as follows: Your program
chooses the number to be guessed by selecting an integer at random in the range 1 to
1000. The program then displays the following:

The player then types a first guess. The program responds with one of the following:

If the player’s guess is incorrect, your program should loop until the player finally gets
the number right. Your program should keep telling the player Too high or Too low to
help the player “zero in” on the correct answer.

CSC102LProgrammingFundamentalsLabs Page60of110
Web Resources

http://www.tutorialspoint.com/cplusplus/
http://www.learncpp.com/

CSC102LProgrammingFundamentalsLabs Page61of110
LAB 8 – C++ LANGUAGE AND FUNCTIONS-II

Objectives
1. Writing overloaded functions.
2. Passing arguments by reference.

Task8.1 Function Overloading


Write a C++ program using function overloading. The name of the overloaded
function is ‘square’. The different prototypes of this overloaded function are;
 voidsquare(void);//Used to print asolid squareof *swith sidelengthof 5
 voidsquare(char);//Usedtoprintasolidsquareofthecharacterpassedas
argument with side length of6
 voidsquare(char,int);//Usedtoprintasolidsquareofthecharacterand
thesidelengthpassedasargumentstothisfunction
Make calls to these functions one by one in your program.

CSC102LProgrammingFundamentalsLabs Page62of110
Task8.2 Passing by Reference
Write a function ‘swap’ in C++ language that swaps the values of two variables
passed as arguments to this function. Write a C++ program which takes values of two
integer variables from the user and swaps their values using function ‘swap’. Your
program should have the following interface.

Enter the first value : 6


Enter the second value :9
The two numbers have been swapped.
The first number is now9.
The second number is now 6.

CSC102LProgrammingFundamentalsLabs Page63of110
Exercise # 8.1 (Pass-by-Value vs. Pass-by-Reference)
Write a complete C++ program with the two alternate functions specified below, each of
which simply triples the variable count defined in main. Then compare and contrast the
two approaches. These two functions are a) function tripleByValue that passes a copy of
count by value, triples the copy and returns the new value and b) function
tripleByReference that passes count by reference via a reference parameter and triples
the original value of count through its alias (i.e., the reference parameter).
HINT (Output should look like the given snapshot):

CSC102LProgrammingFundamentalsLabs Page64of110
Web Resources

http://www.cplusplus.com/doc/tutorial/

CSC102LProgrammingFundamentalsLabs Page65of110
LAB 9 – C++ LANGUAGE AND FUNCTIONS-III

Learning Objectives
1. Using recursive functions.

Task9.1 Using Recursive Functions


a. Define a recursive function named ‘generate number’ which takes single
parameters named n and display the series of numbers from 1 to N.

b. Define a recursive function named ‘power’ which takes two parameters,


base and exponent, and returns the value equivalent to base multiplied by
itself, exponent times.

CSC102LProgrammingFundamentalsLabs Page66of110
Task9.2 Using Recursive Functions
Write a recursive function named ‘separator’ which takes two integer parameters and
finds the quotient and remainder when the first integer is divided by the second
integer. Write a C++ program which takes an integer input from the user, uses the
function ‘separator’ to separate the digits of this number and displays the reverse of
this number. Your program should have the following interface.

Enter a number : 3453


The number in reverse is : 3543

CSC102LProgrammingFundamentalsLabs Page67of110
Exercise # 9.1
Define recursive function ‘factorial’ which takes an integer as argument and returns the
value equivalent to this number’s factorial.

CSC102LProgrammingFundamentalsLabs Page68of110
Exercise # 9.2
Write a recursive function that displays integers less than or equal to a specific number
and greater than or equal to 0, in descending order. Use this function in a C++ program
which prompts the user to enter the value of N and displays all integers from N to 0 in
descending order.

CSC102LProgrammingFundamentalsLabs Page69of110
Web Resources

http://www.cprogramming.com/

CSC102LProgrammingFundamentalsLabs Page70of110
LAB 10 – ARRAYS-I

Objectives
1. Declaring and initializing one-dimensional arrays.
2. Inserting values into and printing values out of one-dimensional arrays.

Task 10.1 Declaring One-Dimensional Arrays


Write a C++ program, which declares;
 Integer type array of ‘10’Indexes
 Float type array of ’15’Indexes
 Double type array of ‘20’Indexes
 Character type array of ‘25’Indexes

Your program should set and print the values of each array. Your program should
have the following interface.

Size of integer array of 10 indexes is : 40 Bytes


Size of float array of 15 indexes is : 60 Bytes
Size of double array of 20 indexes is : 160 Bytes
Size of character array of 25 indexes is : 25Bytes

CSC102LProgrammingFundamentalsLabs Page71of110
Task 10.2 Initializing and Printing One-Dimensional Arrays
Write a C++ program, which declares;

 Integer type array of size ‘6’. Initialize each index of array with ‘12’ at the Time
ofDeclaration.
 Float type array of size ‘5’. Initialize each index of array with ‘0.5’ at the Time of
Declaration.
 Character type array of size ‘4’. Initialize each index of array with ‘a’ at the Time
ofDeclaration.
Your program should display the values stored in these arrays using the following
format.

Integer array : 12 12 12 12 12 12
Float array : 0.5 0.5 0.5 0.5 0.5
Character array : a aaa

CSC102LProgrammingFundamentalsLabs Page72of110
Task 10.3 Inserting Values into and Printing Values out of One-Dimensional
Arrays
Write a C++ program which prompts the user to enter integer, float and character
values into the arrays defined in Task 9.2. Your program should have the following
interface. Do this task using ‘for’, ‘while’, and ‘do/while’ structures one by one.

Enter values into Integer Array:


Location [0] : 3
Location [1] : 4
Location [2] : 10
Location [3] : 102
Location [4] : 21
Location [5] : 968

Enter values into Float Array:


Location [0] : 3.5
Location [1] : 4.1
Location [2] : 1.8
Location [3] : 102.4
Location [4] : 21.3

Enter values into Character Array:


Location [0] : s
Location [1] : r
Location [2] : o
Location [3] :i

The Integer Array values are : 3, 4, 10, 102, 21, 968


The Float Array values are : 3.5, 4.1, 1.8, 102.4, 21.3
The Character Array values are : s, r, o, i

CSC102LProgrammingFundamentalsLabs Page73of110
Exercise # 10.1
Write a C++ program which declares two integer arrays of 5 indexes each, reads in
integer values from user into one of these arrays, copies the values of this array into the
second array, reads values from the indexes of the second array one by one and prints the
respective number of *s on separate lines.

CSC102LProgrammingFundamentalsLabs Page74of110
Exercise # 10.2 (Searching through One-Dimensional Arrays)
Write a C++ program, which;
 Declares and initializes an integer array of 15indexes
 Prompts the user to enter a number (integer value)
 Searches through the array to find this number and
 Prints the respective index numbers where the user specified number has
been found

CSC102LProgrammingFundamentalsLabs Page75of110
Exercise # 10.3
Write a C++ program, which;
 Declares and initializes an integer array of 20indexes
 Finds the minimum, maximum, sum and average of the elements of this array
using functions and,
 Prints these results onscreen
Your program should have the following interface.

The integer array is :


90, 87, 23, 3, 8, 34, 67, 75, 45, 90,
1, 2, 3, 4, 34, 44, 4, 78, 56, 12
The minimum value is 1 found at index [10]
The maximum value is 90 found at index [0] [9]
The sum is 760
The average is 38

CSC102LProgrammingFundamentalsLabs Page76of110
Web Resources

http://www.cprogramming.com/

CSC102LProgrammingFundamentalsLabs Page77of110
LAB 11 – 2-DIMENSIONAL ARRAYS-I

Objectives
1. Declaring and initializing two-dimensional arrays.
2. Inserting values into and reading values out of two-dimensional arrays.
3. Traversing through two-dimensional arrays.

Task 11.1 Declaring, Initializing and Printing Two-Dimensional Arrays


Write a C++ program which initializes a 7x7 float type array with ‘0’ and then prints
it on screen. Your program should have the following interface.

The 7 X 7 array is :
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

CSC102LProgrammingFundamentalsLabs Page78of110
Task 11.2 Storing Values into Two-Dimensional Arrays
Write a C++ program which declares a 2x4-integer type array, prompts the user to
enter data into each element of this array and then prints this array on screen. Your
program should have the following interface.

Enter values for 2 rows and 4 columns.


Enter values for row 0 :
Location [0][0] :1
Location [0][1] :3
Location [0][2] :6
Location [0][3] : 7
Enter values for row 1
: Location [1][0] :7
Location [1][1] :9
Location [1][2] :3
Location [1][3] : 2
Your stored data is :
1 3 67
7 9 32

CSC102LProgrammingFundamentalsLabs Page79of110
Task 11.3 Traversing through Two-Dimensional Arrays
Write a C++ program which prompts the user to enter names of five countries, stores
them into a two-dimensional array, sorts them in alphabetical order and prints them
on screen. Your program should have the following interface.

Enter the names of five countries.


Pakistan
Afghanistan
Iran
Bangladesh
Turkey

The names of these countries in alphabetical order :


Afghanistan
Bangladesh
Iran
Pakistan
Turkey

CSC102LProgrammingFundamentalsLabs Page80of110
Exercise # 11.1
Write a C++ program which declares two 3 x 3int arrays (Matrices). Store random values
using nested for loops in array and perform Addition and Multiplication of Matrices,
stores the result of operation in another Matrix (output arrays). And displays the results.

CSC102LProgrammingFundamentalsLabs Page81of110
Web Resources

http://www.tutorialspoint.com/cplusplus/
http://www.learncpp.com/http://projects
yapa.com/cpp

CSC102LProgrammingFundamentalsLabs Page82of110
LAB 12 – 2-DIMENSIONAL ARRAYS-II

Objectives
1. Traversing through two-dimensional arrays.
2. Passing two-dimensional arrays to functions.
Task 12.1 Searching through Two-Dimensional Arrays
Write a C++ program, which
 Populates a two-dimensional array of size 10 x 10 with random numbers
between 0 and 9 (Hint: C++ has a built-in function for random number
generation)
 Prompts the user to enter a number
 Searches for this number in the 10 x 10 array and
 Displays location where the number is found in the array
Your program should display ‘Value not found’ if the user’s specified number is not
found in the 10 x 10 array. Your program should have the following interface.

Enter a value to find in the 10 x 10 array :


5
Found at location [0][5]
 In-Lab task #11.5
Found at location [1][9]
Found at location [4][6]
Found at location [7][0]
4 instances found.

The 10 x 10 array is :
8 9 6 4 6 5 7 8 1 2
4 6 3 1 1 2 8 7 4 5
6 7 8 3 4 2 3 2 3 1
2 3 2 1 4 0 6 7 8 4
0 1 9 4 7 6 5 3 2 3
2 1 2 1 3 4 3 7 8 6
6 7 8 1 2 1 3 2 4 1
5 1 2 1 1 1 3 4 7 6
7 8 7 6 4 4 3 1 2 3
4 3 2 1 4 7 8 3 0 1

CSC102LProgrammingFundamentalsLabs Page83of110
Task 12.2 Traversing through Two-Dimensional Arrays
Modify your C++ program for Task 12.1 such that the modified program searches
through the array to find maximum and minimum values and displays them with their
respective location numbers. Your program should have the following interface
(Considering the array as populated in Task 12.1).

MinimumValueis : 0
Found at location [3][5]
Found at location [4][0]
Found at location[9][8]
3 instances found.

Maximum Value is : 9
Found at location [0][1]
Found at location [4][2]
2 instances found.

The 10 x 10 array is :
8 9 6 4 6 5 7 8 1 2
4 6 3 1 1 2 8 7 4 5
6 7 8 3 4 2 3 2 3 1
2 3 2 1 4 0 6 7 8 4
0 1 9 4 7 6 5 3 2 3
2 1 2 1 3 4 3 7 8 6
6 7 8 1 2 1 3 2 4 1
5 1 2 1 1 1 3 4 7 6
7 8 7 6 4 4 3 1 2 3
4 3 2 1 4 7 8 3 0 1

CSC102LProgrammingFundamentalsLabs Page84of110
Task 12.3 Passing Two-Dimensional Arrays to Functions
Do Tasks 12.1 and 12.2 by using functions. You must pass respective arrays to these
functions as arguments.

CSC102LProgrammingFundamentalsLabs Page85of110
Exercise # 12.1
Write a C++ program to simulate the tic-tac-toe game for a 3 x 3 grid. You will use a
two-dimensional array of size 3 x 3. Your program must include functions and you must
pass your two-dimensional array to functions as argument. If you don’t know the rules of
this game, read on it on Wikipedia.

CSC102LProgrammingFundamentalsLabs Page86of110
Web Resources

http://en.wikipedia.org/wiki/Tic-tac-
toehttp://www.tutorialspoint.com/cpluspl
us/http://www.learncpp.com/

CSC102LProgrammingFundamentalsLabs Page87of110
LAB 13 – STRINGS

Task #13.1
Write a program that counts the total number of vowels in a sentence. Output the
frequency of each vowel.

CSC102LProgrammingFundamentalsLabs Page88of110
Task # 13.2
Write a program that inserts the characters "******" in the exact middle of a string.

CSC102LProgrammingFundamentalsLabs Page89of110
CSC102LProgrammingFundamentalsLabs Page90of110
Exercise # 13.1
Write a program that erases the sequences "by" and "BY" from a string.

CSC102LProgrammingFundamentalsLabs Page91of110
Exercise # 13.2
Write a program that inputs a sentence and counts the number of palindromes in the
sentence. A palindrome is a word that reads the same backwards and forwards. For
example, "tree" is not a palindrome but "noon" is

CSC102LProgrammingFundamentalsLabs Page92of110
Exercise # 13.3
Write a program that separately inputs a first name and a last name and then concatenates
the two into a new string.

CSC102LProgrammingFundamentalsLabs Page93of110
LAB 14 – STRUCTURES

Objectives
1. Declaring structures.
2. Traversing through structures.

Task 14.1 Declaring Structures


Write a C++ program for maintaining the library record of 100 books. Each record is
composed of 6 fields which include book’s library number, book’s name, author’s
name, edition number, year of publishing and price of the book. Choose appropriate
data types to represent each field. Your program should prompt the user to populate
10 records of this database with some values and then print these values on screen.
You must use array of structures in your program. Your program should have the
following interface.

Enter values for book 1 :


Book’s Name : A Guide to C Language Programming
Author’s Name : XYZ
Edition Number : 1
Year of Publishing : 2013
Price of Book : 250

CSC102LProgrammingFundamentalsLabs Page94of110
Task 14.2 Traversing through Structures (use structure array)
Modify the C++ program for Task 14.1 such that the modified program prompts the
user to enter an integer from 1 to 6 (each number indicates a specific field of the
library books’ records). Based on the specific number entered, your program should
ask the user to enter a specific value to search in the library database. If the specific
value is found, your program should display the corresponding record(s), otherwise it
should print ‘No match found’ on the screen. Your program should have the
following interface.

You can search for a specific book. Press an integer from 1 to


6 to specify the searchitem.
1. Book’s LibraryNumber
2. Book’sName
3. Author’sName
4. EditionNumber
5. Year ofPublishing
6. Price ofBook

Assume the user enters the integer 3.

Enter the Author’s Name :


XYZ

1 Book Found.
Book 1: A Guide to C Language Programming by XYZ, Edition 1
published in 2013. Price 250

CSC102LProgrammingFundamentalsLabs Page95of110
Exercise # 14.1
Do Tasks 14.1 and 14.2 using linked list.

CSC102LProgrammingFundamentalsLabs Page96of110
Web Resources

www.cplusplus.com
msdn.microsoft.com/en-us/library/64973255.aspx
www.cprogramming.com/tutorial/lesson7.html

CSC102LProgrammingFundamentalsLabs Page97of110
LAB 15 – POINTERS

Objectives
1. Declaring and initializing pointers.
2. Using pointer arithmetic.
3. Passing pointer variables to functions.

Task 15.1 Declaring and Initializing Pointers


Write a C++ program which creates pointer variables for the data types int, float,
char and double, initializes these pointers with the addresses of variables of
respective data types and prints the size of pointer and the data held by these
pointers.

CSC102LProgrammingFundamentalsLabs Page98of110
Task 15.2 Using Pointer Arithmetic
Write a C++ program which declares and initializes a 1D – int array with ten
elements. Initialize the elements of array using for loop and display the contents of
array using a pointer.

CSC102LProgrammingFundamentalsLabs Page99of110
Task 15.3 Passing Pointer Variables to Functions
Repeat task 15.2 i.e. write function to display the output of array with a pointer
argument to array. The output should look like.

The contents of array are:


Index[0] = 0 Index[1] = 0 Index[2] = 0 Index[3] =0

Index[4] = 0 Index[5] = 0 Index[6] = 0 Index[7] =0

Index[8] = 0 Index[9] = 0

CSC102LProgrammingFundamentalsLabs Page100of110
Exercise # 15.1
Write the following program in visual studios and explain each line of code considering
the output.

int i; //A
int * ia; //B
cout<<"The address of i "<<&i << " value="<<i <<endl;
cout<<"Theaddressofia"<<&ia<<"value="<<ia<<endl;
i=10; //C
ia = &i; //D

cout<<"afterassigningvalue:"<<endl;
cout<<"The address of i "<<&i << " value="<<i <<endl;
cout<<"Theaddressofia"<<&ia<<"value="<<ia<<"pointto: "<<*ia;

CSC102LProgrammingFundamentalsLabs Page101of110
Web Resources

http://en.wikipedia.org/wiki/Tic-tac-
toewww.cplusplus.com/doc/tutorial/pointers/www.t
utorialspoint.com/cplusplus/cpp_pointers.htmwww.
youtube.com/watch?v=W0aE-w61Cb8

CSC102LProgrammingFundamentalsLabs Page102of110
LAB 16 – POINTERS AND ARRAYS

Learning Objectives
1. Traversing through 2D-arrays.
2. Passing pointer variables to functions.

Practice Questions
You can practice the following problems in this lab.
Task 16.1
Write the following program make changes and check the effect of these changes in
output.

CSC102LProgrammingFundamentalsLabs Page103of110
Task 16.2 Passing Pointer Variables to Functions
Repeat the above task but instead of passing array to function, use pointer as
argument to display the contents of array.

CSC102LProgrammingFundamentalsLabs Page104of110
Exercise # 16.1
Repeat Task 16.1 and 16.2 using 2-D String arrays.

CSC102LProgrammingFundamentalsLabs Page105of110
Web Resources

http://en.wikipedia.org/wiki/Tic-tac-
toewww.cplusplus.com/doc/tutorial/pointers/www.t
utorialspoint.com/cplusplus/cpp_pointers.htmwww.
youtube.com/watch?v=W0aE-w61Cb8

CSC102LProgrammingFundamentalsLabs Page106of110
LAB 17 – FILE PROCESSING

Learning Objectives
1. Creating files
2. Writing data to file
3. Reading data from file

Task 17.1
Write a program which opens a file in reading and writing mode. After writing
information about user i.e. name, age, city, contact no, entered by the user to a file
named sample.txt, the program reads information from the file and outputs it onto the
screen.

CSC102LProgrammingFundamentalsLabs Page107of110
Exercise # 17.1
Write a C++ program to keep records and perform statistical analysis for a structure of
20 students. The information of each student contains id, name, gender, quizzes scores (2
quizzes per semester), mid-term score, final score, and total score.

The program will prompt the user to choose the operation of records from a menu as
shown below:

===================================================
MENU
===================================================

1. Add studentrecords

2. Delete studentrecords

3. Update studentrecords

4. View all studentrecords

5. Calculate an average of a selected student’sscores

6. Calculate total scores of a selectedstudent

7. Display the highest and lowestscores

8. Sort students’ records byID

9. Sort students' records by totalscore

Enter your choice:

CSC102LProgrammingFundamentalsLabs Page108of110
Exercise # 17.2
Write a program to read data from a file called lowercase.txt, and then write data in
another file called uppercase.txt. The content of the input file with all its letters in
uppercase as shown.

Example:lowercase.txt uppercase.txt
pakistan PAKISTAN
india INDIA
china CHINA

CSC102LProgrammingFundamentalsLabs Page109of110
APPENDIX – ASCII TABLE

CSC102LProgrammingFundamentalsLabs Page110of110

You might also like