#include <stdio.h>
#define R 3
#define C 3
void spiralPrint(int m, int n, int a[R][C])
{
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
*/
while (k < m && l < n)
{
/* Print the first row from the remaining rows */
for (i = l; i < n; ++i)
{
printf("%d ", a[k][i]);
}
k++;
/* Print the last column from the remaining columns */
for (i = k; i < m; ++i)
{
printf("%d ", a[i][n-1]);
}
n--;
/* Print the last row from the remaining rows */
if ( k < m)
{
for (i = n-1; i >= l; --i)
{
printf("%d ", a[m-1][i]);
}
m--;
}
/* Print the first column from the remaining columns */
if (l < n)
{
for (i = m-1; i >= k; --i)
{
printf("%d ", a[i][l]);
}
l++;
}
}
}
/* Program to test above functions */
int main()
{
int a[R][C] = { {1, 2, 3},
{8, 9, 4},
{7, 6, 5}
};
spiralPrint(R, C, a);
return 0;
}
Showing posts with label User Defined Function. Show all posts
Showing posts with label User Defined Function. Show all posts
Wednesday, September 23, 2015
Monday, July 27, 2015
Friday, May 22, 2015
Print the Truth Table
Sumit Kar May 22, 2015 Basic, If Else, Switch Case, User Defined Function No comments
#include<stdio.h>
int OR(int,int);
int AND(int,int);
int XOR(int,int);
int NOT(int);
void main()
{
int a,b,ch,o;
printf("\n-:MENU:-\n1.AND\n2.OR\n3.NOT\n4.XOR\n0.Exit\n");
do{
printf("\nEnter Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf ("\nTruth Table for AND\nA B \tO");
for(a=0;a<=1;a++)
{
for(b=0;b<=1;b++)
{
o=AND(a,b);
printf("\n%d %d \t%d",a,b,o);
}
}
break;
case 2:
printf ("\nTruth Table for OR\nA B \tO");
for(a=0;a<=1;a++)
{
for(b=0;b<=1;b++)
{
o=OR(a,b);
printf("\n%d %d \t%d",a,b,o);
}
}
break;
case 3:
printf ("\nTruth Table for NOT\nA \tO");
for(a=0;a<=1;a++)
{
o=NOT(a);
printf("\n%d \t%d",a,o);
}
break;
case 4:
printf ("\nTruth Table for XOR\nA B \tO");
for(a=0;a<=1;a++)
{
for(b=0;b<=1;b++)
{
o=XOR(a,b);
printf("\n%d %d \t%d",a,b,o);
}
}
break;
case 0: exit();
break;
default: printf("\nError");
}
}while(ch!=0);
}
int OR(int a, int b)
{
if(a==0&&b==0)
return 0;
else
return 1;
}
int AND(int a, int b)
{
if (a==1&&b==1)
return 1;
else
return 0;
}
int NOT(int a)
{
if (a==0)
return 1;
else
return 0;
}
int XOR(int a,int b)
{
if (a==0&&b==1||a==1&&b==0)
return 1;
else
return 0;
}
Output
-:MENU:-
1.AND
2.OR
3.NOT
4.XOR
0.Exit
Enter Choice: 3
Truth Table for NOT
A O
1 0
0 1