0% found this document useful (0 votes)
10 views

Lab 11

The program introduces integer variables x, y, z and pointers p, q, r that point to the addresses of x, y, z. It prints the values before and after swapping x and y using a temporary variable. This demonstrates passing addresses to pointers and manipulating variables through pointers.

Uploaded by

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

Lab 11

The program introduces integer variables x, y, z and pointers p, q, r that point to the addresses of x, y, z. It prints the values before and after swapping x and y using a temporary variable. This demonstrates passing addresses to pointers and manipulating variables through pointers.

Uploaded by

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

1.

Introduce int variables x, y, z and int * pointer OUTPUT:


variables p, q, r. Set x, y, z to three distinct values. Set
p, q, r to the addresses of x, y, z respectively. a. Print
with labels the values of x, y, z, p, q, r, *p, *q, *r. b.
Print the message: Swapping values. c. Execute the
swap code: z = x; x = y; y = z; d. Print with labels the
values of x, y, z, p, q, r, *p, *q, *r.
CODE:
#include<iostream>
using namespace std;
int main(){
int x=10,y=20,z=30,*p,*q,*r;
p=&x; q=&y; r=&z;
cout<<"Values of "<<x<<endl;
cout<<"Values of "<<y<<endl;
cout<<"Values of "<<z<<endl;
cout<<"Values of "<<p<<endl; 2.Write a program that reads a group of numbers from
cout<<"Values of "<<q<<endl; the user and places them in an array of type float.
cout<<"Values of "<<r<<endl; Once the numbers are stored in the array, the program
cout<<"Values of "<<*p<<endl; should average them and print the result. Use pointer
cout<<"Values of "<<*q<<endl; notation wherever possible.
cout<<"Values of "<<*r<<endl<<endl; CODE:
cout<<"Swapping Values"<<endl<<endl; #include <iostream>
int temp=0; using namespace std;
temp=x; x=y; y=z; z=temp; int main() {
cout<<"Values of "<<x<<endl; int l;
cout<<"Values of "<<y<<endl; cout << "Enter the number of numbers: ";
cout<<"Values of "<<z<<endl; cin >> l;
cout<<"Values of "<<p<<endl; float *arr = new float[l];
cout<<"Values of "<<q<<endl; for (int i = 0; i < l; ++i) {
cout<<"Values of "<<r<<endl; cout << "Enter number " << (i + 1) << ": ";
cout<<"Values of "<<*p<<endl; cin >> *(arr + i);}
cout<<"Values of "<<*q<<endl; float avg = 0;
cout<<"Values of "<<*r<<endl;} for (int i = 0; i < l; ++i) {
avg += *(arr + i);}
avg=avg / l;
cout << "Average value of entered numbers is " <<
avg << endl;
delete[] arr;}
OUTPUT:

3. Write a program in which three variables a, b and c


of type long, short and integer are taken as user input
and 3 pointers *x, *y and *z point to their addresses. a.
Print x, y and z b. Increment each pointer by 1 (x++, y+
+, z++). Now print x, y and z again By looking at the
variation in addresses, write in your own words what int arr[maxSize];
did you observe in this task? int arr1[maxSize];
CODE: int size;
#include <iostream> cout << "Enter the size of the array: ";
using namespace std; cin >> size;
int main() { for (int i = 0; i < size; i++) {
long a,*x; cout << "Enter element " << i + 1 << ": ";
short b,*y; cin >> arr[i];}
int c,*z; reverseArray(arr, arr1, size);
x = &a; cout << "Reversed array: ";
y = &b; for (int i = 0; i < size; i++) {
z = &c; cout << arr1[i] << " ";}
cout << "Addresses:" << endl; cout << endl;}
cout << "x: " << x << endl; OUTPUT:
cout << "y: " << y << endl;
cout << "z: " << z << endl;
x++;
y++;
z++;
cout<<endl<< "Addresses after increment:" <<
5. Using pointers, write a program that takes a
endl;
string as input from user and calculates the number of
cout << "x: " << x << endl;
vowels in it.
cout << "y: " << y << endl;
CODE:
cout << "z: " << z << endl;}
#include <iostream>
OUTPUT:
#include <cstring>
using namespace std;
int countVowels(char *str) {
char *ptr = str;
int count = 0;
while (*ptr != '\0') {
if (*ptr == 'a' || *ptr == 'e' || *ptr == 'i' || *ptr ==
'o' || *ptr == 'u' ||
*ptr == 'A' || *ptr == 'E' || *ptr == 'I' || *ptr ==
'O' || *ptr == 'U') {
By looking this, I observer that the addresses of the count++;}
pointers change based on the data type and ptr++;}
memory allocation. The increment reflects the size return count;}
of each data type. int main() {
4.Write a program that takes an array as user input int maxSize = 1000;
and store it in reverse order in another array using char str[maxSize];
pointers. cout << "Enter a string: ";
CODE: cin.getline(str, maxSize);
#include <iostream> int vowels = countVowels(str);
using namespace std; cout << "Number of vowels: " << vowels << endl;}
void reverseArray(int *arr, int *arr1, int size) { OUTPUT:
for (int i = 0; i < size; i++) {
arr1[i] = arr[size - i - 1];}}
int main() {
int maxSize = 100;

You might also like