Professional Documents
Culture Documents
Dsa Assignment No2
Dsa Assignment No2
Write c++ program to create two pointers of integer type,now take values in these pointers
and pass them to a function.function will update both value by adding 1 to them .
display value in main
#include <iostream>
using namespace std;
void Inc(int &x, int &y) {
++x;
++y;
}
int main() {
int x = 0, y = 0;
int *pX = &x;
int *pY = &y;
cout<<"before updatation"<<endl;
cout << *pX << " " << *pY << '\n';
Inc(*pX, *pY);
cout<<"after updatation"<<endl;
cout << *pX << " " << *pY << '\n';
return 0;
}
OUTPUT
Program #02
Create a dynamic array of user defined size. Now take input in array from user. Take a
new (integer) value from user and search that how many times the entered number is
present in the array.
#include<string>
using namespace std;
int main()
{
int size;
int numToSearch;
int counter=0;
int* numbers;
//Create a dynamic array of user defined size.
cout<<"Enter the size of the array: ";
cin>>size;
numbers=new int[size];
//Now take input in array from user.
for(int i=0;i<size;i++){
cout<<"Enter the number "<<(i+1)<<": ";
cin>>numbers[i];
}
//Take a new (integer) value from user and search that how many
//times the entered number is present in the array.
cout<<"\nEnter the number to search: ";
cin>>numToSearch;
for(int i=0;i<size;i++){
if(numbers[i]==numToSearch){
counter++;
}
}
cout<<"\n"<<counter<<" times the number "<<numToSearch<<" is present in the
array.\n\n";
return 0;
}
OUTPUT
Program#03
Write a program to create a dynamic array of user defined size. Array should be of
character type. Write a function ChangeCase() that should convert all the small
alphabets to capital and vice versa. All array operations should be done using pointers.
#include<iostream>
using namespace std;
return 0;}
OUTPUT:
Program #04
Write a program to create a dynamic array of user defined size. Size should be in the
range of 0 to 15. Write a function FindLarge that should ask user to enter a non-
negative number. Function should find the next largest number than the input
number in the list.
#include <iostream>
using namespace std;
// Function to find the next largest number than the input number in the array
int FindLarge(int array[], int size){
int x;
// Keep asking the user for a non-negative number until a valid input is provided
do{
cout<<"Enter a non-negative number: ";
cin>>x;
}while(x < 0);
// Initialize the largest number found so far with the input number
int largest = x;
for(int i = 0; i < size; i++){ // Iterate through the array to find the next largest number
if(array[i] > x){
largest = array[i];
// Return the next largest number found
return largest;
}
}
// If no number larger than the input number is found, return the input number + 1
return largest + 1;
}
int main(){
int size;
// Keep asking the user for the size of the array until a valid input is provided
do{
cout<<"Enter size of array (between 0 and 15): ";
cin>>size;
}while(size > 15 || size < 0);
// Dynamically allocate memory for the array based on the user input size
int *array;
array = new int[size];