Monday, September 2, 2013

Storing Student Records Using Array In 'C'


#include 
#include 
#include 
#define N 50


int Student[N][10];
char Names[N][N];


void setName(int ID);
void acceptDetails(int ID);
void printDetails(int Num);
int totalMarks(int id);
float percentage(int id);
void printThisStudent(int id);
void displayClasswise(int Num);


int main(){
 int i=0;
 int j=0;
 int Num=0;
 
 printf("\n Enter number of students:");
 scanf("%d",&Num);
 printf("\n Enter ID's for each student");
 for(i=0;i
 scanf("%d",&Student[i][0]);
 }
 
 for(i=0;i
  setName(i);
  acceptDetails(i);
 }
 
 printDetails(Num);
 displayClasswise(Num);
  
}


void acceptDetails(int ID){
 int k;
 printf("\n Enter details of %d",Student[ID][0]);
 k=1;
  printf("\n Sub1:");
  scanf("%d",&Student[ID][k]);
 k++;
  printf("\n Sub2:");
  scanf("%d",&Student[ID][k]);
 k++;
  printf("\n Sub3:");
  scanf("%d",&Student[ID][k]);
 k++;
  printf("\n Sub4:");
  scanf("%d",&Student[ID][k]);
 k++;
  printf("\n Int1:");
  scanf("%d",&Student[ID][k]);
 k++;
  printf("\n Int2:");
  scanf("%d",&Student[ID][k]);
 k++;
  printf("\n Int3:");
  scanf("%d",&Student[ID][k]);
 k++;
  printf("\n Int4:");
  scanf("%d",&Student[ID][k]);
}


void printDetails(int Num){
 int i=0,j=0,k=0;
 int count = 0;
 printf("\n ID \t name \t s1 \t s2 \t s3 \t s4 \t i1 \t i2 \t i3 \t i4 \t tot \t per ");
 for(i=0;i
  printf("\n");
  count=0;
  for(j=0;j<9 b="" j="">
   printf("%d",Student[i][j]);
   printf("\t");
   count++;
   if(count==1){
    while(Names[i][k] != '\0'){
     printf("%c",Names[i][k]);
     k++;
    }
    printf("\t");
   }
   if(count==9){
    printf("%d",totalMarks(i));
    printf("\t");
    printf("%.02f",percentage(i));
   }
  }
  printf("\n");
 }
}


int totalMarks(int id){
 int i=0,j=0;
 int sum=0;
  for(j=1;j<9 b="" j="">
   sum = sum + Student[id][j];
  }
 return sum; 
}


float percentage(int id){
 float per = 0;
 per = ((totalMarks(id))/8);
 return per;
}


void setName(int id){
 printf("\n Enter name:");
 scanf("%s",Names[id]);
 getchar();
}


void displayClasswise(int Num){
 int i=0;
 printf("\n Fail:");
 for(i=0;i
  if(percentage(i) < 40){
   printThisStudent(i);
  }
 }
 printf("\n 3rd Class:");
 for(i=0;i
  if(percentage(i) >40 && percentage(i) < 50){
   printThisStudent(i);
  }
 }
 printf("\n 2nd Class:");
 for(i=0;i
  if(percentage(i) >50 && percentage(i) < 60){
   printThisStudent(i);
  }
 }
 printf("\n 1st Class:");
 for(i=0;i
  if(percentage(i) >60 && percentage(i) < 70){
   printThisStudent(i);
  }
 }
 printf("\n Distinction:");
 for(i=0;i
  if(percentage(i) > 70){
   printThisStudent(i);
  }
 }
}


void printThisStudent(int id){
 int count = 0;
 int j=0;
 int k=0;
 printf("\n ID \t name \t s1 \t s2 \t s3 \t s4 \t i1 \t i2 \t i3 \t i4 \t tot \t per ");
  count=0;
  for(j=0;j<9 b="" j="">
   printf("%d",Student[id][j]);
   printf("\t");
   count++;
   if(count==1){
    while(Names[id][k] != '\0'){
     printf("%c",Names[id][k]);
     k++;
    }
    printf("\t");
   }
   if(count==9){
    printf("%d",totalMarks(id));
    printf("\t");
    printf("%.02f",percentage(id));
   }
  }
}

Saturday, August 31, 2013

MAT-LAB Image Processing, Basic Image Operations

MAT-LAB BASIC IMAGE OPERATIONS


Image Addition

%Adding constant to image, using inbuilt function
img = imread('face.tif');
img1 = imadd(img,128);
subplot(2,3,1), imshow(img);
title('Original');
subplot(2,3,2), imshow(img1);
title('Inbuilt function added 128');
%Manually
img2 = img;
for i=1:size(img)
for j=1:size(img)
img2(i,j) = img(i,j)+64;
end
end
subplot(2,3,3), imshow(img2);
title('Manually added 64');
%Adding two images of same size 300x300
img3 = rgb2ind(imread('pattern.tiff'),0);
img4 = uint8(zeros(300,300));
for i=1:size(img)
for j=1:size(img)
img4(i,j) = img(i,j)+img3(i,j);
end
end
%img4 = img+img3; even this works
subplot(2,3,4), imshow(img);
title('Original image1');
subplot(2,3,5), imshow(img3);
title('Original image2');
subplot(2,3,6), imshow(img4);
title('Effect of adding image1 and image2');




Image Subtraction

%Subtracting constant to image, using inbuilt function
img = imread('face.tif');
img1 = imsubtract(img,128);
subplot(2,3,1), imshow(img);
title('Original');
subplot(2,3,2), imshow(img1);
title('Inbuilt function subtracted 128');
%Manually
img2 = img;
for i=1:size(img)
for j=1:size(img)
img2(i,j) = img(i,j)-64;
end
end
subplot(2,3,3), imshow(img2);
title('Manually subtracted 64');
%Subtracting two images of same size 300x300
img3 = imread('pattern4.tif');
img4 = uint8(zeros(300,300));
for i=1:size(img)
for j=1:size(img)
img4(i,j) = img(i,j)-img3(i,j);
end
end
%img4 = img3 - img;
subplot(2,3,4), imshow(img);
title('Original image1');
subplot(2,3,5), imshow(img3);
title('Original image2');
subplot(2,3,6), imshow(img4);
title('Effect of subtracting image 1 and 2');




Image Division

%Divide image by 128, using inbuilt function
img = imread('face.tif');
img1 = imdivide(img,2);
subplot(2,3,1), imshow(img);
title('Original');
subplot(2,3,2), imshow(img1);
title('Inbuilt function divided by 2');
%Manually
img2 = zeros(300,300);
for i=1:size(img)
for j=1:size(img)
img2(i,j) = img(i,j)/128;
end
end
subplot(2,3,3), imshow(img2);
title('Manually divided by 128');
%Dividing two images of same size 300x300
img3 = imread('pattern3.tif');
img4 = zeros(300,300);
for i=1:size(img)
for j=1:size(img)
img4(i,j) = img(i,j)/img3(i,j);
end
end
%img4 = img3 - img;
subplot(2,3,4), imshow(img);
title('Original image1');
subplot(2,3,5), imshow(img3);
title('Original image2');
subplot(2,3,6), imshow(img4);
title('Effect of dividing image1 by image2');




Image Multiplication

%Multiply image by 8, using inbuilt function
img = imread('face.tif');
img1 = immultiply(img,4);
subplot(2,3,1), imshow(img);
title('Original');
subplot(2,3,2), imshow(img1);
title('Inbuilt function multiplied by 4');
%Manually
img2 = uint8(zeros(300,300));
for i=1:size(img)
for j=1:size(img)
img2(i,j) = img(i,j)*0.5;
end
end
subplot(2,3,3), imshow(img2);
title('Manually multiplied by 0.5');
%Multiplying two images of same size 300x300
img3 = uint8(ones(300,300));
for i=1:300
for j=1:300
img3(i+(i-1),j+(j-1))=2;
end
end
img4 = uint8(zeros(300,300));
for i=1:size(img)
for j=1:size(img)
img4(i,j) = img(i,j)*img3(i,j);
end
end
%img4 = img3 - img;
subplot(2,3,4), imshow(img);
title('Original image1');
subplot(2,3,5), imshow(img3);
title('Original image2');
subplot(2,3,6), imshow(img4);
title('Effect of multiplying image1 and image2');




Logical AND

%Logical AND operation
A = imread('logical1.tif');
A = logical(A);
B = imread('logical2.tif');
B = logical(B);
C = A & B;
subplot(2,3,1), imshow(A);
title('Original Image 1');
subplot(2,3,2), imshow(B);
title('Original Image 2');
subplot(2,3,3), imshow(C);
title('Effect of Logical AND');
D = logical(imread('logical3.tif'));
E = A & D;
subplot(2,3,4), imshow(A);
title('Original Image 1');
subplot(2,3,5), imshow(D);
title('Original Image 3');
subplot(2,3,6), imshow(E);
title('Effect of Logical AND');
%***********************************************
%Applications are in Object movement detection




Logical OR

% Image Logical OR
A = imread('logical4.tif');
A = logical(A);
B = imread('logical5.tif');
B = logical(B);
C = A | B;
subplot(2,3,1), imshow(A);
title('Original Image 1');
subplot(2,3,2), imshow(B);
title('Original Image 2');
subplot(2,3,3), imshow(C);
title('Effect of Logical OR');
D = logical(imread('logical6.tif'));
E = logical(imread('logical7.tif'));
F = D | E;
subplot(2,3,4), imshow(D);
title('Original Image 3');
subplot(2,3,5), imshow(E);
title('Original Image 4');
subplot(2,3,6), imshow(F);
title('Effect of Logical OR');
%Pattern analysis


Wednesday, May 22, 2013

Self explaining bubble sort code in c.

#include
#include
void getData(void);
void bubbleSort(int [],int);
void printArray(int [],int);

int main(){
getData();
}

void getData(){
int* Array;
int Size,index;

printf("\n How many numbers you would like to sort?");
scanf("%d",&Size);
Array = (int *)malloc(Size*sizeof(int));
for(index=0; index < Size; index++){
printf("\n Enter number %d:",(index+1));
scanf("%d",&Array[index]);
}
printf("\n Array entered is:");
printArray(Array,Size);
bubbleSort(Array,Size);
}

void bubbleSort(int Array[],int Size){
int index1,index2,temp,count=0;

for(index1=0;index1<=Size;index1++){
for(index2=index1+1;index2<=Size-1;index2++){
printf("\n Comparing %d with %d.",Array[index1],Array[index2]);
count++;
_sleep(1000);
if(Array[index1]>Array[index2]){
printf("\n Number %d is greater than %d.",Array[index1],Array[index2]);
printf("\n Swapping elements %d and %d",Array[index1],Array[index2]);
_sleep(1000);
temp = Array[index1];
Array[index1]=Array[index2];
Array[index2]=temp;
printf("\n Array elements after modification are:");
printArray(Array,Size);
}
else if(Array[index1]==Array[index2]){
printf("\n Number are equal");
}
else{
printf("\n Number %d is less than %d.",Array[index1],Array[index2]);
}
}
}
printf("\n Total compares : %d \n\n\n",count);
}

void printArray(int Array[],int Size){
int index;

for(index=0; index < Size; index++){
printf("\t %d",Array[index]);
}
printf("\n\n\n");
}