CS-114 Fundamentals of Programming Updated
CS-114 Fundamentals of Programming Updated
PREPARED BY
This Lab manual has been prepared by Lec. Adeela Waqar under the supervision of Dr. Naveed Iqbal Rao,
Head of Computer Software Engineering Department, in the year 2013.
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/illustrations must be solved in the space 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.
e. Students are required to go through the experiment before coming to the lab session. Lab session details will
be given in training schedule.
h. Plagiarism is strictly forbidden. No credit will be given if a lab session is plagiarized and no resubmission
will be entertained.
VERSION HISTORY
Date Updated By Details
August 2013 Lec Adeela Waqar First Version Created
At the end of the course the students will be able to: PLOs BT Level*
1. Recognize syntax and semantics of different programming 1 C-2
Languages.
2. Apply basic algorithms for identifying and solving real world 2 C-3
problems.
3. Complete the program coding , compilation and execution with 5 P-4
confidence
9 1-DIMENSIONAL ARRAYS 3 1
10 2-DIMENSIONAL ARRAYS 3 1
13 STRUCTURES 3 1
14 Lab Exam 3 1
Instructor
Max. Marks Obtained
Date Experiment Sign
Marks
R1 R2 R3 R4 R5 R6
Grand Total
Learning Objectives
In today’s lab, you will practice;
1. To become familiar with the login process and the C++ environment used in the lab
2. To understand the basics of program design and algorithm development
3. To learn, recognize and correct the three types of computer errors: syntax errors
4. To learn the basics of an editor and compiler and be able to compile and run existing programs
5. To enter code and run a simple program from scratch
Practice Questions
You can practice the following problems in this lab.
Problem solving
Phase
Implementation
Create a simple tip calculator. The program should prompt for a bill amount and a tip rate. The program must
compute the tip and then display both the tip and the total amount of the bill.
When you’ve finished writing the basic version of the program, try tackling some additional challenges:
Ensure that the user can enter only numbers for the bill amount and the tip rate. If the user enters non-numeric
values, display an appropriate message, and exit the program.
Expected result: Please enter a valid number for the bill amount
Working with multiple inputs and currency can introduce some tricky precision issues.
Create a simple self-checkout system. Prompt for the prices and quantities of three items. Calculate the subtotal
of the items. Then calculate the tax using a tax rate of 5.5%. Print out the line items with the quantity and total,
and then print out the subtotal, tax amount, and total.
Total: $67.52
Constraints
• Keep the input, processing, and output parts of your program separate. Collect the input, then do the
math operations and string building, and then print out the output.
• Be sure you explicitly convert input to numerical data before doing any calculations.
Challenges
• Revise the program to ensure that prices and quantities are entered as numeric values. Don’t allow the
user to proceed if the value entered is not numeric.
• Alter the program so that indeterminate number of items can be entered. The tax and total are computed
when there are no more items to be entered
•
16 CPS 110 Fundamentals of Programming – C Building Blocks I
Introduction to Compiler
(Visual Studio 2010)
1. Creating a new project:
Go to the File menu. Select
New. IntheProjectTab, select
Win32 Console Application.
Write the
Project Name. You can
change the default location
of the project from the
location box. Check
C language
C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchieto develop the
UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in
1972.In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now
known as the K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX application
programs have been written in C.
C has now become a widely used professional language for various reasons:
• Easy to learn
20 CPS 110 Fundamentals of Programming – C Building Blocks I
• Structured language
• It produces efficient programs
• It can handle low-level activities
• It can be compiled on a variety of computer platforms
Why Use C?
C was initially used for system development work, particularly the programs that make-up the operating system.
C was adopted as a system development language because it produces code that runs nearly as fast as the code
written in assembly language. Some examples of the use of C might be:
• Network Drivers
• Modern Programs
• Databases
• Language Interpreters
• Utilities
C++ language: C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form programming.
Sample Program
#include <iostream>
int main()
Lab-02 Task
Task 2.2: Write a program to print the following table using tab character:
Input :
Output :
Output :
Task 2.4: Write a program to print the letter 'A' using stars.
Output:
Output:
Input :
Output :
_____ _____
|_____| >>----------------->> |_____|
|—----> <----|
|—------------> <—-------- |
|—----—----—-------> <------------------|
|—------------> <—-------- |
|—----> <----|
Input :
Output :
Input:
Output:
Learning Objectives
In today’s lab, you will practice;
Practice Questions
You can practice the following problems in this lab.
Task 3.1 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.
Input :
Output:
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
Input :
MCS
Input :
Output:
This is a test.
He asked, “How are you doing?”
“Education is the most powerful weapon which you can use to ‘CHANGE’ the world”.
S
Input:
Ideally, the values entered by user should not be changed while your program is executing, otherwise your
program’s calculations will be based on modified values and thus, will be considered incorrect. Is there any
way to ensure that your program does not accidently modify the values entered by user? If yes, please write
appropriate C++ code to incorporate this feature. If no, explain why it is not possible. Do you think the C++
reserved word ‘const’ can be useful here?
Output of your program must be presentable and self-explanatory. You may use different escape sequences
to customize output of your program.
Web Resources
Input:
Learning Objectives
In today’s lab, you will practice;
1. Taking a single character input from user using different built-in functions.
Practice Questions
You can practice the following problems in this lab.
Input :
Output:
Input :
Output:
Input:
Output:
Enter a number: 20
20 + 5 = 25
20 – 3 = 17
(20 + 3)- 2 = 21
((20 + 5) *2 / (20 + 3)) = 2
Input:
Output:
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
3. (a + b)3 = a3 + 3a2b + 3ab2 + b3
4. (a - b)3 = a3 - 3a2b + 3 ab2 - b3
5. a2 - b2 = (a - b) (a + b)
6. a3 - b3 = (a - b)3 + 3 a b (a - b)
7. a3 - b3 = (a - b) (a2 + a b + b2)
8. a3 + b3 = (a + b) (a2 - a b + b2)
9. a3 + b3 = (a + b)3 - 3 a b (a + b)
Web Resources
http://www.cprogramming.com/
Input:
Learning Objectives
In today’s lab, you will practice;
Practice Questions
You can practice the following problems in this lab.
Task 5.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.
Input:
Input:
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.
Input:
Output:
Input:
Output:
It is an odd digit.
Enter a value: H
It is an alphabet in UPPERCASE.
Enter a value: *
It is a special symbol.
Input:
Output:
1.
3.
4.
7
5 + 7 = 12
Input:
Output:
1.
2.
50 CPS 110 Fundamentals of Programming – C++ Language And Functions II
3.
Web Resources
http://math.about.com/od/businessmath/ss/Interest_2.htm http://www.tutorialspoint.com/cplusplus/
Input:
Task 1:
Write a program to calculate gross salary and net salary. Besides, gross salary and net salary, you are
also required to print medical allowance, house rent and tax. The input is basic salary. The details are as under:
Gross Salary = Basic Salary + Medical Allowance + Conveyance allowance + House rent
Tax:
Note: For tax calculation multiply gross salary with 12 and calculate annual gross salary, apply check on annual
Sample Run:
Input: Basic Salary = 30000
Output:
-----------------------------
-----------------------------
-----------------------------
Input:
Write a program to input the amount (deposited in the bank). Calculate and print the amount after each
year for three years, where compound interest rate is 7% (if amount deposited is less than equal 5 lac), and
otherwise compound interest rate will be 10%:
Sample Run:
----------------------------------------------------------
Output:
1.
Task 3:
Write a program to generate a random number 0 to 20, check and print the number in English words.
Sample Run:
Output:
Learning Objectives
In today’s lab, you will practice;
1. Using ‘for’ repetition structure.
Practice Questions
You can practice the following problems in this lab.
Task 6.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
Input:
Input:
Output:
Input:
Output:
Task 6.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.
Output:
Output:
Web Resources
http://www.tutorialspoint.com/cplusplus/ http://www.learncpp.com/
Output:
Input:
Output:
Output:
Input:
Output:
Output:
Output:
Output:
Output:
Output:
Learning Objectives
In today’s lab, you will practice;
1. Using nested ‘for’ repetition structure.
01:00:00
01:00:01
01:00:02
:
:
Input:
Output:
Input:
Input:
Output:
***
*****
***
*
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
Output:
Sum = 11 + 22 + 33 +……+NN
Enter a number: 4
Sum = 1^1 + 2^2 + 3^3 + 4^4 = 288
Output:
Web Resources
http://www.tutorialspoint.com/cplusplus/ http://www.learncpp.com/
Practice Questions
You can practice the following problems in this lab.
Task 8.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.
* * * * * * * * * * * * * * * 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
Output:
Input:
Output:
Web Resources
1. http://www.tutorialspoint.com/cplusplus/
Learning Objectives
In today’s lab, you will practice;
3. Writing macros.
Practice Questions
You can practice the following problems in this lab.
Task 9.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;
• void square(void); // Used to print a solid square of *s with side length of 5
• void square (char); // Used to print a solid square of the character passed as argument with side
length of 6
• void square(char, int); // Used to print a solid square of the character and the side length passed as
arguments to this function
Make calls to these functions one by one in your program.
Enter a number: 6
The square of 6 is: 36
The cube of 6 is: 216
The values of character variables can either be ‘l’ for left or ‘r’ for right. The first integer after both
characters indicates the number of shifts required and the second one indicates the additive constant to be
added to each digit of the four-digit integer supplied as the fifth input.
Your program should calculate the value and display result on screen. A sample output is given below. You
will have to separate the digits of the fifth input before passing them to circular shift functions.
Enter the input.
L
2
1
R
1
0
1234
The result is 3452
1234 will be shifted left 2 (indicated by input # 1 and 2) times yielding (2341 -> 3412). All digits of this
number will now be incremented by the additive constant 1(indicated by input #3), thus yielding 4523. This
number will now be shifted right by 1 place (indicated by input # 4 and 5), thus yielding 3452. All digits of
this number will now be incremented by the additive constant 0(indicated by input #6), thus yielding 3452
as the final output.
1. http://www.cplusplus.com/doc/tutorial/
Learning Objectives
In today’s lab, you will practice;
1. Writing functions in separate header files and including them in your main file.
Practice Questions
You can practice the following problems in this lab.
Task 10.1 Using Header Files
Repeat Task 8.1, 8.2, 8.3 and 8.4 placing the defined functions in separate header files.
1. http://www.cprogramming.com/
Learning Objectives
In today’s lab, you will practice;
Practice Questions
You can practice the following problems in this lab.
Task 11.1 Declaring One-Dimensional Arrays
Write a C++ program, which declares;
• Integer type array of ‘10’ Indexes <
Your program should calculate and print the size of each array. Your program should have the following
interface.
• Integer type array of size ‘6’. Initialize each index of array with ‘12’ at the Time of Declaration.
• 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 of Declaration. Your
program should display the values stored in these arrays using the following format.
Integer array : 12 12 12 12 12 12
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
• Prints the respective index numbers where the user specified number has been found Your program
should have the following interface
• Finds the minimum, maximum, sum and average of the elements of this array and
1. http://www.cprogramming.com/
2. http://www.cprogramming.com/
3. http://www.dreamincode.net/
Practice Questions
You can practice the following problems in this lab.
Task 12.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.
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 6 7
Bangladesh
Iran
Pakistan
Turkey
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.
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 isnterface (Considering the array as populated in Task 12.1).
Minimum Value i: 0
Found at location [3][5]
Found at location [4][0]
Found at location [9][8]
3 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
1. http://www.tutorialspoint.com/cplusplus/
2. http://projectsyapa.com/cpp
3. http://en.wikipedia.org/wiki/Tic-tac-toe
Practice Questions
You can practice the following problems in this lab.
Task 13.1 Declaring and Initializing Pointers
Write a C++ program that 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 pthe ointer and
the data held by these pointers.
It is displayed as:
I
am
a
student
1. http://en.wikipedia.org/wiki/Tic-tac-toe
2. www.cplusplus.com/doc/tutorial/pointers/
3. www.tutorialspoint.com/cplusplus/cpp_pointers.htm
4. www.youtube.com/watch?v=W0aE-w61Cb8
Practice Questions
You can practice the following problems in this lab.
Task 14.1 Traversing through Strings
Write a C function called ReplaceNot that takes a string as a parameter, searches all three letter words in
the string passed as argument and replaces them with ‘Not’. Apply all the checks and cases. For example, if
the passed string is: “Every boy has a pen”. Your function should modify this string to: “Every Not Not a
Not”. Use this function in your main program to verify that your function works correctly.
Task 14.2 Passing Pointer Variables to Functions
Write a C function called ConvertToUpper that takes a string as a parameter, converts all lowercase
characters occurring in this string to uppercase and displays the modified string on screen. Use this function
in your main program to verify that your function works correctly.
Web Resources
1. http://en.wikipedia.org/wiki/Tic-tac-toe
2. www.tutorialspoint.com/cplusplus/cpp_pointers.htm
3. www.youtube.com/watch?v=W0aE-w61Cb8
1. Declaring structures.
Practice Questions
You can practice the following problems in this lab.
Task 15.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.
16 –
9
3. Author’s Name
4. Edition Number
5. Year of Publishing
6. Price of Book
Assume the user enters the integer 3.
3
1 Book Found.
Book 1: A Guide to C Language Programming by XYZ, Edition 1
published in 2013. Price 250
Web Resources
1. www.cplusplus.com
2. msdn.microsoft.com/en-us/library/64973255.aspx
3. www.cprogramming.com/tutorial/lesson7.html
17 –
0
CPS 110 Fundamentals of Programming Structures
17 –
1
“I hear and I forget,
I see and I remember,
I do and I understand”
Confucius