Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

ASSIGNMENT 2

1.
#include <iostream>
using namespace std;

int main() {
int number1 = 88, number2 = 22;

// Create a pointer pointing to number1


int * pNumber1 = &number1; // Explicit referencing
*pNumber1 = 99; // Explicit dereferencing
cout << *pNumber1 << endl; // 99
cout << &number1 << endl; // 0x22ff18
cout << pNumber1 << endl; // 0x22ff18 (content of the pointer variable -
same as above)
cout << &pNumber1 << endl; // 0x22ff10 (address of the pointer variable)
pNumber1 = &number2; // Pointer can be reassigned to store another
address

// Create a reference (alias) to number1


int & refNumber1 = number1; // Implicit referencing (NOT &number1)
refNumber1 = 11; // Implicit dereferencing (NOT *refNumber1)
cout << refNumber1 << endl; // 11
cout << &number1 << endl; // 0x22ff18
cout << &refNumber1 << endl; // 0x22ff18
//refNumber1 = &number2; // Error! Reference cannot be re-assigned
// error: invalid conversion from 'int*' to 'int'
refNumber1 = number2; // refNumber1 is still an alias to number1.
// Assign value of number2 (22) to refNumber1 (and
number1).
number2++;
cout << refNumber1 << endl; // 22
cout << number1 << endl; // 22
cout << number2 << endl; // 23
}

To get the value pointed to by a pointer, you need to use the


dereferencingoperator*(e.g.,ifpNumber is
a int pointer, *pNumber returns the value pointed to by pNumber. It is
called dereferencing or indirection). To assign an address of a variable
into a pointer, you need to use the address-of
operator & (e.g., pNumber =&number).
On the other hand, referencing and dereferencing are done on the
references implicitly. For example, if refNumber is a reference (alias) to
another int variable, refNumber returns the value of the variable. No
explicit dereferencing operator * should be used. Furthermore, to
assign an address of a variable to a reference variable, no address-of
operator & is needed.

2.

#include<iostream>
void main()
{
int sum(int,int); //function with 2 argument
int sum(int,int,int); //function with 3 argument
int sum(int,int,int,int); //function with 4 argument
int sum(int[],int); //function with n argument
clrscr();
int a,b,c,d,result;
cout<<"\n\nfor 2 argument\n";
cout<<"Enter 2 Integers\n";
cin>>a>>b;
result=sum(a,b);
cout<<"Addition =" << result;
cout << "\n\nfor 3 argument\n";
cout<<"Enter 3 Integers\n";
cin>> a >> b >> c;
result=sum(a,b,c);
cout<<"Addition ="<< result;
cout<< "\n\nfor 4 argument\n";
cout<<"Enter 4 Integers\n";
cin>>a >> b >> c >> d;
result=sum(a,b,c,d);
cout<<"Addition =" << result;
cout<<"\n\nHow many Argument You want to enter:-";
int no;
cin>>no;
int num[50];
cout<<"Enter "<< no <<" Integers\n";
for(int i=0;i < no ;i++)
cin>>num[i];
result=sum(num,no);
cout<<"Addition =" << result;
getch();
}
//function with 2 argument
int sum(int a,int b)
{
return(a+b);
}
//function with 3 argument
int sum(int a,int b,int c)
{
return(a+b+c);
}
//function with 4 argument
int sum(int a,int b,int c,int d)
{
return(a+b+c+d);
}
//function with n argument
int sum(int a[],int n)
{
int sum=0;
for(int i=0;i < n;i++)
{
sum=sum+a[i];
}
return(sum);
}

3. #include<iostream >

using namespace std;

int &f1(int y, int x=0)


{
int z= x*y;

}
int *f2(int a,int b=1)
{
int t= a*b;
}
int main()
{
const int r = 10;
const int&a = r;
const char*pc1="john";
const char*pc2="john";
const char*pc3="john";
const char*pc4="dukang";
pc1[2]='t';//Error
pc2[2]='t';//Error
pc3[2]='t';//Error
pc4[2]='t';//Error
cout<<f1(3);
cout<<f2(2,3);
return 0;
}

4. 1. Output
Error, in the header file <iostream> should not be added dot h. the
second error is the statement using namespace std; should be included. If
those errors corrected the output should be:
1000
2000
500
1000
2. Output
200
20
100
300
5. #include <iostream>
using namespace std;
int min(int intArray[]);
float min(float floatArray[]);
double min(double doubleArray[]);
long double min(long double longdoubleArray[]);
int min(int intArray[])
{
int min = 999;
for (int i = 0; i < 5; i++)
{
if (intArray[i] < min)
{
min = intArray[i];
}
}
return min;
}
float min(float floatArray[])
{
float min = 9999.9;
for (int i = 0; i < 5; i++)
{
if (floatArray[i] < min)
{
min = floatArray[i];
}
}
return min;
}
double min(double doubleArray[])
{
double min = 9999.9;
for (int i = 0; i < 5; i++)
{
if (doubleArray[i] < min)
{
min = doubleArray[i];
}
}
return min;
}
long double min(long double longdoubleArray[])
{
long double min = 9999.9999;
for (int i = 0; i < 5; i++)
{
if (longdoubleArray[i] < min)
{
min = longdoubleArray[i];
}
}
return min;
}

int main()
{
int intArray[5] = {5,3,2,1,4};
float floatArray[5] = { 12.2,123.6,456.3,236.2,23.1 };
double doubleArray[5] = { 465.25552,632.2,532.3,47.69,63.2 };
long double longdoubleArray[5] = {
3523.1,6562.3,6325.7,335.3366,6587.333};
cout << "smallest value in the intArray is " << (min(intArray)) << endl;
cout << "smallest value in the floatArray is " << (min(floatArray)) <<
endl;
cout << "smallest value in the doubleArray is " << (min(doubleArray))
<< endl;
cout << "smallest value in the longdoubleArray is " <<
(min(longdoubleArray)) << endl;

return 0;
}

You might also like