Showing posts with label Array and Pointer. Show all posts
Showing posts with label Array and Pointer. Show all posts

C program to find largest number using dynamic memory allocation

PROGRAM:


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, num;
    float *data;

    printf("Enter total number of elements(1 to 100): ");
    scanf("%d", &num);

   
    data = (float*) calloc(num, sizeof(float));

    if(data == NULL)
    {
        printf("Error!!! memory not allocated.");
        exit(0);
    }

    printf("\n");

   
    for(i = 0; i < num; ++i)
    {
       printf("Enter Number %d: ", i + 1);
       scanf("%f", data + i);
    }

    for(i = 1; i < num; ++i)
    {
       
       if(*data < *(data + i))
           *data = *(data + i);
    }

    printf("Largest element = %.2f", *data);

    return 0;
}

OUTPUT:

Enter total number of elements(1 to 100): 10

Enter Number 1: 2.34
Enter Number 2: 3.43
Enter Number 3: 6.78
Enter Number 4: 2.45
Enter Number 5: 7.64
Enter Number 6: 9.05
Enter Number 7: -3.45
Enter Number 8: -9.99
Enter Number 9: 5.67
Enter Number 10: 34.95
Largest element: 34.95

C program swap numbers in cyclic order using call by reference

PROGRAM:

#include<stdio.h>
void cyclicSwap(int *a,int *b,int *c);

int main()
{
    int a, b, c;

    printf("Enter a, b and c respectively: ");
    scanf("%d %d %d",&a,&b,&c);

    printf("Value before swapping:\n");
    printf("a = %d \nb = %d \nc = %d\n",a,b,c);

    cyclicSwap(&a, &b, &c);

    printf("Value after swapping:\n");
    printf("a = %d \nb = %d \nc = %d",a, b, c);

    return 0;
}
void cyclicSwap(int *a,int *b,int *c)
{

    int temp;

    temp = *b;
    *b = *a;
    *a = *c;
    *c = temp;
}


OUTPUT:


Enter a, b and c respectively: 1
2
3
Value before swapping:
a = 1 
b = 2 
c = 3
Value after swapping:
a = 3 
b = 1 
c = 2

C program to access elements of an array using pointer

PROGRAM:

#include <stdio.h>

int main()
{
   int data[5], i;
   printf("Enter elements: ");

   for(i = 0; i < 5; ++i)
     scanf("%d", data + i);

   printf("You entered: \n");
   for(i = 0; i < 5; ++i)
      printf("%d\n", *(data + i));

   return 0;
}


OUTPUT:


Enter elements: 1
2
3
5
4
You entered: 
1
2
3
5
4

C program to multiply two matrices by passing matrix to a function

PROGRAM:

#include <stdio.h>

void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
void display(int mult[][10], int rowFirst, int columnSecond);

int main()
{
 int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k;

 printf("Enter rows and column for first matrix: ");
 scanf("%d %d", &rowFirst, &columnFirst);

 printf("Enter rows and column for second matrix: ");
 scanf("%d %d", &rowSecond, &columnSecond);

 
 while (columnFirst != rowSecond)
 {
  printf("Error! column of first matrix not equal to row of second.\n");
  printf("Enter rows and column for first matrix: ");
  scanf("%d%d", &rowFirst, &columnFirst);
  printf("Enter rows and column for second matrix: ");
  scanf("%d%d", &rowSecond, &columnSecond);
 }

 
        enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond);

        
        multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond);

       
        display(mult, rowFirst, columnSecond);

 return 0;
}

void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)
{
 int i, j;
 printf("\nEnter elements of matrix 1:\n");
 for(i = 0; i < rowFirst; ++i)
 {
  for(j = 0; j < columnFirst; ++j)
  {
   printf("Enter elements a%d%d: ", i + 1, j + 1);
   scanf("%d", &firstMatrix[i][j]);
  }
 }

 printf("\nEnter elements of matrix 2:\n");
 for(i = 0; i < rowSecond; ++i)
 {
  for(j = 0; j < columnSecond; ++j)
  {
   printf("Enter elements b%d%d: ", i + 1, j + 1);
   scanf("%d", &secondMatrix[i][j]);
  }
 }
}

void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)
{
 int i, j, k;

 
 for(i = 0; i < rowFirst; ++i)
 {
  for(j = 0; j < columnSecond; ++j)
  {
   mult[i][j] = 0;
  }
 }

 
 for(i = 0; i < rowFirst; ++i)
 {
  for(j = 0; j < columnSecond; ++j)
  {
   for(k=0; k<columnFirst; ++k)
   {
    mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
   }
  }
 }
}

void display(int mult[][10], int rowFirst, int columnSecond)
{
 int i, j;
 printf("\nOutput Matrix:\n");
 for(i = 0; i < rowFirst; ++i)
 {
  for(j = 0; j < columnSecond; ++j)
  {
   printf("%d  ", mult[i][j]);
   if(j == columnSecond - 1)
    printf("\n\n");
  }
 }
}


OUTPUT:


Enter rows and column for first matrix: 3
2
Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row of second.

Enter rows and column for first matrix: 2
3
Enter rows and column for second matrix: 3
2

Enter elements of matrix 1:
Enter elements a11: 3
Enter elements a12: -2
Enter elements a13: 5
Enter elements a21: 3
Enter elements a22: 0
Enter elements a23: 4

Enter elements of matrix 2:
Enter elements b11: 2
Enter elements b12: 3
Enter elements b21: -9
Enter elements b22: 0
Enter elements b31: 0
Enter elements b32: 4

Output Matrix:
24  29

6  25

C program to find transpose of a matrix

PROGRAM:

#include <stdio.h>

int main()
{
    int a[10][10], transpose[10][10], r, c, i, j;
    printf("Enter rows and columns of matrix: ");
    scanf("%d %d", &r, &c);

    
    printf("\nEnter elements of matrix:\n");
    for(i=0; i<r; ++i)
        for(j=0; j<c; ++j)
        {
            printf("Enter element a%d%d: ",i+1, j+1);
            scanf("%d", &a[i][j]);
        }

    
    printf("\nEntered Matrix: \n");
    for(i=0; i<r; ++i)
        for(j=0; j<c; ++j)
        {
            printf("%d  ", a[i][j]);
            if (j == c-1)
                printf("\n\n");
        }

   
    for(i=0; i<r; ++i)
        for(j=0; j<c; ++j)
        {
            transpose[j][i] = a[i][j];
        }

    
    printf("\nTranspose of Matrix:\n");
    for(i=0; i<c; ++i)
        for(j=0; j<r; ++j)
        {
            printf("%d  ",transpose[i][j]);
            if(j==r-1)
                printf("\n\n");
        }

    return 0;
}


OUTPUT:


Enter rows and columns of matrix: 2
3

Enter element of matrix:
Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 6
Enter element a23: 4

Entered Matrix: 
2  3  4  

5  6  4  


Transpose of Matrix:
2  5  

3  6  

4  4  

C program to multiply two matrix using multi-dimensional arrays

PROGRAM:

#include <stdio.h>

int main()
{
    int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;

    printf("Enter rows and column for first matrix: ");
    scanf("%d %d", &r1, &c1);

    printf("Enter rows and column for second matrix: ");
    scanf("%d %d",&r2, &c2);

    while (c1 != r2)
    {
        printf("Error! column of first matrix not equal to row of second.\n\n");
        printf("Enter rows and column for first matrix: ");
        scanf("%d %d", &r1, &c1);
        printf("Enter rows and column for second matrix: ");
        scanf("%d %d",&r2, &c2);
    }

   
    printf("\nEnter elements of matrix 1:\n");
    for(i=0; i<r1; ++i)
        for(j=0; j<c1; ++j)
        {
            printf("Enter elements a%d%d: ",i+1, j+1);
            scanf("%d", &a[i][j]);
        }

    printf("\nEnter elements of matrix 2:\n");
    for(i=0; i<r2; ++i)
        for(j=0; j<c2; ++j)
        {
            printf("Enter elements b%d%d: ",i+1, j+1);
            scanf("%d",&b[i][j]);
        }

    
    for(i=0; i<r1; ++i)
        for(j=0; j<c2; ++j)
        {
            result[i][j] = 0;
        }

    
    for(i=0; i<r1; ++i)
        for(j=0; j<c2; ++j)
            for(k=0; k<c1; ++k)
            {
                result[i][j]+=a[i][k]*b[k][j];
            }

    printf("\nOutput Matrix:\n");
    for(i=0; i<r1; ++i)
        for(j=0; j<c2; ++j)
        {
            printf("%d  ", result[i][j]);
            if(j == c2-1)
                printf("\n\n");
        }
    return 0;
}


OUTPUT:


Enter rows and column for first matrix: 3
2
Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row of second.

Enter rows and column for first matrix: 2
3
Enter rows and column for second matrix: 3
2

Enter elements of matrix 1:
Enter elements a11: 3
Enter elements a12: -2
Enter elements a13: 5
Enter elements a21: 3
Enter elements a22: 0
Enter elements a23: 4

Enter elements of matrix 2:
Enter elements b11: 2
Enter elements b12: 3
Enter elements b21: -9
Enter elements b22: 0
Enter elements b31: 0
Enter elements b32: 4

Output Matrix:
24  29

6  25