Wednesday, October 29, 2014

C Matrix

Calculate the Sum & Difference of the Matrices

#include 
#include 
#include 
void readmatA();
void printmatA();
void readmatB();
void printmatB();
void sum();
void diff();
 
int a[10][10], b[10][10], sumarray[10][10], arraydiff[10][10];
int i, j, row1, column1, row2, column2;
 
void main()
{
    clrscr();
    printf("Enter the order of the matrix A \n");
    scanf("%d %d", &row1, &column1);
    printf("Enter the order of the matrix B \n");
    scanf("%d %d", &row2, &column2);
    if (row1 != row2 && column1 != column2)
    {
        printf("Addition and subtraction are possible \n");
        exit(1);
    }
    else
    {
        printf("Enter the elements of matrix A \n");
        readmatA();
        printf("matrix a is \n");
        printmatA();
        printf("Enter the elements of matrix B \n");
        readmatB();
        printf("matrix b is \n");
        printmatB();
        sum();
        diff();
    }
}
void readmatA()
{
    for (i = 0; i < row1; i++)
    {
        for (j = 0; j < column1; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
    return;
}
 
void readmatB()
{
    for (i = 0; i < row2; i++)
    {
        for (j = 0; j < column2; j++)
        {
            scanf("%d", &b[i][j]);
        }
    }
}
void printmatA()
{
    for (i = 0; i < row1; i++)
    {
        for (j = 0; j < column1; j++)
        {
            printf("%3d", a[i][j]);
        }
        printf("\n");
    }
}
 
void printmatB()
{
    for (i = 0; i < row2; i++)
    {
        for (j = 0; j < column2; j++)
        {
            printf("%3d", b[i][j]);
        }
        printf("\n");
    }
}
void sum()
{
    for (i = 0; i < row1; i++)
    {
        for (j = 0; j < column2; j++)
           {
            sumarray[i][j] = a[i][j] + b[i][j];
        }
    }
    printf("Sum matrix is \n");
    for (i = 0; i < row1; i++)
    {
        for (j = 0; j < column2; j++)
           {
            printf("%3d", sumarray[i][j]) ;
        }
        printf("\n");
    }
    return;
}
void diff()
{
    for (i = 0; i < row1; i++)
    {
        for (j = 0; j < column2; j++)
           {
            arraydiff[i][j] = a[i][j] - b[i][j];
        }
    }
    printf("Difference matrix is \n");
    for (i = 0; i < row1; i++)
    {
        for (j = 0; j < column2; j++)
        {
            printf("%3d", arraydiff[i][j]);
        }
        printf("\n");
    }
    return;
    getch();
}
 
Output
 
Enter the order of the matrix A
3 3
Enter the order of the matrix B
3 3
Enter the elements of matrix A
1 4 5
6 7 8
4 8 9
Matrix a is
  1  4  5
  6  7  8
  4  8  9
Enter the elements of matrix B
3 6 7
8 4 2
1 5 3
Matrix b is
  3  6  7
  8  4  2
  1  5  3
Sum matrix is
  4 10 12
 14 11 10
  5 13 12
Difference matrix is
 -2 -2 -2
 -2  3  6
  3  3  6
 
Split an Array from Specified Position & Add First Part to the End
 
#include 
#include 
void main ()
{
    int number[30];
    int i, n, a, j;
    clrscr();
    printf("Enter the value of n\n");
    scanf("%d", &n);
    printf("enter the numbers\n");
    for (i = 0; i < n; ++i)
        scanf("%d", &number[i]);
    printf("Enter the position of the element to split the array \n");
    scanf("%d", &a);
    for (i = 0; i < a; ++i)
    {
        number[n] = number[0];
        for (j = 0; j < n; ++j)
        {
            number[j] = number[j + 1];
        }
    }
    printf("The resultant array is\n");
    for (i = 0; i < n; ++i)
    {
        printf("%d\n", number[i]);
    }
     getch();
}
 
 
Output
 
Enter the value of n
4
enter the numbers
3
678
345
876
Enter the position of the element to split the array
3
The resultant array is
876
3
678
345

Sort the N Names in an Alphabetical Order

#include 
#include 
#include 
void main()
{
    char name[10][8], tname[10][8], temp[8];
    int i, j, n;
 
    printf("Enter the value of n \n");
    scanf("%d", &n);
    printf("Enter %d names n", \n);
    for (i = 0; i < n; i++)
    {
        scanf("%s", name[i]);
        strcpy(tname[i], name[i]);
    }
    for (i = 0; i < n - 1 ; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (strcmp(name[i], name[j]) > 0)
            {
                strcpy(temp, name[i]);
                strcpy(name[i], name[j]);
                strcpy(name[j], temp);
            }
        }
    }
    printf("\n----------------------------------------\n");
    printf("Input NamestSorted names\n");
    printf("------------------------------------------\n");
    for (i = 0; i < n; i++)
    {
        printf("%s\t\t%s\n", tname[i], name[i]);
    }
    printf("------------------------------------------\n");
    getch();
}
 
Output
 
Enter the value of n
7
Enter 7 names
heap
stack
queue
object
class
program
project
 
----------------------------------------
Input Names    Sorted names
------------------------------------------
heap           class
stack          heap
queue          object
object         program
class          project
program        queue
project        stack
------------------------------------------

No comments:

Post a Comment