Wednesday, November 26, 2008

C Programming Solved - Session 4

13. Largest and Smallest Values within the input data:

#include
#include
void main()
{
int MAX_INT, MIN_INT;

int numbers;
printf("Program to find Max and Min number from a list of integers");
printf("How many numbers would you like to enter? ");
scanf("%d",&numbers);

printf("Alright, please input those %d numbers now.",numbers);
for(int n = 0; n < numbers; n++)
{
int num;
scanf("%d",&num);
if(n==0)
{
MIN_INT = num;
MAX_INT = num;
}
if(num < MIN_INT)
{
MIN_INT = num;
}
if(num > MAX_INT)
{
MAX_INT = num;
}
}
printf("\n\nMaximum Number=%d Minimum Number=%d",MAX_INT,MIN_INT);
getch();
}


--------------------------------------------------------------------------


14. Reverse the Order of the integers and dispaly them:

#include
#include
void main()
{
int x[20];
int temp[20];
clrscr();
printf("Please enter 20 integers:\n");
for(int i=0;i<20;i++)
{
scanf("%d",&x[i]);
temp[19-i]=x[i];
}
printf("Reverese list:\n");
for(i=0;i<20;i++)
printf("%d ",temp[i]);
getch();
}


---------------------------------------------------------------------------------

15. Add/ Subtract/Multiply Two Matrics:

#include
#include
void main()
{
int a[3][3],b[3][3];
int x=0;
clrscr();
printf("Please enter first matrix:\n");
for(int i=0;i<3;i++)
{
for(int j=0; j<3;j++)
scanf("%d",&a[i][j]);
}
printf("Please enter second matrix:\n");
for(i=0;i<3;i++)
{
for(int j=0; j<3;j++)
scanf("%d",&b[i][j]);
}
while(x==0)
{
int choice = 5;
printf("\nPlease enter choice: 1-> Add, 2-> Subtract, 3-> Multiply, 4-> Exit");
scanf("%d",&choice);
switch(choice)
{
case 1:
for(i=0;i<3;i++)
{
for(int j=0; j<3;j++)
{
printf("%d ",a[i][j]+b[i][j]);
}
printf("\n");
}
break;
case 2:
for(i=0;i<3;i++)
{
for(int j=0; j<3;j++)
{
printf("%d ",a[i][j]-b[i][j]);
}
printf("\n");
}
break;
case 3:
for(i=0;i<3;i++)
{
for(int j=0; j<3;j++)
{
int tmp = 0;
for(int k=0;k<3;k++)
{
tmp+= a[i][k]*b[k][j];
}
printf("%d ",tmp);
}
printf("\n");
}
break;
case 4:
x=1;
break;
}
}
getch();
}

No comments: