Tuesday, August 14, 2012

Reverse Diagonal Elements of a Square Matrix using C language

Genaerally 3x3 matrix is represented as
| a1 a2 a3|
| a4 a5 a6|
| a7 a8 a9|
We need reverse diagonal elements means a3,a5,a7. Now observe the indices of these elements they are (0,2),(1,1),(2,0). By observing the indices we can say that by adding the row and column indes we are getting the one less than the size of matrix. means if the matrix size is 4x4 then the sum of reverse diagonal indeces is 4-1=3. based on this logic program is written. The matrix can be represented by the two dimensional array so we need two dimensional array. to load and manipulate this array we need two for loops. one for loop deals with the row and second for loop deals with the columns.
This program takes the size of square matrix as the input and according to it the elements of the matrix are loaded into the 2Darray.
I will deal the For loop indetail after.
Progrm:
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,a[20][20],k=1,n;
printf("\n Enter the size of square matrix");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=k;
k++;
}
}
printf("\n Reverse Diagonal elements:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i+j==n-1)
{
printf("\n %d",a[i][j]);
}
}
}
getch();
}

 

Lower Triangle elements of a Square Matrix using C language

Genaerally 3x3 matrix is represented as
| a1 a2 a3|
| a4 a5 a6|
| a7 a8 a9|
Lower triangle elements are the a2,a3,a6. we need to print theese elements. The indices of these elements are(0,1),(0,2),(1,2). By observing these elements we can say that the index of the row is less than the column. by using conditional statements i.e if statement we can print the required elements.two for loops one for row and one for column are used.

Program:
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,a[20][20],k=1,n;
printf("\n Enter the size of square matrix");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=k;
k++;
}
}
printf("\n lower triangle elements:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
{
printf("\n %d",a[i][j]);
}
}
}
getch();
}

Upper Triangle of Square Matrix using C language

#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,a[20][20],k=1,n;
printf("\n Enter the size of square matrix");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=k;
k++;
}
}
printf("\n Upper triangle elements:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(j<i)
{
printf("\n %d",a[i][j]);
}
}
}
getch();
}

Reverse Diagonal elements of a Square Matrix using single for loop in C language

#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,a[20][20],k=1,n;
printf("\n Enter the size of square matrix");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=k;
k++;
}
}
printf("\n Reverse Diagonal elements:");
for(i=0,j=0;i<n,j<n;i++,j++)
{
printf("\n %d",a[i][n-1-i]);
}
getch();
}

Forward Diagonal Elements of a Square Matrix using C language

#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,a[20][20],k=1,n;
printf("\n Enter the size of square matrix");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=k;
k++;
}
}
printf("\n Forward Diagonal elements:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
printf("\n %d",a[i][j]);
}
}
}
getch();
}

Monday, August 13, 2012

Write a program to find Nth prime number in a given range, inclusive of the first and the last element in that range

#include<stdio.h>
main()
{
 long int i=0,j=0;
 long int n1,n2,t=0,v;
 int k;
 int flag=0;
 printf("\n FROM the number n1 To the number n2 enter the nth prime no you want ");
 scanf("%ld %ld %ld",&n1,&n2,&v);
 if(v>0&&n2>n1&&n1<1500000&&n2<1500000)
 {
 for(i=n1;i<=n2;i++)
 {
  for(j=1,k=0;j<=i;j++)
  {
   if(i%j==0)
   k++;
  }
 if(k==2)
 {
 t++;
 if(t==v)
 {
 printf("\n %ld",i);
 flag=1;
 break;
 }
 }
 }
 if(flag==0)
 {
 printf("\n no prime number is present at this index");
 }
 }
 else
 {
 printf("Invalid Input");
 }
 getch();
 }


Sunday, August 12, 2012

Strong Number program using C language


#include<conio.H>
#include<stdio.h>
void main()
{
int i,k,t,x,r,s=0;
clrscr();
printf("\n Enter any number");
scanf("%d",&x);
t=x;
while(x>0)
{
k=1;
r=x%10;
for(i=1;i<=r;i++)
{
k=k*i;
}
s=s+k;
x=x/10;
}
if(t==s)
{
printf("\n %d is a strong number",t);
}
else
{
printf("\n %d is not a strong number",t);
}
getch();
}

Saturday, August 11, 2012

Program to find Prime number from 0 to given range C language



#include<conio.h>
#include<stdio.h>
void main()
{
int n,i,k;
clrscr();
printf("\n Enter any number");
scanf("%d",&n);
i=1;
k=0;
while(i<=n)
{
if(n%i==0)
{
k++;
if(k>2)
{
printf("\n Enterd number %d is not prime number",n);
break;
}
}
i++;
}
if(k<=2)
{
printf("\n Enterd number %d is prime number",n);
}
getch();
}

Sunday, July 8, 2012

Phase Shift Keying using matlab/psk using matlab

Phase Shift Keying 

PSK:

          Psk is the one of the digital modulation technique. in this technique phase of the carrier signal is varied according to the message signal. In case of binary PSK we use two phases 0 and 180. To transmit the bit one we transmit the carrier signal as it is with out the phase change. To transmit the bit zero we transmit the carrier wave with phase change of 180 with respect to the original carrier wave. the power consumed by this technique is more than the ASK because this technique uses carrier to transmit the bit zero also.similar to any digital modulation technique PSK also uses the finite number of distinct signals to represent the digital data. incase of bpsk i.e bipolar phase 
shift keying we use two distinct signals which are in phase difference of 180 degrees. we can implement the PSK in two methods one is in the form of differential phase shift keying in this the phase of the carrier is changed with respect to the previous bit. in other case the phase is changed with the instantaneous message signal which is in digital nature.

a=input('enter the sequence')
k=length(a)
t=[0.01:0.01:k]
f=7
for i=1:1:k

    if a(i)==1
        a(i)=1
    else
        a(i)=0
    end
end
for i=1:1:k
    m((i-1)*100+1:i*100)=a(i)
end
subplot(211)
plot(m)
xlabel('time')
ylabel('amplitude')
title('message signal')
wt=2*pi*f*t
c=sin(wt+(1-m).*pi)
subplot(212)
plot(c)
xlabel('time')
ylabel('amplitude')
title('phase shift keying')
 


Other Digital Modulation Techniques
1. Amplitude Shift Keying              
2. Frequency Shift Keying              
3. Hybrid Shift Keying                    

Wednesday, June 20, 2012

Amplitude Modulation using matlab

Amplitude Modulation Matlab Program
fs=400;
fo=5;
fc=25;
num_periods=40;
samp_period=(fs/fo);
totalsamp=samp_period*num_periods;
n=[0:totalsamp-1];
t=(n/fs);
m=sin(2*pi*fo*t);
N=totalsamp;
M=abs(fft(m,N))/totalsamp;
M=fftshift(M);
F=[-N/2:(N/2)-1];
Ac=1;
Ka=0;
DCoffset=1;
s=Ac*(DCoffset+Ka*m).*cos(2*pi*fc*t);
S=abs(fft(s,N))/totalsamp;
S=fftshift(S);
close all;
F2=figure;
subplot(3,1,1);
plot(t,s,t,(DCoffset+Ka*m),'--'),grid;
Maxtime=5*samp_period/fs;
xlabel('time(ms)');
subplot(3,1,2);
plot(F,M)
xlabel('frequency(khz)');
subplot(3,1,3);
plot(F,s)



 You may like Digital Modulation Techniques now you are at analog modulation
0. Frequency Modulation                     
1. Amplitude shift keying                     
2. Frequency shift keying                     
3. Phase shift keying                             
4. Hybrid Shift Keying                          

Friday, June 15, 2012

Frequency Modulation Using Matlab

clear;
close all;

%PARAMETERS
fc=200;            % carrier frequency in hertz
t0=0.15;            %time range in seconds
Df=50;            %frequency deviation constant in hertz/volt

ts=0.0005;                  % sampling interval
t=[0:ts:t0];                % time vector

%MESSAGE SIGNAL
m=[2*ones(1,t0/(3*ts)),-3*ones(1,t0/(3*ts)),ones(1,t0/(3*ts)+1)];
figure(1)
hold on
plot(m,'black');
title('Message signal');
xlabel('time');
ylabel('signal value');
set(1,'PaperPositionMode','auto');
%FM SIGNAL
fi=fc+Df*m;
s=cos(2*pi*fi.*t);
plot(s,'red');
title('FM signal');
xlabel('time');
ylabel('signal value');
hleg=legend('message signal','FM signal')



You may like Digital Modulation Techniques now you are at analog modulation
0. Amplitude Modulation         AM
1. Amplitude shift keying        ASK
2. Frequency shift keying        FSK
3. Phase shift keying                PSK

DC motor control with Pulse Width Modulation Part 1

DC Motor intro DC motor is a device which converts electrical energy into kinetic energy. It converts the DC power into movement. The typica...