You are on page 1of 7

I.

We have the following


code, which plots the graph
from right side. This program
should be modified in such a
way, that will plot the given
graphs in laboratory work.
W = 1024;
x = -W/2:1:W/2;
xmat = meshgrid(x);
ymat = xmat;
R = sqrt(xmat.^2+ymat.^2);
%filter it out!
filter = (xmat > 0 & R > 100);
imshow(filter)

A)
clf
W = 1024;
x = -W/2:1:W/2;
xmat = meshgrid(x);
ymat = xmat';
R = sqrt(xmat.^2+ymat.^2);
%filter it out!
filter = (xmat <0 & R<100 | xmat >
0 & R > 100);
imshow(filter)

B)
clf
W = 1024;
x = -W/2:1:W/2;
xmat = meshgrid(x);
ymat = xmat';
R = sqrt(xmat.^2+ymat.^2);
%filter it out!
filter = (xmat >0 & R>100 | ymat >
0 & R<100);

imshow(filter)

C)
clf
W = 1024;
x = -W/2:1:W/2;
xmat = meshgrid(x);
ymat = xmat';
R = sqrt(xmat.^2+ymat.^2);
%filter it out!
filter = ((-xmat >0
| ymat >0) &
(xmat>0 | ymat<0) );
imshow(filter)

D)
clf
W = 1024;
x = -W/2:1:W/2;
xmat = meshgrid(x);
ymat = xmat;
R = sqrt(xmat.^2+ymat.^2);
%filter it out!
filter = (xmat < 0 | ymat > 0)&
(xmat > 0 | ymat < 0) | (R < 512);
imshow(filter)

E)
clf
W = 1024;
x = -W/2:1:W/2;
xmat = meshgrid(x);
ymat = xmat';
R = sqrt(xmat.^2+ymat.^2);
%filter it out!
filter= (ymat < 0 & R>512 | ymat > 0 & R < 512);

imshow(filter)
F)
clf
W = 1024;
x = -W/2:1:W/2;
xmat = meshgrid(x);
ymat = xmat;
R = sqrt(xmat.^2+ymat.^2);
%filter it out!
filter = (xmat > 0 & R > 512);
imshow(filter)

Modifying the given code, I obtained asked graphs and


it didn't me a lot to modify the rest cases after 2nd one,
because I needed a little time to understand what's the
role of & and |.

II.
Create a program that would ask for two boolean values
(true or false, 0 or 1) and would output the result of the
XOR operation performed on them. Use only And, Or and Not
operations.
>>> a=0;
b=1;
c=(a&!b)|(!a&b);
disp(c)
>>> 1

This microscopic program is with replaced XOR with


and, or and not. It looks funny :D, but it's really simple
exercises, so it will have a simple solve.

III.
We should write a program which will recognize the
number of variables and knowing it, will be displayed on
the screen the results for any case.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[20];
char b[20];
char c[20];
int result[20];
int n,i;
system("clear");
printf("\n\n\n \tEnter number of variables = ");
scanf("%d",&n );
printf("\n");
printf("---------------------------------------------\n\n");
if(n==2)
{
strcpy(a,"0011");
strcpy(b,"0101");
}
else if(n==3)
{
strcpy(a,"00001111");
strcpy(b,"00110011");
strcpy(c,"01010101");
}
switch(n)
{
case 2:
printf(" \ta = %s\n \tb = %s",a,b);
printf("\n");
printf("\n---------------------------------------------\n");
printf("\n");
printf("Result for each case\n\n");
printf("a*b = ");
for(i=0;i<strlen(a);i++)
{
if ((a[i]==b[i]) && (a[i]=='0'))
{
result[i]=0;
}
else if((a[i]==b[i])&&(a[i]=='1'))
{
result[i]=1;
}
else
{
result[i]=0;
}
}
for(i=0;i<strlen(a);i++)
{
printf("%d",result[i]);
}
printf("\n\n");
printf("a+b = ");
for(i=0;i<strlen(a);i++)
{
if ((a[i]==b[i]) && (a[i]=='0'))
{
result[i]=0;
}
else if((a[i]==b[i])&&(a[i]=='1'))
{
result[i]=1;
}
else {result[i]=1;
}
}
for(i=0;i<strlen(a);i++)
{
printf("%d",result[i]);

}
printf("\n\n");
getchar();
break;
case 3:
printf(" \ta = %s\n \tb = %s\n \tc = %s\n",a,b,c);
printf("\n");
printf("---------------------------------------------\n");
printf("\n");
printf("Result for each case\n\n");
printf("a*b*c = ");
for(i=0;i<strlen(a);i++)
{
if ((a[i]==b[i])&&(b[i]==c[i]) && (a[i]=='0'))
{
result[i]=0;
}
else if((a[i]==b[i])&&(b[i]==c[i])&&(c[i]=='1'))
{
result[i]=1;
}
else
{
result[i]=0;
}
}
for(i=0;i<strlen(a);i++)
{
printf("%d",result[i]);
}
printf("\n\n");
printf("a+b+c = ");
for(i=0;i<strlen(a);i++)
{
if ((a[i]==b[i]) &&(b[i]==c[i])&&(a[i]=='0'))
{
result[i]=0;
}
else
{
result[i]=1;
}
}
for(i=0;i<strlen(a);i++)
{
printf("%d",result[i]);
}
printf("\n\n");
printf("a*b+c = ");
for(i=0;i<strlen(a);i++)
{
if ((a[i]==b[i])&&(b[i]==c[i])&&(c[i]=='0'))
{
result[i]=0;
}
else if ((a[i]==b[i])&&(b[i]='0')&&(c[i]=='1'))
{
result[i]=1;
}
else if ((a[i]==c[i])&&(c[i]='0')&&(b[i]=='1'))
{
result[i]=0;
}
else if ((c[i]==b[i])&&(b[i]=='1')&&(a[i]=='0'))
{
result[i]=1;
}
else if ((c[i]==b[i])&&(b[i]=='0')&&(a[i]=='1'))
{
result[i]=0;
}
else if ((a[i]=='1')&&(c[i]=='1')&&(b[i]=='0'))
{

result[i]=1;
}
else if ((a[i]=='1')&&(b[i]=='1')&&(c[i]=='0'))
{
result[i]=1;
}
else if ((a[i]==b[i])&&(b[i]==c[i])&&(c[i]=='1'))
{
result[i]=1;
}
}
for (i=0;i<strlen(a);i++)
{
printf("%d",result[i]);
}
printf("\n\n");
printf("a+b*c = ");
for(i=0;i<strlen(a);i++)
{
if ((a[i]==b[i])&&(b[i]==c[i])&&(c[i]=='0'))
{
result[i]=0;
}
else if ((a[i]==b[i])&&(b[i]='0')&&(c[i]=='1'))
{
result[i]=0;
}
else if ((a[i]==c[i])&&(c[i]='0')&&(b[i]=='1'))
{
result[i]=0;
}
else if ((c[i]==b[i])&&(b[i]=='0')&&(a[i]=='1'))
{
result[i]=1;
}
else if ((c[i]==b[i])&&(b[i]=='1')&&(a[i]=='0'))
{
result[i]=1;
}
else if ((a[i]==c[i])&&(c[i]=='1')&&(b[i]=='0'))
{
result[i]=1;
}
else if ((a[i]==b[i])&&(b[i]=='1')&&(c[i]=='0'))
{
result[i]=1;
}
else if ((a[i]==b[i])&&(b[i]==c[i])&&(c[i]=='1'))
{
result[i]=1;
}
}
for(i=0;i<strlen(a);i++)
{
printf("%d",result[i]);
}
printf("\n");
printf("\n");
getchar();
break;
}
printf("---------------------------------------------\n\n");
return 0;
}

Results
1)
Enter number of variables = 3
--------------------------------------------a = 00001111
b = 00110011
c = 01010101
--------------------------------------------Result for each case
a*b*c = 00000001
a+b+c = 01111111
a*b+c = 01010001
a+b*c = 00011111
---------------------------------------------

2)

Enter number of variables = 2

--------------------------------------------a = 0011
b = 0101
--------------------------------------------Result for each case
a*b = 0001
a+b = 0111
---------------------------------------------

You might also like