Roll No 150252 Lab-1: Exercise 1
Roll No 150252 Lab-1: Exercise 1
Lab-1
Exercise 1
Write a program in C++ which carries out following tasks
- Create two integer variables (A and B) and assign values to them
- Print values of both variables on screen
- Print address of both variables
- Create an integer pointer and point it to first integer variable (A)
- Print first variable (A) value using pointer
- Point/Direct pointer variable to second variable
- Print the value where pointer points to
- Print address and value of pointer
- Program should not exit unless user decides to
- Create second pointer and point pointer1 and pointer 2 to first variable
- Create Change value of value of first pointer and print value, change value using second
pointer and print value of variable?
Code:
#include <iostream>
int main() {
int n=0;
while(n==0)
int A=5,B=10;
int* p1=&A;
int *p2=&A;
p1=p2;
*p1=15;
*p2=3;
cin>>n;
if(n!=0)
break;}
return 0;}
Exercise 2
Write a program which reads two integer type values from user and displays the values.
Create a function to swap the values of both variables using pointers and display to user.
Program should repeat itself until wants to exit.
#include<iostream>
{int c;
c=*num1;
*num1=*num2;
*num2=c;}
int main() {
char choice='y';
while(choice!='a')
{int a,b;
cout<<"enter 1st values";
cin>>a;
cin>>b;
swap(&a,&b);
cin>>choice;}
return 0;}
Exercise 3
Write C++ program which reads a number from user and makes use of user defined
function which accepts an integer pointer. This function should increment or decrement
sent value by one depending on user choice. User should pass value by reference to the
function and the option as an argument (1 to increment and 2 to decrement value).
Program should repeat itself without user choice.
Custom function signature is:
Void IncrementDecrement ( int * number, int operation );
Code: #include<iostream>
int main()
{ int p;
int num;
cin>>num;
cin>>p;
incdec(&num,p);
cout<<num;
return 0;}
{if (p==1)
*n=*n+1;
else if (p==2)
{*n=*n-1;}
else
Exercise 4
Write C++ program which reads a number from user and makes use of user defined
function to calculate factorial. Please pass argument by reference (using pointer) and
display result (factorial) in main function.
Code:
#include<iostream>
int main()
{int b;
cout<<"enter a number"<<endl;
cin>>b;
factorial(&b);
cout<<b;
return 0;}
{int fac;
fac=1;
{ fac =fac* i; }
*a=fac;}
Exercise 5
Create an array of type integer having 10 elements in it. Assign values to all elements using
initializer list.
Print values of all array elements using for loop.
Code:
#include<iostream>
int main()
{ int i;
int a[10]={1,2,3,4,5,6,7,8,9,0};
return 0;}
Exercise 6
Write a program in C++ which makes use of double type array to read subject marks of
student and display entered marks, their total and average on screen.
- Create an array of type double with 5 elements; initialize all elements with value 0.
- Make use of for loop to read values from user and fill array elements
- Display all elements data on screen using loop
Code:
#include<iostream>
int main()
{ int stu[5];
stu[5]={0};
{ cin>>stu[i];
total=0;
for(int i=0;i<5;i++)
{ total=total+stu[i]; }
return 0;}
Exercise 7
Create an integer type array A with 10 elements using initializer list. Create another array
B with same size having all elements initialized to 0 by default. Copy all elements from
array A to array B and print both arrays for loop.
Code:
#include<iostream>
int main()
{int a[10]={0,1,2,3,4,5,6,7,8,9,};
int b[10]={0};
for(int i=0;i<10;i++)
{b[i]=a[i];}
for(int i=0;i<10;i++)
{cout<<"A="<<a[i]<<endl;}
for(int i=0;i<10;i++)
{cout<<"B = "<<b[i]<<endl;}
return 0;}
Exercise 8
Write a program which reads 10 decimal numbers in an array and sums all array elements
and displays total on screen
- Create an array of type double with 10 elements
- Using for loop sum all elements of array and use first elements for result storage
- Display results (sum of all elements)
Code:
#include<iostream>
double total=0;
for(int i=0;i<10;i++)
{total=total+a[i];}
cout<<"total= "<<total<<endl;
return 0;}
Exercise 10
Create an integer array with 10 elements and read values from user at run time. Ask user
to enter a value to search in array. Find all the occurrences of search value and return
index numbers of occurrences (if found).
Code:
#include<iostream>
int main()
{int a[10]={0};
cout<<"enter numbers"<<endl;
for(int i=0;i<10;i++)
{cin>>a[i];}
int num,flag=0;
int i;
cout<<"enter a number"<<endl;
cin>>num;
for( i=0;i<10;i++)
{if(a[i]==num){
flag=1;
a[i]=2;}}
if (flag==1){
for(i=0;i<10;i++){
if (a[i]==2)
cout<<"index"<<i<<endl;}}
if (flag==1)
return 0;}
Exercise 11
Write a program in C++ which does following:
- Make use of strlen function to display size of a string and use strcat function to
concatenate two strings
- Use strcpy function to copy one string to another
#include<iostream>
int main()
{char a[6];
char *a1="rimsha";
char *a2="kanwal";
string s1="rimsha";
strcpy(a,a2);
s1+=s2;
cout<<"Size of string="<<s1<<s1.size()<<endl;
strcat(a1,a2);
cout<<a1<<" "<<strlen(a1)<<endl;
return 0;}