0% found this document useful (0 votes)
21 views4 pages

Arrays 1 Solutions.

The document provides solutions to various programming assignments related to arrays, including calculating the product of elements, finding the second largest element, determining the minimum value, checking for duplicates, and identifying the smallest missing positive element. Each solution is presented with C++ code snippets. Additionally, there is a section predicting the output of a specific code snippet.

Uploaded by

prakashgyan1067
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)
21 views4 pages

Arrays 1 Solutions.

The document provides solutions to various programming assignments related to arrays, including calculating the product of elements, finding the second largest element, determining the minimum value, checking for duplicates, and identifying the smallest missing positive element. Each solution is presented with C++ code snippets. Additionally, there is a section predicting the output of a specific code snippet.

Uploaded by

prakashgyan1067
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/ 4

Assignment Solutions | Arrays - 1 | Week 5

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

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

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

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

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

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

You might also like