0% found this document useful (0 votes)
32 views9 pages

Roll No 150252 Lab-1: Exercise 1

The document contains 11 programming exercises involving the use of arrays, pointers, strings and other basic C++ concepts. The exercises cover tasks such as: 1. Declaring and initializing integer variables and printing their values and addresses. Using pointers to access variable values. 2. Reading two integer values from the user, swapping them using a function that accepts pointer arguments, and printing the swapped values. 3. Defining a function that increments or decrements an integer value passed by reference using a pointer. The function is called with different operation arguments. 4. Defining a factorial function that calculates the factorial of an integer passed by reference using a pointer. 5. Initializing and printing the

Uploaded by

Aleena Kanwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views9 pages

Roll No 150252 Lab-1: Exercise 1

The document contains 11 programming exercises involving the use of arrays, pointers, strings and other basic C++ concepts. The exercises cover tasks such as: 1. Declaring and initializing integer variables and printing their values and addresses. Using pointers to access variable values. 2. Reading two integer values from the user, swapping them using a function that accepts pointer arguments, and printing the swapped values. 3. Defining a function that increments or decrements an integer value passed by reference using a pointer. The function is called with different operation arguments. 4. Defining a factorial function that calculates the factorial of an integer passed by reference using a pointer. 5. Initializing and printing the

Uploaded by

Aleena Kanwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Roll no 150252

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>

using namespace std;

int main() {

int n=0;

while(n==0)

int A=5,B=10;

cout<<"Value of A = "<<A<<" and Value of B = "<<B<<endl;

cout<<"Address of A = "<<&A<<" and Address of B = "<<&B<<endl;

int* p1=&A;

cout<<"Value of A using pointer p1 is "<<*p1<<endl;


p1=&B;

cout<<"Value of B using pointer p1 is "<<*p1<<endl;

cout<<"Value of pointer p1 is "<<p1<<" Address of pointer p1 is "<<&p1<<endl;

int *p2=&A;

p1=p2;

*p1=15;

cout<<"Value of A after setting by p1 "<<A<<endl;

*p2=3;

cout<<"Value of A after setting by p2 "<<A<<endl;

cout<<"Enter 0 to run program again\n";

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>

using namespace std;

void swap(int *num1,int* num2)

{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;

cout<<"enter 2nd values";

cin>>b;

cout<<"value = "<<a<<" value2= "<<b<<endl;

swap(&a,&b);

cout<<"interger1 = "<<a<<" interger2 = "<<b<<endl;

cout<<"Enter character 'e' to exit the program";

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>

using namespace std;

void incdec(int *n,int p); // argument pass +return a value

int main()

{ int p;

int num;

cout<< "enter number" <<endl;

cin>>num;

cout<<"chose inc or dec"<<endl;

cin>>p;

incdec(&num,p);

cout<<num;
return 0;}

void incdec(int *n,int p)

{if (p==1)

*n=*n+1;

else if (p==2)

{*n=*n-1;}

else

cout<< "error ,enter 1 or 2"<< endl;}

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>

using namespace std;

void factorial(int *a);

int main()

{int b;

cout<<"enter a number"<<endl;

cin>>b;

factorial(&b);

cout<<b;

return 0;}

void factorial(int *a)

{int fac;

fac=1;

for (int i=*a; i>0; i-- )

{ 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>

using namespace std;

int main()

{ int i;

int a[10]={1,2,3,4,5,6,7,8,9,0};

for ( i=0;i<10 ;i++)

{cout<< "array"<<i<< "point"<<a[i]<<endl;}

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>

using namespace std;

int main()

{ int stu[5];

stu[5]={0};

cout <<"enter marks"<<endl;

for (int i=0;i<5;i++)

{ cin>>stu[i];

cout<< "students mark"<< stu[i]<<endl;}


int avg,total;

total=0;

for(int i=0;i<5;i++)

{ total=total+stu[i]; }

cout<<"student total"<< total<< endl;

cout<<"average"<< total/5<< endl;

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>

using namespace std;

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>

using namespace std;

int main() {double a[10]={1.1,1.2,1.4,1.4,1.2,1.5,1.6,1.2,1.3,1.7};

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>

using namespace std;

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)

{cout<<"number found "<<endl;}

else {cout<<"value not found"<<endl;}

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>

using namespace std;

int main()

{char a[6];

char *a1="rimsha";

char *a2="kanwal";

string s1="rimsha";

strcpy(a,a2);

cout<<"Size of string ="<<a1<<strlen(a2)<<endl;

string s2=" kanwal";

s1+=s2;

cout<<"Size of string="<<s1<<s1.size()<<endl;

strcat(a1,a2);
cout<<a1<<" "<<strlen(a1)<<endl;

return 0;}

You might also like