You are on page 1of 15

Assignment:

Programming Fundamentals

Department:
Software Engineering

Submitted to:

Dr.Madiha Liaqat

Submitted by:

Name Registration No.


Muhammad Hassan 20-SE-64

University of Engineeering and Technology, Taxila


Q1. a. What is passing arguments by value?
Explain with the help of example.
Solution:
When you pass an argument by value, you pass a
copy of the value in memory. The function operates
on the copy. This means that when a function
changes the value of an argument passed by value,
the effect is local to that function; the copy changes
but the original value in memory is not affected.
Example:
#include<iostream>
using namespace std;
 
//function to swap the Numbers:
void swap(int i , int j)
{
int temp=j;
j=i;
i=temp;
}
 
main()
{
int a,b; //variable to store the Numbers:
cout<<"Enter The value of A : ";
cin>>a;
cout<<"\nEnter The value of B : ";
cin>>b;
 
cout<<"\n Before swaping the value is : A =
"<<a<<" B = "<<b<<endl;
 
swap(a,b); //function call-by-value.
cout<<" After swaping the value is A = :"<<a<<"
B = "<<b<<endl;
 
return 0;
}
OUTPUT:
Enter The value ofA: 25
Enter The value of B 52
Before swaping the value is aA-25 B52
After swaping thetalue is A is 25 B 52
In the Above example, did you Notice that the value
of A and B is same after and before swapping
Because the value of the actual parameter is copied
to the fomal parameter for the purpose of executing
the function's code. Since it is working on a copy of
the actual parameter, the function's exxecution
cannot aftect the value of the actual parameter
Owmed by the caller.
b. Differentiate structure and enumeration.
Both are said to be user defined data type, so
for what purpose we use each of the two.
Enumeration:
Enumeration is a collection of named constants.
Similar to integral numbers. But represented with
names.
Where as:
Structure:
Structures is compound which can store multiple
related data (fields) with different data types to
represent an object.
What purpose Use:
A structure is a user defined data type in C/C++. A
structure creates a data type that can be used to
group items of possibly different types into a single
type and Enumeration (or enum) is a user defined
data type in C. It is mainly used to assign names to
integral, the names make a program easy to read and
maintain.
c. Explain namespace with the help of
example.
Namespace is a feature added in C++ and not
present in C. A namespace is a declarative
region that provides a scope to the identifiers
(names of the types, function, variables etc)
inside it. Multiple namespace blocks with the
same name are allowed. All declarations within
those blocks are declared in the named scope.
Example:
1)C:\Program Files\Internet Explorer is the
namespace that describes where Internet
Explorer files on a Windows computer.
2) You might be writing some code that has a
function called xyz() and there is another library
available which is also having same function
xyz(). ... Using namespace, you can define the
context in which names are defined. In essence,
a namespace defines a scope.
Question 2:
What is the output when the following
code fragment is executed? Explain.
#include<conio.h>
#include<iostream>
using namespace std;
int main()
{ int i = 5;
while (i > 0)
{ --i;
cout << i << endl;
}
return 0;}
OUTPUT:
B)
#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
for (int i = 4; i >= 0; --i)
{
cout << i << endl;
}
}
Explination/A Program:
In this program the variable “i” is initialized 5. The
condition in while loop is i>0. First of all, the condition is
evaluated. If the condition is true, the control enter the
body and executed all statements in the body. While the
loop terminates only when the condition is false. The third
part contain --I. It indicates that the value of “I “will be
decremented by 1 after each execution of loop.
Question 3:

Write a C++ program that converts the number


entered by user into characters. For Example, if a user
has entered a number 56, then your program should
print “five , six”, if he has entered 895, then your
program should display “eight , nine , five”.

Code:
#include <iostream>
using namespace std;
//main method
int main()
{
//Declaring the variable and initializing sum
long int n,sum=0,r;
cout<<"Enter the Number= ";
cin>>n;
// To calculate the reverse of the number
while(n>0)
{
// The remainder will give the last digit of the
number
r=n%10;
sum=sum*10+r;
n=n/10;
}
n=sum;
//test expression with while loop
while(n>0)
{
// Extract the first digit of the reversed number
r=n%10;
// Match it with switch case
switch(r)
{
case 1: // expression is equal to constant one
cout<<"one "; // code to be executed
break; // break statement end
case 2: // expression is equal to constant two
cout<<"two ";
break; //break statement end
case 3: // code to be executed
cout<<"three "; // expression is equal to
constant three
break; //break statement end
case 4: // code to be executed
cout<<"four "; // expression is equal to
constant four
break; //break statement end
case 5: // expression is equal to constant
five
cout<<"five "; // code to be executed
break; //break statement end
case 6: // expression is equal to constant
six
cout<<"six "; // code to be executed
break; //break statement end
case 7: // expression is equal to constant
seven
cout<<"seven "; // code to be executed
break; //break statement end
case 8: // expression is equal to constant
eight
cout<<"eight "; // code to be executed
break; //break statement end
case 9: // expression is equal to constant
nine
cout<<"nine "; // code to be executed
break;//break statement end
case 0: // expression is equal to constant zero
cout<<"zero "; // code to be executed
break; //break statement end
default:
// Created by (20-SE-64)
cout<<"tttt ";
break;//break statement end
}
// Divide the number by 10 to get the next
number
n=n/10;
}
}

You might also like