Programming fundamentals
Digital assignment 2:
1.Write a c-program to explain structure within union and union within structure concept
Aim:
To write a c-program to explain structure within union and union within structure concept.
Programming code:
#include<stdio.h>
#include<string.h> //structure
within union struct purchase{
char product1[35];
char product2[35]; char
product3[35]; char
product4[35]; char
product5[35];
};
union items{
struct purchase p;
};
//union within structure union
details{
int mob_num; char
customer_name[50]; char
customer_address[50];
};
struct customer{
union details de;
};
void main(){
int n,i; char cname[50]; char cadd[50];
printf("\tHow many times you want to purchace?");
scanf("%d",&n); union items it[n]; struct customer c;
printf("\nPurchasing process:\t\t\t\tCustomer details:"); printf("\
nEnter the customer name:"); scanf("%s",cname);
strcpy(c.de.customer_name,cname); printf("\t\t\t\t\
tCustomer name is:%s",c.de.customer_name); printf("\nEnter
the mobile number:"); scanf("%d",&c.de.mob_num);
printf("\t\t\t\t\tMobile number is:%d",c.de.mob_num); printf("\
nEnter the address:"); scanf("%s",cadd);
strcpy(c.de.customer_address,cadd); printf("\t\t\t\t\tAddress
of the customer:%s",c.de.customer_address); printf("\n\tType
your product\n"); for(i=0;i<n;i++)
printf("Purchase time %d \nProduct 1:",i+1);
scanf("%s",it[i].p.product1);
printf("Product 2:"); scanf("%s",it[i].p.product2);
printf("Product 3:"); scanf("%s",it[i].p.product3);
printf("Product 4:"); scanf("%s",it[i].p.product4);
printf("Product 5:"); scanf("%s",it[i].p.product5);
for(i=0;i<n;i++)
printf("\n\t\t\t\t\tOrder purchased for %d time:",i+1); printf("\n\t\t\t\t\tItem is:
%s",it[i].p.product1); printf("\n\t\t\t\t\tItem is:%s",it[i].p.product2);
printf("\n\t\t\t\t\tItem is:%s",it[i].p.product3); printf("\n\t\t\t\t\tItem
is:%s",it[i].p.product4); printf("\n\t\t\t\t\tItem is:%s",it[i].p.product5);
printf("\n\t\tThank you for ordering!!!!");
}
Sample input and output:
2.How to pass an array as arguments to the function and return array from function.
Aim:
To pass an array as arguments to the function and return array from function.
Programming code:
#include<stdio.h> void
ascending(int arr[],int size){
int i,j,temp; printf("\nAfter
Function Call\n");
for(i=0;i<size;i++){
for(j=0;j<size;j++){
if(arr[i]<arr[j])
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
void main(){
int size;
printf("Enter the size of array:");
scanf("%d",&size); int i,arr[size];
printf("Enter the values:");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("Going to function:");
ascending(arr,size);
for(i=0;i<size;i++)
printf("%d ",arr[i]);
}
Sample input and output:
3.Write a c-program to explain pointer’s to structure concept.
Aim:
To write a c-program to explain pointer’s to structure concept.
Programming code:
#include <string.h> struct
Books
char title[50]; char
author[50]; char
subject[100]; int
book_id;
};
/* function declaration */ void
printBook( struct Books *book ); int
main( )
struct Books Book1; /* Declare Book1 of type Book */ struct
Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */ strcpy( Book1.title,
"Computer science"); strcpy( Book1.author,
"Balaguruswamy"); strcpy( Book1.subject, "C
Programming Tutorial");
Book1.book_id = 6495407; /* book 2
specification */ strcpy( Book2.title,
"Computer Application");
strcpy( Book2.author, "Rockford");
strcpy( Book2.subject, "Applications on PC");
Book2.book_id = 6495700;
/* print Book1 info by passing address of Book1 */ printBook(
&Book1 );
/* print Book2 info by passing address of Book2 */
printBook( &Book2 ); return 0;
void printBook( struct Books *book )
{
printf( "Book title : %s\n", book->title);
printf( "Book author : %s\n", book->author);
printf( "Book subject : %s\n", book->subject);
printf( "Book book_id : %d\n", book->book_id);
Sample input and output:
4.Write a c-program to perform matrix multiple between two 3x3 matrix.
Aim:
To write a c-program to perform matrix multiple between two 3x3 matrix.
Programming code: #include<stdio.h>
void main() { int a[10][10], b[10][10],
c[10][10], i, j, k; int sum = 0;
printf("\nEnter First Matrix : n");
for (i = 0; i < 3; i++) { for (j = 0; j
< 3; j++) { scanf("%d", &a[i]
[j]);
printf("\nEnter Second Matrix:n");
for (i = 0; i < 3; i++)
{ for (j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
printf("The First Matrix is: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", a[i][j]);
printf("\n");
printf("The Second Matrix is : \n");
for (i = 0; i < 3; i++) { for (j = 0; j <
3; j++) { printf(" %d ", b[i][j]);
}
printf("\n");
//Multiplication Logic for (i = 0;
i <= 2; i++) { for (j = 0; j <= 2; j+
+) { sum = 0; for (k = 0; k
<= 2; k++) { sum = sum + a[i]
[k] * b[k][j];
c[i][j] = sum;
printf("\nMultiplication Of Two Matrices : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", c[i][j]);
printf("\n");
Sample input and output: