Showing posts with label Arrays. Show all posts
Showing posts with label Arrays. Show all posts

Saturday, April 9, 2022

C program to create an array of student names

This program is based on C programming String and an Array concept.


Problem definition: Write a C program to create an array of student names.

//C Program to create an array of student names

#include<stdio.h>

int main(){
	char name[5][20]; 
	//This can store 5 names; maximum 20 characters in each name
	
	int i;
	//Logic to Read name of 5 students
	for(i=0; i<5; i++){
		printf("Enter name %d:", i+1);
		gets(name[i]);
	}
	printf("List of student names stored: \n");
	//Logic to print name of 5 students
	for(i=0; i<5; i++){
		printf("%s \n", name[i]);
	}
	return 0;
}

Output of the program

Enter name 1:Amit
Enter name 2:Sunit
Enter name 3:Kamlesh
Enter name 4:Bhargav
Enter name 5:Alpesh

List of student names stored:
Amit
Sunit
Kamlesh
Bhargav
Alpesh

* * *





Sunday, August 1, 2021

C Program to perform Matrix Subtraction

Matrix Subtraction

We will write a C program to perform matrix subtraction operation.

Key points for subtraction of matrices

  • Two matrices should be of same order (number of rows=number of columns).
  • Subtract the corresponding element of matrices.

Example: Subtraction of matrix of order 3 * 3

Subtraction of Matrix (A-B) is as under:

A - B = C


C Program to perform Matrix Subtraction

Program Code:

#include<stdio.h>
int main()
{
 int A[3][3],B[3][3],C[3][3],i, j;
 //Loops for Reading A matrix
 for ( i = 0; i < 3; i++ ) //related to row
 {
   for ( j = 0; j < 3; j++ ) //related to column
   {
     printf("Matrix A[%d][%d]:",i,j);
     scanf("%d", &A[i][j]) ;
   }   
 }
 //Loops for Reading B matrix
 for ( i = 0; i < 3; i++ ) //related to row
 {
   for ( j = 0; j < 3; j++ ) //related to column
   {
     printf("Matrix B[%d][%d]:",i,j);
     scanf("%d", &B[i][j]) ;
   }   
 }
 printf("Matrix A is as under: \n") ;
 /* Print Matrix A, B */
 for ( i = 0; i < 3; i++ ) //related to row
 {
   for ( j = 0; j < 3; j++ ) //related to column
   {
     printf(" %d\t ",A[i][j] );
   }
   printf("\n");
 }
 printf("Matrix B is as under: \n") ;
 for ( i = 0; i < 3; i++ ) //related to row
 {
   for ( j = 0; j < 3; j++ ) //related to column
   {
     printf(" %d\t ",B[i][j] );
   }
   printf("\n");
 }
 printf("Matrix Subtraction is as under: \n");
 for ( i = 0; i < 3; i++ ) //related to row
 {
   for ( j = 0; j < 3; j++ ) //related to column
   {
     printf(" %d\t ",A[i][j] - B[i][j] );
   }
   printf("\n");
 }
 return 0;
}

Program Output:

Matrix A[0][0]:4
Matrix A[0][1]:5
Matrix A[0][2]:6
Matrix A[1][0]:7
Matrix A[1][1]:8
Matrix A[1][2]:9
Matrix A[2][0]:10
Matrix A[2][1]:11
Matrix A[2][2]:12
Matrix B[0][0]:1
Matrix B[0][1]:3
Matrix B[0][2]:2
Matrix B[1][0]:5
Matrix B[1][1]:4
Matrix B[1][2]:3
Matrix B[2][0]:2
Matrix B[2][1]:3
Matrix B[2][2]:6
Matrix A is as under:
 4        5       6
 7        8       9
 10       11      12
Matrix B is as under:
 1        3       2
 5        4       3
 2        3       6
Matrix Subtraction is as under:
 3        2       4
 2        4       6
 8        8       6

* * * * *

* * * * *








Tuesday, April 10, 2018

Best Life Insurance Plan


Program to compare best life insurance plan using an array.

Assumption: Best life insurance company plan is one which offers more risk cover for the specified premium.

Other parameters are also important to find best life insurance plan, but in this program only one parameter(risk cover) is considered.

#include<stdio.h>

int main()
{
 int riskcover[5], i, max, ans=1;
 char insurance_company[5][30];

 printf("Enter Risk Cover offered for Rs. 1000 premium by Each life insurance company.\n\n");

 for(i=0;i<5;i++) {
printf("Enter Name of Life Insurance Company- %d:",i+1);
scanf("%s",insurance_company[i]);
printf("Enter Risk Covered by Policy- %d:",i+1);
scanf("%d",&riskcover[i]);
 }
 //Logic to find Best Life Insurance Company.
 max=riskcover[0];
 for(i=0;i<5;i++) {
if(riskcover[i] >= max)
{
  max=riskcover[i];
  ans=i;
}
 }

 printf("Best Insurance Plan is offered by company:%s", insurance_company[ans]);
}

Output of program

Enter Risk Cover offered for Rs. 1000 premium by Each life insurance company.

Enter Name of Life Insurance Company- 1: AAA
Enter Risk Covered by Policy- 1:5000
Enter Name of Life Insurance Company- 2: BBB
Enter Risk Covered by Policy- 2:10000
Enter Name of Life Insurance Company- 3: CCC
Enter Risk Covered by Policy- 3:15000
Enter Name of Life Insurance Company- 4: DDD
Enter Risk Covered by Policy- 4:8000
Enter Name of Life Insurance Company- 5: EEE
Enter Risk Covered by Policy- 5:5000
Best Insurance Plan is offered by company: CCC

Friday, April 28, 2017

Program to delete duplicate array elements

#include<stdio.h>

int main()
{
   int arrIn[20], arrOut[20];
   int i, j, k, size, temp,cnt=0, flag=0;

   printf("\nEnter array size(Max 20) : ");
   scanf("%d", &size);

   printf("\nEnter %d Numbers : ", size);
   for (i = 0; i < size; i++)
      scanf("%d", &arrIn[i]);

   printf("\nYour array elements:");
 
   for (i = 0; i < size; i++)
      printf("%d ", arrIn[i]);
     
   //Logic to delete duplicate array elements
   for (i=0; i < size; i++)
   {
     temp = arrIn[i];
     flag = 0;
      for (j=i+1; j<size; j++)
 {
         if (arrIn[j] == temp )
         {
          flag=1; cnt++;
break;
         }
         else
            flag=0;
      }
      if(flag==0)
      arrOut[k++]=temp;
   }
   printf("\n\nArray with unique elements:\n");
   for (i=0; i<size-cnt; i++)
   {
      printf("%d ", arrOut[i]);
   }
   return (0);
}

Output of program

Enter array size(Max 20) : 10

Enter 10 Numbers : 1 2 2 3 3 4 5 5 6 7

Your array elements:1 2 2 3 3 4 5 5 6 7

Array with unique elements:
1 2 3 4 5 6 7


Wednesday, April 12, 2017

Array of Structure

This program will demonstrate Array of Structure.

#include <stdio.h>
#include <string.h>

struct Book{
   int   book_id;
   char  book_title[30];
   char  book_publisher[30];
   char  book_subject[30];
};

int main( ) {

// Declare 3 variables of Book structure

 struct Book myBook[3];  
int i;

//Initialise values to myBook variable
for(i=0; i<3; i++)
{
printf("\nEnter Details of Book%d:\n",i+1);
printf("ID:");
scanf("%d",&myBook[i].book_id);

printf("Title:");
  scanf("%s",myBook[i].book_title);
  
  printf("Publisher Name:");
    scanf("%s",myBook[i].book_publisher);
  
    printf("Subject:");
    scanf("%s", myBook[i].book_subject);
}
   
   /* print myBook details */
   for(i=0; i<3; i++)
   {
    printf("\nDetails of Book%d:\n",i+1);
    printf( "ID: %d\n", myBook[i].book_id);
    printf( "Title: %s\n", myBook[i].book_title);
    printf( "Publisher Name : %s\n", myBook[i].book_publisher);
    printf( "Subject : %s\n", myBook[i].book_subject);
    printf("\n");
   }
   return 0;
}

Output of program


Enter Details of Book1:
ID:101
Title:Math-1
Publisher Name:ABC
Subject:Mathematics

Enter Details of Book2:
ID:102
Title:DataScience
Publisher Name:XYZ
Subject:Science

Enter Details of Book3:
ID:103
Title:Security
Publisher Name:ABC
Subject:Cryptography

Details of Book1:
ID: 101
Title: Math-1
Publisher Name : ABC
Subject : Mathematics


Details of Book2:
ID: 102
Title: DataScience
Publisher Name : XYZ
Subject : Science


Details of Book3:
ID: 103
Title: Security
Publisher Name : ABC
Subject : Cryptography


Thursday, February 2, 2017

Merge Array


Read two one dimensional arrays of five elements. Merge both the arrays into third array and display the same.


#include<stdio.h>
int main()
{
int i,count=0, arr1[5], arr2[5], arr3[10];
//Read array1..
for(i=0; i<5; i++)
{
printf("Enter arr1[%d]: ",i);
scanf("%d",&arr1[i]);
}
printf("\n");
//Read array2..
for(i=0; i<5; i++)
{
printf("Enter arr2[%d]: ",i);
scanf("%d",&arr2[i]);
}
//Copy arr1 into arr3...
for(i=0; i<5; i++)
{
arr3[i] = arr1[i];
}
//Append arr2 into arr1..
for(i=0;i<5;i++)
{
arr3[i+5]=arr2[i];
}
printf("Elements in Array 3 are :\n");
for(i=0;i<10;i++)
{
printf("Arr3[%d] = %d \n", i, arr3[i]);
}
}

Output of the program:

Enter arr1[0]: 1
Enter arr1[1]: 2
Enter arr1[2]: 3
Enter arr1[3]: 4
Enter arr1[4]: 5

Enter arr2[0]: 6
Enter arr2[1]: 7
Enter arr2[2]: 8
Enter arr2[3]: 9
Enter arr2[4]: 10
Elements in Array 3 are :
Arr3[0] = 1
Arr3[1] = 2
Arr3[2] = 3
Arr3[3] = 4
Arr3[4] = 5
Arr3[5] = 6
Arr3[6] = 7
Arr3[7] = 8
Arr3[8] = 9
Arr3[9] = 10

* * * * *

Average of Array Elements

Find average of Array Elements.

Example: This program will find avg. of height of five students.



#include<stdio.h>
int main()
{
int i;
float avg_height,sum=0,height[5];
//Logic to read height of 5 students
printf("Enter height in foot.\n");
for(i=0; i<5; i++){
printf("Height of student%d:",i+1);
scanf("%f", &height[i]);
}
for(i=0; i<5; i++){
sum = sum + height[i];
}
avg_height = sum/5;
printf("Average height of 5 students = %.2f",avg_height);
return 0;
}

Output of program

Enter height in foot.
Height of student1:5.5
Height of student2:5.9
Height of student3:5.3
Height of student4:6.2
Height of student5:5.4
Average height of 5 students = 5.66

* * * * *

Wednesday, February 1, 2017

Transpose Matrix

C program to obtain Transpose of a given 3X3 Matrix.


#include<stdio.h>
int main()
{
 int arr[3][3], arrTrans[3][3];
 int i,j;
 //Logic for reading matrix from user...
 printf("Enter the 3x3 matrix:\n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("Enter the element arr[%d][%d] : ",i,j);
   scanf("%d",&arr[i][j]);
  }
 }

 printf("The Original matrix is: \n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("\t%d",arr[i][j]);
  }
  printf("\n");
 }

 printf("Press any key to print Transposed Matrix..\n");
 getch();
//Logic for transposing matrix...
 for(i=0;i<3;i++) {
  for(j=0;j<3;j++)
     arrTrans[j][i] = arr[i][j];
 }
 //Logic for printing Transposed matrix...
 printf("The transpose of the matrix is: \n");
 for(i=0;i<3;i++) {
  for(j=0;j<3;j++)
     printf("\t%d",arrTrans[i][j]);
  printf("\n");
 }
 return 0;
}

Output of program

Enter the 3x3 matrix:
Enter the element arr[0][0] : 1
Enter the element arr[0][1] : 2
Enter the element arr[0][2] : 3
Enter the element arr[1][0] : 4
Enter the element arr[1][1] : 5
Enter the element arr[1][2] : 6
Enter the element arr[2][0] : 7
Enter the element arr[2][1] : 8
Enter the element arr[2][2] : 9
The Original matrix is:
        1       2       3
        4       5       6
        7       8       9
Press any key to print Transposed Matrix..
The transpose of the matrix is:
        1       4       7
        2       5       8
        3       6       9

*/

2D Array Search

C program to search an element in to 2D Array.




#include<stdio.h>
int main()
{
int A[3][3], i, j, number, flag=0;
for (i=0; i<3; i++) {
for(j=0; j<3; j++)
{
printf("Enter your element[%d][%d]:",i,j);
scanf("%d", &A[i][j]);
}
}
printf("Your array is:\n");
for (i=0; i<3; i++) {
for(j=0; j<3; j++)
printf("%d ",A[i][j]);
printf("\n");
}
printf("Enter the value to be search:");
scanf("%d",&number);

for (i=0; i<3; i++) {
for(j=0; j<3; j++)
{
if(A[i][j]==number)
{
flag=1;
break;//exit(0);
}
}
}
if (flag==1)
printf("%d: Present in Array.",number);
else
printf("%d: Not in Array", number);
return 0;
}

Output - 1

Enter your element[0][0]:1
Enter your element[0][1]:2
Enter your element[0][2]:3
Enter your element[1][0]:4
Enter your element[1][1]:5
Enter your element[1][2]:6
Enter your element[2][0]:7
Enter your element[2][1]:8
Enter your element[2][2]:9
Your array is:
1 2 3
4 5 6
7 8 9
Enter the value to be search:5
5: Present in Array.

Output - 2

Enter your element[0][0]:1
Enter your element[0][1]:2
Enter your element[0][2]:3
Enter your element[1][0]:4
Enter your element[1][1]:5
Enter your element[1][2]:6
Enter your element[2][0]:7
Enter your element[2][1]:8
Enter your element[2][2]:9
Your array is:
1 2 3
4 5 6
7 8 9
Enter the value to be search:0
0: Not in Array


* * * * *

Saturday, September 24, 2016

Use of pointer to access the Array elements.



Use of pointer to access the Array elements.

#include <stdio.h>

int main ()
{
       int marks[] = {60, 70, 80, 90, 100};
       int i, *p1;
       /* let us use pointer to refer array elements */
       p1 = marks;
       for ( i = 0; i < 5; i++)
       {
              printf("Value stored at marks[%d] = %d\n", i, *p1 );
              p1++;
       }
       return 0;
}



Output of the Program:


Value stored at marks[0] = 60
Value stored at marks[1] = 70
Value stored at marks[2] = 80
Value stored at marks[3] = 90
Value stored at marks[4] = 100

Thursday, March 10, 2016

C program to print array elements in reverse order.

Printing Array Elements in Reverse Order

#include<stdio.h>
int main()
{
 int i, arr1[5];
 for(i=0; i<5; i++)
 {
  printf("Enter Element[%d]:", i);
  scanf("%d",&arr1[i]);
 }
 printf("\n");
 printf("Array elements in reverse order are:\n");
 for(i=4; i>=0; i--)
 {
  printf("Element[%d]: %d \n", i,arr1[i]);
 }
 return 0;
}

Output of program

Enter Element[0]:1
Enter Element[1]:2
Enter Element[2]:3
Enter Element[3]:4
Enter Element[4]:5

Array elements in reverse order are:
Element[4]: 5
Element[3]: 4
Element[2]: 3
Element[1]: 2
Element[0]: 1

Saturday, February 6, 2016

Matrix Addition using C program

#include<stdio.h>
int main()
{
int a[3][3],b[3][3],c[3][3],i, j;
 //Loops for Reading A matrix
for ( i = 0; i < 3; i++ ) //related to row
{
for ( j = 0; j < 3; j++ ) //related to column
{
printf("Matrix A[%d][%d]:",i,j);
scanf("%d", &a[i][j]) ;
}
}
 //Loops for Reading B matrix
for ( i = 0; i < 3; i++ ) //related to row
{
for ( j = 0; j < 3; j++ ) //related to column
{
printf("Matrix B[%d][%d]:",i,j);
scanf("%d", &b[i][j]) ;
}
}
printf("Matrix A is as under: \n") ;
/* Print Matrix A, B */
for ( i = 0; i < 3; i++ ) //related to row
{
for ( j = 0; j < 3; j++ ) //related to column
{
printf(" %d\t ",a[i][j] );
}
printf("\n");
}
printf("Matrix B is as under: \n") ;
for ( i = 0; i < 3; i++ ) //related to row
{
for ( j = 0; j < 3; j++ ) //related to column
{
printf(" %d\t ",b[i][j] );
}
printf("\n");
}
printf("Matrix Addition is as under: \n");
for ( i = 0; i < 3; i++ ) //related to row
{
for ( j = 0; j < 3; j++ ) //related to column
{
printf(" %d\t ",a[i][j] + b[i][j] );
}
printf("\n");
}
 return 0;
}


Output of program

Matrix A[0][0]:1
Matrix A[0][1]:1
Matrix A[0][2]:1
Matrix A[1][0]:2
Matrix A[1][1]:2
Matrix A[1][2]:2
Matrix A[2][0]:3
Matrix A[2][1]:3
Matrix A[2][2]:3
Matrix B[0][0]:4
Matrix B[0][1]:4
Matrix B[0][2]:4
Matrix B[1][0]:5
Matrix B[1][1]:5
Matrix B[1][2]:5
Matrix B[2][0]:6
Matrix B[2][1]:6
Matrix B[2][2]:6
Matrix A is as under:
 1        1       1
 2        2       2
 3        3       3
Matrix B is as under:
 4        4       4
 5        5       5
 6        6       6
Matrix Addition is as under:
 5        5       5
 7        7       7
 9        9       9