0% found this document useful (0 votes)
39 views7 pages

Dsa Assignment No2

The document contains programs to create dynamic arrays of integer and character types in C++. It includes functions to update values by passing pointers, search for a number in an array, change case of characters, and find the next largest number in an array.

Uploaded by

QUDDUS LARIK
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)
39 views7 pages

Dsa Assignment No2

The document contains programs to create dynamic arrays of integer and character types in C++. It includes functions to update values by passing pointers, search for a number in an array, change case of characters, and find the next largest number in an array.

Uploaded by

QUDDUS LARIK
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/ 7

Program #01

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;

// Function to change the case of characters in the array


void ChangeCase(char str[], int n) {
for (int i = 0; i < n; i++) {
// If the character is lowercase, convert it to uppercase
if (str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 32;
// If the character is uppercase, convert it to lowercase
else if (str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
}
}
int main() {
// Prompting the user to enter the size of the array
cout << "Enter the size of the array:\n";
int n;
cin >> n;

// Dynamically allocating memory for the character array


char* str = new char[n];
char letters[n]; // This line might cause an error, better to use dynamic allocation as
above
// Prompting the user to enter characters for the array
for(int i = 0; i < n; ++i) {
cout << "Enter character " << i+1 << ": ";
cin >> letters[i]; // Storing characters in the array
}
// Calling the function to change the case of characters
cout << "After changing case:" << endl;
ChangeCase(letters, n);
// Displaying the characters after changing the case
for(int i = 0; i < n; ++i) {
cout << letters[i] << " " << endl; // Displaying each character
}
cout << letters << endl; // Displaying the entire string
// Deallocating the memory allocated for the character array
delete[] str;

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

// Prompt the user to input items in the array


cout<<"Input items in the array: \n";
for(int i = 0; i < size; i++){
cin>>array[i];
}
// Call the FindLarge function to find the next largest number in the array
int large = FindLarge(array, size);

// Display the next largest number found


cout<<"Next largest number than the input number is: "<<large<<endl;
return 0;
}
OUTPUT:

You might also like