Answered by:
sunday special quiz contd.........
Question

Answers








Well this is the program for integer... let someone else post the prog for the float or else i will do it in some time...
Code Snippet#include<stdio.h>
#include<conio.h>
void main()
{
char s[]="1234";
int number=0,i;
for(i=0; s[i]!='\0'; i++)
{
number *= 10;
number += s[i]  '0';
}
printf("%d", number);
}


Well if you want those constraints, we can have them like this, i m writing it here directly so there can be problems...
Code Snippet#include<stdio.h>
#include<conio.h>
void main(int argc, char *argv[])
{
// char s[]="1234";
int number=0,i;
for(i=0; argv[1][i]!='\0'; i++)
{
if(argv[1][i] <= '0'  argv[1][i] >='9')
{
printf("Sorry the string is not of numbers");
exit(1);
}
number *= 10;
number += s[i]  '0';
}
printf("%d", number);
}





Well there is a possibility that the array has elements in some sequence.. lets say asscending or descending.. in that case the complexity will be only O(1), you need to check only one condition.. now where can we find such thing.. remember when we create a binary search tree, the elements are arranged in an order, you can directly get the max and min number in a tree..
so if numbers are in a sequence, you directly get the max or min from it... 




Well the logic is simple, you know how we calculate the determinant,
like for
1 2 3
4 5 6
7 8 9
we have the formula 1*(5*9  8*6)  2*(4*9  6*7) + 3*(4*8  5*1)
If you expand it, you will get this format...
(1*5*9 + 4*8*3 + 7*2*6)  ( 7*5*3 + 4*2*9 + 1*8*6)
Try to get the pattern, i have used it in my program...
This patter was given by a mathematician, i dont remember the name, but it works for any N*N matrix, and its easy to code... you can also find determinant in another way or recursive fassion, but i have not tried it out.. may be you can find it out... 

All replies











Well this is the program for integer... let someone else post the prog for the float or else i will do it in some time...
Code Snippet#include<stdio.h>
#include<conio.h>
void main()
{
char s[]="1234";
int number=0,i;
for(i=0; s[i]!='\0'; i++)
{
number *= 10;
number += s[i]  '0';
}
printf("%d", number);
}


Well if you want those constraints, we can have them like this, i m writing it here directly so there can be problems...
Code Snippet#include<stdio.h>
#include<conio.h>
void main(int argc, char *argv[])
{
// char s[]="1234";
int number=0,i;
for(i=0; argv[1][i]!='\0'; i++)
{
if(argv[1][i] <= '0'  argv[1][i] >='9')
{
printf("Sorry the string is not of numbers");
exit(1);
}
number *= 10;
number += s[i]  '0';
}
printf("%d", number);
}









Well there is a possibility that the array has elements in some sequence.. lets say asscending or descending.. in that case the complexity will be only O(1), you need to check only one condition.. now where can we find such thing.. remember when we create a binary search tree, the elements are arranged in an order, you can directly get the max and min number in a tree..
so if numbers are in a sequence, you directly get the max or min from it... 




Which one are you talking about...
As for the determinant, i had made it on my own, without any help... here is the code for it.. I m sure you will like it...
Code Snippet// This program will find the determinant value of a square matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[10][10],deter,n,i,j,k,k1,add=0, sub=0, temp=1;
clrscr();
printf("Please give the row or column No. of a square matrix = ");
scanf("%d", &n);
printf("Please give the values of the Matrix");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("Row no %d Column No. %d = ",i+1,j+1);
scanf("%d",&mat[i][j]);
}
}
// Now we will start calculating the addition part...
for (k=0;k<n;k++)
{
temp=1;
for (i=k,k1=0,j=0; k1<n; k1++, j++, i++)
{
i=i%n;
temp*=mat[i][j];
}
add+=temp;
}
// Now we will start the subtraction portion....
for (k=1; k<=n; k++)
{
temp=1;
for (i=nk, k1=0, j=0; k1<n; k1++, j++, i)
{
i= (i+n)%n;
temp*=mat[i][j];
}
sub+=temp;
}
deter = add  sub;
printf("\nThe matrix is :");
for(i=0; i<n; i++)
{
printf("\n");
for(j=0; j<n; j++)
{
printf(" %d", mat[i][j]);
}
}
printf("\n The Determinant of the matrix is %d", deter);
getch();
} 

Well the logic is simple, you know how we calculate the determinant,
like for
1 2 3
4 5 6
7 8 9
we have the formula 1*(5*9  8*6)  2*(4*9  6*7) + 3*(4*8  5*1)
If you expand it, you will get this format...
(1*5*9 + 4*8*3 + 7*2*6)  ( 7*5*3 + 4*2*9 + 1*8*6)
Try to get the pattern, i have used it in my program...
This patter was given by a mathematician, i dont remember the name, but it works for any N*N matrix, and its easy to code... you can also find determinant in another way or recursive fassion, but i have not tried it out.. may be you can find it out... 


