Assignment 2 ->Question3:
You are given a non-negative sequence of numbers, ending with a -1. You can assume that there are at least two numbers before the ending -1.
Note : -1 is not a part of input. It only signifies that input has ended.
Note : -1 is not a part of input. It only signifies that input has ended.
Let us call the sequence x0 x1 ... xn -1.
You have to output the second largest element of the sequence. if there is no second largest element in the sequence then output 0.
IF YOU NEED EXPLANATION YOU CAN COMMENT .
PROGRAM:
#include<stdio.h>
int main()
{
int a[100],b[100],i=-1,n=0,m=0,temp;
do
{
i++;
scanf("%d",&a[i]);
}while(a[i]!=-1);
for(int j=0;a[j]!=-1;j++)
{ b[j]=a[j];
n++;}
for(i=1;i<=2;i++)
{
for(int k=0;k<n-i;k++)
{
if(b[k]==b[k+1])
{ m++; }
if(b[k]>b[k+1])
{
temp=b[k];
b[k]=b[k+1];
b[k+1]=temp;
}
}
}
if(m>1)
{
printf("0");
}
else
{
printf("%d",b[n-2]);
}
return 0;
}
PLEASE SUBSCRIBE OUR CHANNEL.
<CLICK HERE>
FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100% correct solutions.
<CLICK HERE>
FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100% correct solutions.
No comments:
Post a Comment
If you have any problems related to solutions or any concept please let me know.