Uploaded by RISHI MAKHARIA (RA2011031010032)

RISHI DSA

advertisement
RA2011031010032
RISHI MAKHARIA
DSA ASSIGNMENT:-1
Simple Question:6) Write a program that accepts a string S as input and print the
second vowel that occurs in the string S if such a vowel exists
otherwise an appropriate message.
Code:
#include <iostream> #include
<string> using namespace std;
int main() { string str;
cout<<"Enter the string :";
cin>>str; int
l=str.length(),cnt=0; for(int
i=0;i<l;i++) {
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o'
|| str[i]=='u')
{ cnt++;
if(cnt==2)
{
cout<<"The second vowel is --> "<<str[i]<<" in position
"<<i+1;
break;
}
} } if(cnt==0) { cout<<"There is no vowel character
present in the string";
} else
if(cnt==1)
{
cout<<"There is only 1 vowel character present in the string";
}
else
return 0;
}
Output :
Dry Run:-
Difficult Questions:13. Given an array arr[] and an integer K where K is smaller than size of
array, the task is
tofind the Kth smallest element in the given array. It is given that all
array elements are distinct.
Input: size of array = 5
Array [] = 7 10 4 5 2
Kth element = 4
Output: 7 (since 4th smallest element in the array is 7)
Code:
#include<iostream>
using namespace std;
int main(){
int n; cout<<"Enter size of
array : "; cin>>n;
int arr[n]; for(int i = 0;
i < n; i++){ cin>>arr[i];
}
for(int i =0; i<n-1; i++){
for(int j = 0; j<n-1-i; j++){
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
int x; cout<<"Enter
element : "; cin>>x;
if(x<n){ cout<<"The "<<x<<"th element
is:"<<arr[x-
1]; }else{ cout<<"value of x should be smaller than the length of the
array";
}
}
OUTPUT :
Dry Run:-
Download