Programming Fundamentals 2020 Workbook
Programming Fundamentals 2020 Workbook
(CS02118)
WORK BOOK
Revised Date
March 2020
It is certified that the lab manual titled “Programming Fundamentals”, in scope and in quality,
covers the objectives and topics defined in the course outline.
Taxonomy Mapped
CLO # CLO Description Domain
Level PLO
3 Selection Statements / Decisions (Switch) CLO 4,5,6 PLO 3,8,9 C3, A3, A5
4 Selection Statements / Decisions (If / Else) CLO 4,5,6 PLO 3,8,9 C3, A3, A5
5 Repetition Structures / Loops (For loop) CLO 4,5,6 PLO 3,8,9 C3, A3, A5
9 Design of ATM Model (Open-Ended Lab) CLO 4,5,6 PLO 3,8,9 C3, A3, A5
16 Banking transaction scenario (Open-Ended Lab) CLO 4,5,6 PLO 3,8,9 C3, A3, A5
1 INTRODUCTION TO COMPILER
1.1 PROCEDURES
• A compiler is a program that translates the source code for another program from
a programing language into executable code
• Make sure your code is accurate as you know that c language is k sensitive that’s why
• Now run the program you will see a black window in which your output is visible
• Do it as many times for your own practice
Before making the program, you must have some basic knowledge of the C++ language
and also, be careful while making a program.
1.4.3 How you can add a new C++ Source file to the active project.
To add the new file to the project, bring up the file template wizard through either File
New File or Main Toolbar New file .File. Select C/C++ source and click Go. Continue
through the following dialogs very much like the original project creation, selecting C++
when prompted for a language.
GENERALIZED LAB RUBRICS
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS
2.1 PROCEDURES
• Before using C++ language, you must know that this language is k sensitive so that’s why
be careful while using this language
• Now construct a program which is assign to you for practice
• Recheck your program twice so that the chances of mistake can be minimize
Before making the program, you must have some basic knowledge of the C++ language
and also, be careful while making a program.
With the help of this lab I am able to contract different kind of programs.
2.4 EXERCISE QUESTIONS
Low level language is machine readable form of program. Whereas the high-level
language will be in human readable form.
There are three kinds of errors: syntax errors, runtime errors, and logic errors.
Syntax error:
A syntax error is an error in the source code of a program.
Runtime errors:
A runtime error is a program error that occurs while the program is running.
logic errors:
A logic error is an error in the program
2.4.4 Write a program to print your introduction in output window.
Code Output
Name: BURHAN
#include<iostream>
NAJEEB
using namespace std;
Reg No: BSEE-
int main() 02183108
{ Location: LAHORE
return 0; continue.
}
GENERALIZED LAB RUBRICS
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
3 SELECTION STATEMENTS/DECISIONS (IF/ELSE & SWITCH)
3.1 PROCEDURES
Before making the program, you must have some basic knowledge of the C++ language
With the help of this program I am able to construct different program using if else statement.
3.4 EXERCISE QUESTIONS
3.4.1 Write a C++ Program in this we are taking a character from keyboard and
checking whether it is Vowel or Consonant, before it we are checking it is valid
alphabet or not?
Write a C++ Program to print positive number entered by the user If user enters negative
number, it is skipped.
Write a program to determine whether a given number is 5-digit number or not. Further
check if it’s a Numeric Palindrome.
GENERALIZED LAB RUBRICS
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS
4.1 PROCEDURES
Switch case statement is used when we have multiple conditions and we need to
perform different action based on the condition. When we have multiple conditions and
such case we can use switch case. Switch Case statement is mostly used with break
The expression is evaluated once and compared with the values of each case label.
If there is a match, the corresponding code after the matching label is executed
If there is no match, the code after default: is executed.
4.3 LEARNING OUTCOMES
we learn the use of switch statement if C++ programing and become familiar with the
4.4.1 Write a C++ Program to Make a Simple Calculator to Add, Subtract, Multiply
or Divide Using switch...case.
Code Output
#include <iostream> Enter operator either +
or - or * or /: +
using namespace std; Enter two operands: 2
int main() 2
{ 4
char op;
float num1, num2;
cout<< "Enter operator either + or - or * or /: ";
cin>> op;
cout<< "Enter two operands: ";
cin>> num1 >> num2;
switch(op)
{
case '+':
cout<< num1+num2; break;
case '-': cout<< num1-num2;
break;
case '*': cout<< num1*num2; break;
case '/':
cout<< num1/num2; break;
cout<< "Error! operator is not correct"; break;
}
return 0;
}
4.4.2 Write a program that will find the grade of a number given his marks out of
100.
Code Output
#include <iostream> enter the obtain
marks=60
using namespace std; Your Grade Is: C
int main()
{
int marks;
cout<< "enter the obtain marks="<<endl;
cin>>marks;
switch(marks/10)
{case 10:
case 9:
cout<< "\nYour Grade Is: A ";
break;
case 8:
case 7:
cout<< "\nYour Grade Is: B ";
break;
case 6:
cout<< "\nYour Grade Is: C ";
break;
case 5:
case 4:
cout<< "\nYour Grade Is: D or Pass";
break;
default:
cout<< "\nYou Grade Is: F ";}
Return 0;
}
Code Output
#include <iostream> Enter any number to
check even or odd:2
using namespace std; Number is even
int main(){
intnum;
cout<<"Enter any number to check even or odd: ";
cin>>num;
switch(num % 2)
{
case 0: cout<<"Number is even";
break;
case 1: cout<<"Number is odd";
break;
}
return 0;
}
GENERALIZED LAB RUBRICS
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS
5.1 PROCEDURES
Before making the program, you must have some basic knowledge of the C++ language
In this lab I have learned to design different programs using while and do while conditions.
5.4 EXERCISE QUESTIONS
5.4.1 Write a C++ program to find if the given number is a perfect number and find
its all positive divisors excluding that number
Code Output
Enter number to be checked
#include<iostream>
:
using namespace std; 56
{ 1
7
for (i=1; i < num; i++)
{ 8
div = num % i;
if (div == 0) 14
sum = sum + i;
} 28
if (sum == num)
cout << num <<" is a perfect number."<< endl;
Else
cout << num <<" is not a perfect number."<< endl;
{
if(num%i==0)
cout<<i<<endl;
}
return 0;
}
5.4.2 Write a C++ program to make custom countdown timer using while loop.
Code Output
#include <iostream> 68
int main()
int n;
cin>>n;
Sleep(1000);
while (n >= 1)
Sleep(1000);
n--;
Code Output
cin >> n;
if(i == 1)
continue;
if(i == 2)
continue;
num1 = num2;
num2 = nextTerm;
}
return 0;
}
5.4.4 Write a program that takes input of two integer type variables from user then prints
their LCM (Least Common Multiple).
Code Output
do
break;
else
++max;
} while (true);
return 0;
}
GENERALIZED LAB RUBRICS
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS
6.1 PROCEDURES
Before making the program, you must have some basic knowledge of the C++ language
In this lab I have learned to design different programs using loops and for loop conditions.
6.4 EXERCISE QUESTIONS
6.4.1 A program that gets starting and ending point from the user and displays all
odd numbers in the given range.
Code Output
{ 12
if(num % 2 !=0)
return 0;
}
6.4.2 Program that takes input of an integer type variable from user then calculates
and prints its factorial.
Code Output
#include<iostream> Enter Number:
int num,factorial=1;
cin>>num;
factorial=factorial*a;
Code Output
Enter value to find highest
#include<iostream>
power of 2 is:
using namespace std; 34
(int n)
{
int res = 0;
for (int i=n; i>=1; i--)
{
if ((i & (i-1)) == 0)
{
res = i;
break;
}
}
return res;
}
int main()
{
int n;
cout<<"Enter value to find highest power of 2 is: "<<endl;
cin>> n;
cout <<"Highest power of 2 is: "<<
highestPowerof2(n)<<endl;
return 0;
}
6.4.4 Write a program to print following pattern using Nested Loop.
*****
****
***
**
*
Code Output
{ ****
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS
7.1 PROCEDURES
Before making the program, you must have some basic knowledge of the C++ language
In this lab I have learned to design different programs using recursive conditions.
7.4 EXERCISE QUESTIONS
7.4.1 Write a program to find and print Roots of a quadratic equation using pre-
defined functions.
Code Output
Enter coefficients a, b and
#include <iostream>
c:
#include <cmath> 3
int main() 5
{
Roots are complex and
float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
different.
cout << "Enter coefficients a, b and c: "<<endl; x1 = -0.666667+1.10554i
cout << "x1 = " << realPart << "+" << imaginaryPart << "i"
<< endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i"
<< endl;
}
return 0;
}
Code Output
Enter number to find
#include <iostream>
factorial
using namespace std; 4
{
if ((n==0)||(n==1))
return 1;
else
return n*fact(n-1);
}
int main()
{
int n;
cin>>n;
Code Output
if (!isalpha(c))
cout << c << " is not an alphabetical character." << endl;
else
{
int case_val;
if (c >= 'a' && c <= 'z')
{
c = c - 'a' + 'A';
case_val = 1;
}
else if (c >= 'A' || c <= 'Z')
{
c = c + 'a' - 'A';
case_val = 0;
}
cout << c << " is the " << ( (case_val == 1) ? "upper" :
"lower" )
<< " case of given character " << endl;
}
return 0;
}
7.4.4 Write a recursive function that finds out whether a number is even or odd.
You can’t use % operator. If you subtract 2 repeatedly from a number then
finally the number will be reduced to 0 or 1 that would be base case for
recursive call
Code Output
#include <iostream> Enter a number to find odd
or even
#include <conio.h> 7
int isEven(int);
int main()
{
int num;
if(isEven(num))
{
cout<<"It is a even number";
}
else{
getch();
return 0;
}
int isEven(int num)
{
if(num==0)
{
return 1;
}
else if(num==1)
{
return 0;
}
else if(num<0)
{
return isEven(-num);
}
Else
return isEven(num-2);
}
GENERALIZED LAB RUBRICS
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS
8.1 PROCEDURES
The call by reference method of passing arguments to a function copies the reference of an
argument into the formal parameter. Inside the function, the reference is used to
access the actual argument used in the call. This means that changes made to the
To pass the value by reference, argument reference is passed to the functions just like
any other value. So accordingly, you need to declare the function parameters as reference
types as in the following function swap (), which exchanges the values of the two integers
In call by reference, the operation performed on formal parameters, affects the value
of actual parameters because all the operations performed on the value stored in the
In this lab we learned about functions (call by reference) and become familiar with the
properties
8.4.1 In main declare variables x and y. Write the definition of the function that gets
the value of x. The function then changes the value of x by assigning the value
of the expression 2 times the (old) value of x plus the value of y minus the value
entered by the user.
Code Output
#include<iostream>
using namespace std;
void funcone(int x);
int main() * Before Fun Of X and
{ Y*
int x,y; X= 5
cout<<"* Before Fun Of X and Y *"<<endl; Y= 3
x=5; ** After Fun X and Y **
y=3; X= 8
cout<<"X= "<<x<<endl;
cout<<"Y= "<<y<<endl; Process returned 0 (0x0)
funcone(x); execution time : 0.018 s
return 0; Press any key to
} continue.
void funcone(int x)
{
int y=3;
x=((2*5)+3)-5;
cout<<"** After Fun X and Y **"<<endl;
cout<<"X= "<<x<<endl;
}
8.4.2 Write the definition of the function next Char that sets the value of z to the
next character stored in z using reference variable.
Code Output
#include <iostream>
#include <iomanip>
using namespace std ; Enter character :
void nextChar ( char ) ; a
int main() The character stored in
{ 'ch' after incriment is: b
char z ;
nextChar ( z ) ;
} Process returned 0 (0x0)
void nextChar ( char ch ) execution time : 2.252 s
{ Press any key to
cout << "Enter character : " << endl ; continue.
cin>>ch;
ch = ++ch ;
cout << "The character stored in 'ch' after incriment
is: " << ch << endl << endl ;
}
8.4.3 Write a program that will take an integer value in main function. And use a
void function to convert it to its absolute value.
Code Output
#include <iostream> Enter 6 numbers:
using namespace std; 1
int main() { 5
int numbers[2][3]; 4
cout << "Enter 6 numbers: " << endl; 5
for (int i = 0; i < 2; ++i) { 5
for (int j = 0; j < 3; ++j) { 4
cin >> numbers[i][j]; The numbers are:
} numbers[0][0]: 1
} numbers[0][1]: 5
cout << "The numbers are: " << endl; numbers[0][2]: 4
for (int i = 0; i < 2; ++i) { numbers[1][0]: 5
for (int j = 0; j < 3; ++j) { numbers[1][1]: 5
cout << "numbers[" << i << "][" << j << "]: " << numbers[1][2]: 4
numbers[i][j] << endl;
} Process returned 0 (0x0)
} execution time : 8.383 s
return 0; Press any key to
} continue.
8.4.4 Write a program that will take two numbers as first and second numbers of
Fibonacci Series. And then generate the series up to the given number using a
void function.
Code Output
#include <iostream> enter the position
using namespace std; 3
int main() the fabonnic series are as
follows
{ 12
int fib1 = 0, fib2 = 1, fib3 = 1,n;
cout<<"enter the position" <<endl;
cin>>n;
cout<<"the fabonnic series are as follows" <<endl;
while (fib1 + fib2 <n)
{
fib3 = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
cout<< fib3 << " ";}
cout<<endl;
return 0;
}
GENERALIZED LAB RUBRICS
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS
9.1 PROCEDURES
The code carries out all the functions that all standard atm machines do. You can check
amount present in your account, withdraw balance and deposit amount.
When you run the project compiled code in visual studio ide or its exe file. A text
message will pop up on the screen of the display “PLEASE ENTER THE PINCODE “.
Now enter the pin code assigned to users above. Pin code must be of the person in
whose account you want to withdraw, deposit or print the balance receipt.
When you run the project compiled code in visual studio ide or its exe file. A text
message will pop up on the screen of the display “PLEASE ENTER THE PINCODE “.
Now enter the pin code assigned to users above. Pin code must be of the person in
whose account you want to withdraw, deposit or print the balance receipt.
In this open-ended lab, I have come to know about the security system of the Bank ATMs
And I also learned how to construct a security code for ATM
9.4 EXERCISE QUESTIONS
9.4.1 Write a C++ program that will allow a consumer to withdraw funds from their
bank accounts (savings or current) using ATM machine. The program can
check the balance of their bank account, choose the amount to withdraw, show
the remaining balance etc. Assume some amount as initial balance in account.
Code Output
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
int main ()
{ Enter your pin: 1998
int deposit=0;
int amount=5000; Pin Approved!
int withdraw= 0;
int choice=0; Welcome to Burhan's
int pin=0; special ATM
char keypin; 1. Check Balance
keypin = 1998; 2. Withdraw
char comppin; 3. Deposit
4. Exit
cout << "Enter your pin: ";
cin >> pin;
s:
cout << "\nEnter Number: ";
cin >> choice;
if (choice==1)
{
cout <<"\nYour current balance is: \n" << amount;
goto s;
}
else if (choice==2)
{
cout << "\nEnter the amount you want to withdraw: ";
cin >> withdraw;
if (withdraw>amount)
{
cout << "\nYou don't have sufficient balance.\n";
goto s;
}
else
{
amount=amount-withdraw;
cout << "\nYour current balance is: \n" << amount;
goto s;
}
}
else if (choice==3)
{
cout << "\nEnter the amount you want to deposit: ";
cin >> deposit;
amount=amount+deposit;
goto s;
}
else if (choice==4)
{
cout << "\nTHANK YOU!";
}
else if (pin!=0)
{
cout << "\nInvalid pin!" << "\nPlease try again.";
}
return 0;
getch();
}
The open-ended lab task qualifies as a Complex Engineering Problem as it meets the criterion 1
and 3:
Extent of stakeholder
Involve diverse groups of stakeholders with widely
7 involvement and level of ☐
varying needs.
conflicting requirements
S
Attribute Complex Problems = 1 & (2 | 3 | 4 | 5 | 6 | 7 | 8 | 9)
#
Extent of stakeholder
Involve diverse groups of stakeholders with widely
7 involvement and level of ☐
varying needs.
conflicting requirements
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS
10.1 PROCEDURES
The array indices start with 0. Meaning x [0] is the first element stored at index 0. If size of
array is n last element would be store in (n-1).
We learn from this lab that Arrays are used to store multiple values in a single
variable, instead of declaring separate variables for each value.
10.4 EXERCISE QUESTIONS
10.4.1 Write a program that declares an array of 10 elements of type int. Initialize
each element of array with cube of the index variable. Then display all values
in the array using loop.
Code Output
#include<iostream>
using namespace std; 0
int main( ) 1
{ 8
int num[10]; 27
num[0]=0; 64
num[1]=1; 125
num[2]=8; 216
num[3]=27; 343
num[4]=64; 512
num[5]=125; 729
num[6]=216;
num[7]=343; Process returned 0 (0x0)
num[8]=512; execution time : 0.023 s
num[9]=729; Press any key to
int i; continue.
for(i=0;i<10;i++)
{
cout<<num[i]<<endl;
}
return 0;
}
10.4.2 Write a program that declares an integer array of size 10 and reads values
from user in it. Then find out how many elements are even and how many are
odd.
Code Output
#include<iostream> Enter size of the array
int i,size,odd=0,even=0; 4
cin>>arr[i];
if(arr[i]%2==0)
even++;
Else
odd++;
}
}
cout<<"Total even numbers of an array :"<<even<<endl;
cout<<"Total odd numbers of an array : "<<odd<<endl;
10.4.3 Write a program that declares an integer array of size 10 and reads values
from user in it. Then replace its negative elements with same positive values.
Code Output
Int main () 2
{ -3
{ 5
cin>>arr[i]; 6
} 7
{
countn++;
}
else if(arr[i]==0)
{
countz++;
}
Else
{
countp++;
}
cout<<"Positive Numbers = "<<countp<<"\n";
cout<<"Negative Numbers = "<<countn<<"\n";
cout<<"Zero = "<<countz<<"\n";
return 0;
}
10.4.4 Write a program that takes an integer array of size 10 from user then prints
the smallest and largest element of the array.
Code Output
10.4.5 Write a program that takes an integer array of size 10 from user then shifts
it’s all elements to the left and its first element on the last location. (Circular
Shift Right)
Code Output
Int main() 2
{ 3
Int n,I; 4
Int arr[size]; 5
For(I=0;I<size;I++) 7
{ 8
Cout<<arr[I]; 9
} 10
Enter number of time to
Cout<<”enter number of time to left rotate”<<endl;
left rotate=2
Array after
Cin>>n;
rotation=array(arr)
N=n%size;
Cout<<”array before rotation”<<endl;
Cout<<array(arr);
For(I=1;I<=n;I++)
{
Cout<<Rotate by one(arr);
}
Cout<<”array aftar rotation”<<endl;
Cout<<array(arr);
return0;
}
GENERALIZED LAB RUBRICS
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS
11.1 PROCEDURES
11.4.1 Write a program that uses a two-dimensional array to store the highest and
lowest temperatures for each month of the year. The program should output
the average high, average low, and the highest and lowest temperatures for the
year.
Code Output
Please enter temperature
#include<iostream>
for month=
Using namespace std; 23
Total temperature
Int main()
was=120
{ Highest temperature was
double temperature; 23
Lowest temperature
double total=0;
was=
double avg=0; 20
double maxtemp=0;
double mintemp=0;
double a=0;
for (int i=0;i<12;i++)
{
cout<<"please enter temperature for
month"<<i+1<<":"<<endl;
cin>>temperature;
Total=temperature;
}
avg=total/12;
Maxtemp=temperature;
Mintemp=temperature;
for (int i=1;i<=12;i++)
{
a=temperature;
if(a<mintemp)
mintemp=a;
If(a>maxtemp)
maxtemp=a;
}
cout<<"totaltemperature
was"<<fixed<<showpoint<<total<<endl;
cout<<”averagetemperature
Was”<<fixed<<showpoint<<average<<endl;
cout<<"Heighesttemperature
was"<<fixed<<showpoint<<maxtemp<<endl;
cout<<"lowesttemperature
was"<<fixed<<showpoint<<mintemp<<endl;
Return 0;
}
11.4.2 A program that reads two 2-d arrays as 3*3 matrices then calculates and
stores their product into another 2-d array of size 3*3.
Code Output
{
cin>>matrix1[i][j];
}
}
cout<<"\n 1 Matrix You Entered\n;
for( i=0;i<3;i++){
for( j=0;j<3;j++)
{
cin>>matrix2[i][j];
}
}
cout<<"\n 2 Matrix You Entered\n";
for( i=0;i<3;i++){
for( j=0;j<3;j++)
{
cout<<matrix2[i][j];
}
cout<<endl;
}
cout<<"Multiplying two matrices...\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout<<matrix3[i][j]<<"";
}
cout<<"\n";
}
Return 0;
}
11.4.3 A program that reads a 2-d array as 3*3 matrix then checks whether the
matrix is symmetric or not. A matrix is symmetric if mat [ i ] [ j ] = mat [ j ] [ i
] for all i and j except when i = j.
Code Output
{ 1
int matrix[3][3] 3
int I,j; 4
int flag=0; 6
for(i=0;i<3;i++) 7
{ 6
for(j=0;j<3;j++) 5
{ matrix is not symmetric
cout<<"enter matrix element"<<endl;
cin>>matrix[i][j];}}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{if(matrix[i][j]!=matrix[j][i])}
flag=1}}}
if (flag==1)
{
cout<<"matrix is not symmetric!"<<endl;
}
Else
{
cout<<"matrix is symmetric"<<endl;
}
Return 0;}
11.4.4 A program that reads a 2-d array as 3*3 matrix then calculates and prints its
Determinant.
Code Output
Calculate the
#include<iostream> determinent of 3*3
matrix=
Input element in first
Using namespace std;
matrix=
Void main() 1
{ 2
int arr1[10][10],i,j,n; 3
int det=0; 4
for(i=0;i<3;i++) 2
{ 3
for(j=0;j<3;j++) 4
Determinent of matrix is
{
5
cout<<"element - [%d],[%d] : "<<i<<j<<endl;
cin>>arr1[i][j];
}
}
cout<<"The matrix is "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3 ;j++)
cout<<"arr1[i][j]"<<endl;
}
for(i=0;i<3;i++)
det = det + (arr1[0][i]*(arr1[1][(i+1)%3]*arr1[2][(i+2)%3] -
arr1[1][(i+2)%3]*arr1[2][(i+1)%3]));
cout<<"The Determinant of the matrix is:"<<det<<endl;
Return 0;
}
GENERALIZED LAB RUBRICS
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS
12.1 PROCEDURES
The find () method will then check if the given string lies in our string. It will return
the size of the sub-string including the '\0' terminating character (as size_ t).
But if the string does not lie in our original string, it will return 0.
In this lab I have come to know about the string concept in C++ language. And how to compile
the code.
12.4 EXERCISE QUESTIONS
12.4.1 Write a program that takes a string as input from user then converts its
capital letters into small letters and small letters into capital letters.
Code Output
#include <iostream>
using namespace std;
int lower_string(string str)
{
for(int i=0;str[i]!='\0';i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
}
cout<<"\n The string in lower case: "<< str;
}
int upper_string(string str)
{ Enter the string Burhan
for(int i=0;str[i]!='\0';i++)
The string in lower case:
{ burhan
if (str[i] >= 'a' && str[i] <= 'z') The string in upper case:
str[i] = str[i] - 32; BURHAN
}
cout<<"\n The string in upper case: "<< str;
}
int main()
{ string str;
cout<<"Enter the string ";
getline(cin,str);
lower_string(str);
upper_string(str);
return 0;
}
12.4.2 Write a program that takes a string as input from user then prints whether it
is palindrome or not. (Palindrome is a type of word play in which a word,
phrase, or sentence reads the same backward or forward).
Code Output
#include <iostream> Enter the string: 313
#include <string.h> 313 is a palindrome
using namespace std;
int main() Enter the string: 522
{ 522 is not a palindrome
char str1[20], str2[20];
int i, j, len = 0, flag = 0;
cout << "Enter the string : "; Process returned 0 (0x0)
gets(str1); execution time: 3.292 s
len = strlen(str1) - 1; Press any key to continue.
for (i = len, j = 0; i >= 0 ; i--, j++)
str2[j] = str1[i];
if (strcmp(str1, str2))
flag = 1;
if (flag == 1)
cout << str1 << " is not a palindrome";
else
cout << str1 << " is a palindrome";
return 0;
}
12.4.3 Write a program that takes a string as input from user then capitalize its each
word.
Code Output
#include<iostream.h> Enter a string: burhan
#include<conio.h> New string is: BURHAN
#include<stdio.h>
#include<ctype.h>
void main() {
clrscr();
int i;
char a[30];
cout<<“Enter a string:”;
gets(a);
for (i=0;a[i]!=’’;++i) {
if(i==0) {
if(islower(a[i]))
a[i]=toupper(a[i]);
} else {
if(a[i]!=’ ‘) {
if(isupper(a[i]))
a[i]=tolower(a[i]);
} else {
i++;
if(islower(a[i]))
a[i]=toupper(a[i]);
12.4.4 Write a program that takes a string as input then replaces all words of “is” by
“was”.
Code Output
#include<iostream>
using namespace std;
struct Replace
{
string str;
int j,i,k;
string key;
string repl;
enter the string:
}; Bunny
Original text: Bunny
int main() the edit text
{
Replace r1; Process returned 0 (0x0)
cout<< "enter the string:"<<endl; execution time : 4.216 s
getline(cin, r1.str);
Press any key to continue.
cout << "Original text: " << r1.str<<endl;
for (r1.j = 0; r1.j < r1.str.size(); r1.j++)
{
r1.key = r1.str.substr(r1.j, 3),r1.repl;
if (r1.key == "is")
{
r1.repl = "was";
for (r1.k = 0; r1.k < 3; r1.k++)
{
r1.str[r1.j+r1.k] = r1.repl[r1.k];
}
}
}
cout<<"the edit text"<<endl;
cout<<r1.repl;
return 0;
}
12.4.5 Write a program that takes two strings as input from user then prints whether
both strings are equal or not without using built-in function of strcmp ().
Code Output
#include<string.h>
int main()
{ Strings are equal
Value returned by strcmp()
char leftStr[] = "g f g"; is: 0
char rightStr[] = "g f g";
Process returned 0 (0x0)
int res = strcmp(leftStr, rightStr); execution time : 0.056 s
Press any key to continue.
if (res==0)
printf("Strings are equal");
else
printf("Strings are unequal");
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS
13.1 PROCEDURES
Structures group members (data and functions) to create new data types. Structures
encapsulate data members (usually different data types), much like functions
encapsulate
program statements. Unions are like structures, but data members overlay (share)
memory, and unions may access members as different types. We use structures and
and graphics.
A structure is a user-defined data type available in C that allows to combining data items
of
different kinds. Structures are used to represent a record. A union is a special data type
available in C that allows storing different data types in the same memory location.
13.3 LEARNING OUTCOMES
In this lab we have learned and become familiar with the structure and union.
13.4.1 Write a function called future Date. The function is to use two parameters.
The first parameter is a structure containing today’s date. The second
parameter is an integer showing the number of days after today. The function
returns a structure showing the next date, which may be in a future year.
Code Output
#include <iostream>
using namespace std; Enter Date: 28
Enter Month: 08
struct day Enter Year: 2020
{
int date; Your required date will
int month; come after days: 27
int year;
Date: 24
day() Month: 9
{ Year: 2020
do{
cout<<"Enter Date: "; Process returned 0 (0x0)
cin>>date; execution time : 28.078
}while(date>31); s
Press any key to
do{ continue.
cout<<"Enter Month: ";
cin>>month;
}while(month>12);
void show();
};
while(b!=future)
{
if(a.year % 4 ==0 && a.month==2)
{
a.month++;
a.date=1;
b++;
}
return a;
}
void day::show()
{
cout<<endl<<"Date: "<<date<<endl;
cout<<"Month: "<<month<<endl;
cout<<"Year: "<<year<<endl;
}
int main()
{
day var;
int next;
cout<<endl<<"Your required date will come after days:
";
cin>>next;
futureDate(var, next);
var.show();
return 0;
}
13.4.2 Write a function called increment that accepts a date structure with three
fields each field is an integer, one for the month, one for the day in the month,
and one for the year. The function increments the date by one day and returns
the new date. If the date is the last day in the month, the month field must also
be changed. If the month is December, the value of the year field must also be
changed when the day is 31. A year is a leap year if it is evenly divisible by 4
but not by 100 or it is divisible by 400.
Code Output
#include <iostream> Enter Date: 27
using namespace std; Enter Month: 08
Enter Year: 2020
struct day
{ After Increment:
int date; Date: 28
int month; Month: 8
int year; Year: 2020
day() Process returned 0 (0x0)
{ execution time : 10.657
do{ s
cout<<"Enter Date: "; Press any key to
cin>>date; continue.
}while(date>31);
do{
cout<<"Enter Month: ";
cin>>month;
}while(month>12);
void show();
};
else
{
a.date++;
}
return a;
}
void day::show()
{
cout<<endl<<"After Increment: "<<endl;
cout<<"Date: "<<date<<endl;
cout<<"Month: "<<month<<endl;
cout<<"Year: "<<year<<endl;
}
int main()
{
day var;
increment(var);
var.show();
return 0;
}
13.4.3 Create a user defined structure type Rectangle with these integer members,
length and width. The program defines a rectangle r1 and initializes the
members according to user input. Then write a function recto square which
will take a rectangle type variable and calculate the area of that rectangle. It
will then return a structure square that will have an integer member a length
Code Output
#include<iostream> Enter length: 12
using namespace std; Enter width: 3
Area is 36
struct rectangle Process returned 0 (0x0)
{ execution time : 3.094 s
int l,w; Press any key to
}r1; continue.
int rectosquare(int l,int w)
{
int area =l*w;
cout<<"Area is "<<area;
}
int main()
{
int l,w;
cout<<"Enter length: ";
cin>>l;
cout<<"Enter width: ";
cin>>w;
rectosquare(l,w);
return 0;
}
13.4.4 Write a program that declares a struct to store the data of a Volunteer at an
NGO(Name, Age, City, Blood Group, Total Hours Volunteered.). Suppose
there are 7 volunteers, whose profile you need to maintain. Your program
must contain a function to input data and a function to output data
Code Output
#include<iostream> Enter city:
using namespace std; Enter blood group:
Enter hours:
Enter name:
struct volunteer Enter age:
{ Enter city:
string name; Enter blood group:
int age; Enter hours:
string city; Enter name:
string blood_grp; Enter age:
int hrs; Enter city:
}v[7]; Enter blood group:
Enter hours:
void getdata() Displaying information
{ Name: Burhan
cout<<endl; Age: 21
cout<<"Enter the information: "<<endl; City: lahore
for(int i=0;i<7;i++) Blood Group: B+
{ Hours: 16
cout<<"Enter name: "; Name: Waleed
cin>>v[i].name; Age: 21
cout<<endl; City: Lahore
cout<<"Enter age: "; Blood Group: A-
cin>>v[i].age; Hours: 14
cout<<endl; Name: Taimoor
cout<<"Enter city: "; Age: 0
cin>>v[i].city; City:
cout<<endl; Blood Group:
cout<<"Enter blood group: "; Hours: 0
cin>>v[i].blood_grp; Name:
cout<<endl; Age: 0
cout<<"Enter hours: "; City:
cin>>v[i].hrs; Blood Group:
cout<<endl; Hours: 0
} Name:
} Age: 0
City:
void display() Blood Group:
{ Hours: 0
cout<<"Displaying information"; Name:
cout<<endl; Age: 0
for(int i=0;i<7;i++) City:
{ Blood Group:
cout<<"Name: "<<v[i].name<<endl; Hours: 0
cout<<"Age: "<<v[i].age<<endl; Name:
cout<<"City: "<<v[i].city<<endl; Age: 0
cout<<"Blood Group: "<<v[i].blood_grp<<endl; City:
cout<<"Hours: "<<v[i].hrs<<endl; Blood Group:
} Hours: 0
}
int main() Process returned 0 (0x0)
{ execution time : 45.525
getdata(); s
display(); Press any key to
return 0; continue.
}
GENERALIZED LAB RUBRICS
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS
14.1 PROCEDURES
In this lab I have generated a C++ code for the banking scenario and I have come to know
about how to generate this type of code for the safety of public and security of wealth of
public.
In this lab I have generated a C++ code for the banking scenario and I have come to know
about how to generate this type of code for the safety of public and security of wealth of
public.
14.4 EXERCISE QUESTIONS
14.4.1 You are required to maintain a file record that will mimic a banking system.
Write a program that will first create a file to maintain customers record.
Your program will use various user-defined functions to add a new record,
update record, delete record and print a customer’s record given his ID card
number or bank account number. Add all fields that you think are necessary
to keep a customer’s record.
Code Output
#include<iostream> ***Account Information
System***
#include<fstream> Select one option below
using std::ios;
char firstName[10];
void account_query::read_data()
{
cout<<"\nEnter Account Number: ";
cin>>account_number;
cout<<"Enter First Name: ";
cin>>firstName;
cout<<"Enter Last Name: ";
cin>>lastName;
cout<<"Enter Balance: ";
cin>>total_Balance;
cout<<endl;
}
void account_query::show_data()
{
cout<<"Account Number: "<<account_number<<endl;
cout<<"First Name: "<<firstName<<endl;
cout<<"Last Name: "<<lastName<<endl;
cout<<"Current Balance: Rs. "<<total_Balance<<endl;
cout<<"-------------------------------"<<endl;
}
void account_query::write_rec()
{
ofstream outfile;
outfile.open("record.bank", ios::binary|ios::app);
read_data();
outfile.write(reinterpret_cast<char *>(this), sizeof(*this));
outfile.close();
}
void account_query::read_rec()
{
ifstream infile;
infile.open("record.bank", ios::binary);
if(!infile)
{
cout<<"Error in Opening! File Not Found!!"<<endl;
return;
}
cout<<"\n****Data from file****"<<endl;
while(!infile.eof())
{
if(infile.read(reinterpret_cast<char*>(this),
sizeof(*this))>0)
{
show_data();
}
}
infile.close();
}
void account_query::search_rec()
{
int n;
ifstream infile;
infile.open("record.bank", ios::binary);
if(!infile)
{
cout<<"\nError in opening! File Not Found!!"<<endl;
return;
}
infile.seekg(0,ios::end);
int count = infile.tellg()/sizeof(*this);
cout<<"\n There are "<<count<<" record in the file";
cout<<"\n Enter Record Number to Search: ";
cin>>n;
infile.seekg((n-1)*sizeof(*this));
infile.read(reinterpret_cast<char*>(this), sizeof(*this));
show_data();
}
void account_query::edit_rec()
{
int n;
fstream iofile;
iofile.open("record.bank", ios::in|ios::binary);
if(!iofile)
{
cout<<"\nError in opening! File Not Found!!"<<endl;
return;
}
iofile.seekg(0, ios::end);
int count = iofile.tellg()/sizeof(*this);
cout<<"\n There are "<<count<<" record in the file";
cout<<"\n Enter Record Number to edit: ";
cin>>n;
iofile.seekg((n-1)*sizeof(*this));
iofile.read(reinterpret_cast<char*>(this), sizeof(*this));
cout<<"Record "<<n<<" has following data"<<endl;
show_data();
iofile.close();
iofile.open("record.bank", ios::out|ios::in|ios::binary);
iofile.seekp((n-1)*sizeof(*this));
cout<<"\nEnter data to Modify "<<endl;
read_data();
iofile.write(reinterpret_cast<char*>(this), sizeof(*this));
}
void account_query::delete_rec()
{
int n;
ifstream infile;
infile.open("record.bank", ios::binary);
if(!infile)
{
cout<<"\nError in opening! File Not Found!!"<<endl;
return;
}
infile.seekg(0,ios::end);
int count = infile.tellg()/sizeof(*this);
cout<<"\n There are "<<count<<" record in the file";
cout<<"\n Enter Record Number to Delete: ";
cin>>n;
fstream tmpfile;
tmpfile.open("tmpfile.bank", ios::out|ios::binary);
infile.seekg(0);
for(int i=0; i<count; i++)
{
infile.read(reinterpret_cast<char*>(this),sizeof(*this));
if(i==(n-1))
continue;
tmpfile.write(reinterpret_cast<char*>(this),
sizeof(*this));
}
infile.close();
tmpfile.close();
remove("record.bank");
rename("tmpfile.bank", "record.bank");
}
int main()
{
account_query A;
int choice;
cout<<"***Acount Information System***"<<endl;
while(true)
{
cout<<"Select one option below ";
cout<<"\n\t1-->Add record to file";
cout<<"\n\t2-->Show record from file";
cout<<"\n\t3-->Search Record from file";
cout<<"\n\t4-->Update Record";
cout<<"\n\t5-->Delete Record";
cout<<"\n\t6-->Quit";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
A.write_rec();
break;
case 2:
A.read_rec();
break;
case 3:
A.search_rec();
break;
case 4:
A.edit_rec();
break;
case 5:
A.delete_rec();
break;
case 6:
exit(0);
break;
default:
cout<<"\nEnter corret choice";
exit(0);
}
}
system("pause");
return 0;
}
The open-ended lab task qualifies as a Complex Engineering Problem as it meets the criterion 1 and 3:
S
Attribute Complex Problems = 1 & (2 | 3 | 4 | 5 | 6 | 7 | 8 | 9)
#
Extent of stakeholder
Involve diverse groups of stakeholders with widely
7 involvement and level of ☐
varying needs.
conflicting requirements
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
GENERALIZED LAB RUBRICS