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/ 5
DSA With Coding Army
Assignment Solution: 1st Week : 2nd Date : 03/12/2024
1. Calculate the product of all the elements in the given array. Solution : #include <iostream> using namespace std; int main() { int arr[5]={1,2,3,4,5}; int pdt = 1; for(int i=0;i<5;i++){ pdt = pdt*arr[i]; } cout<<pdt; return 0; } 2. Find the second largest element in the given Array in one pass. Solution : #include <iostream> using namespace std; int main() { int arr[6]={12 ,35, 1, 10, 29, 1}; int max1=max2=INT_MIN; for(int i=0;i<5;i++){ if(max1<a[i]){ max2=max1; max1=a[i]; } else if(max2<a[i] && a[i]!=max1){ max2=a[i]; } } if(max2==INT_MIN){ cout<<”No second element exists”<<endl; } else cout<<max2<<endl; return 0; } 3. Find the minimum value out of all elements in the array. Solution : #include <iostream> using namespace std; int main(){ int arr[5] = {1,2,3,4,5}; int min = INT_MAX; for(int i=0;i<5;i++){ min = min(min,arr[i]); } cout << min; return 0; } 4. Given an array, predict if the array contains duplicates or not. Solution : #include<iostream> using namespace std; int main() { int arr[5]={1,2,2,4,5}; bool flag = false; for(int i=0;i<5;i++){ for(int j=i+1;j<5;j++){ if(arr[i]==arr[j]){ flag = true; cout<<arr[i]; break; } } } if(flag==false) cout << ”No duplicate”; return 0; } 5. WAP to find the smallest missing positive element in the sorted Array that contains only positive elements. Solution : #include<iostream> using namespace std; int main() { cout<<”enter 5 elements of the array”<<endl; int a[5]; for(int i=0;i<5;i++){ cin>>a[i]; } int x=0; bool flag=false; for(int i=0;i<n;i++){ if(a[i]!= x){ cout<<x<<endl; flag=true; break; } else x++; } if(flag==false) cout<<x<<endl; return 0; } 6. Predict the output : int main() { int sub[50], i ; for ( i = 0 ; i <= 48 ; i++ ) ; { sub[i] = i ; cout<<sub[i]<<endl ; } return 0; } Solution : 49 Note:- Please try to invest time doing the assignments which are necessary to build a strong foundation. Do not directly Copy Paste using Google or ChatGPT. Please use your brain 😃.