C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf
Subscribe to:
Post Comments (Atom)
7 comments:
it wont give the desired result if array is 3 2 8 7 5 it will print 5 as the second smallest no. instead of 2 hence loop for secondsmall shud begin with 0 instead of 1
*instead of 3
it is better to sort elements and find whatever we want....(may increase complexity of prog(maybe n^2))
i made it using only 1 for loop, check it out (take array as input or declare it as you wish, already understood, i've declared)
#include
int main()
{
int a[]={5,2,1,0,7,8,-5,-8};
int i,p=a[0],n=a[0],s=a[0];
for(i=1;i<8;i++)
{
if(a[i]<n)
n=a[i];
if(s!=n)
{
p=s;
s=n;
}
}
printf("Second Largest Number is %d",p);
return 0;
}
//crack this function if you can.(0 is considered as error, I mean you can't pass 0 from array)
#include
int secsmall(int a[], int size){
int s1 = a[0], s2, flg = 0, i;
if(size < 2)
return 0;
for(i = 1; i < size; i++){
if(flg){
if(a[i] < s1){
s2 = s1;
s1 = a[i];
}
if(a[i] < s2 && a[i] > s1){
s2 = a[i];
}
}
else{
if(a[i] < s1){
s2 = s1;
s1 = a[i];
flg = 1;
}
if(a[i] > s1){
s2 = a[i];
flg = 1;
}
}
}
if(flg)
return s2;
return 0;
}
main(){
int a[] = {3, 3, 3};
printf("%d\n", secsmall(a, 3));
}
what about this input 4 50 2 60 30
Post a Comment