0% found this document useful (0 votes)
35 views10 pages

U24 AI029 Ass 2

Uploaded by

shintae2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views10 pages

U24 AI029 Ass 2

Uploaded by

shintae2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Q1) Write a C program to multiply two matrices using dynamic memory allocation.

Each
two-dimensional array should be processed as array of pointers to a set of 1-
dimensional integer arrays. Read, access and display the matrix elements using pointers
instead of subscript notation. Use three functions i) To read input matrix ii) To compute
the product and iii) To display the resultant matrix.

CODE:

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

// Function to read input matrix


void readMatrix(int *matrix) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", matrix + (i * 3) + j);
}
}
}

// Function to compute the product of two matrices


void multiplyMatrices(int *matrix1, int *matrix2, int *result) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
*(result + (i * 3) + j) = 0;
for (int k = 0; k < 3; k++) {
*(result + (i * 3) + j) += *(matrix1 + (i * 3) + k) *
*(matrix2 + (k * 3) + j);
}
}
}
}

// Function to display the resultant matrix


void displayMatrix(int *matrix) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", *(matrix + (i * 3) + j));
}
printf("\n");
}
}

int main() {

// Dynamic memory allocation for matrices


int *matrix1 = (int *)malloc(3 * 3 * sizeof(int));
int *matrix2 = (int *)malloc(3 * 3 * sizeof(int));
int *result = (int *)malloc(3 * 3 * sizeof(int));

printf("Enter elements of Matrix 1:\n");


readMatrix(matrix1);

printf("Enter elements of Matrix 2:\n");


readMatrix(matrix2);

multiplyMatrices(matrix1, matrix2, result);

printf("Matrix 1:\n");
displayMatrix(matrix1);

printf("Matrix 2:\n");
displayMatrix(matrix2);

printf("Resultant Matrix:\n");
displayMatrix(result);

// Free dynamically allocated memory


free(matrix1);
free(matrix2);
free(result);

return 0;
}
OUTPUT:
Q2) Write a C program to hold two integer pointers as structure members. Allocate space
for the structure and its data members during runtime. Get one array as input. In the
second array copy the elements of the first array and replace the odd positioned
elements by the product of its adjacent elements. Access the array elements and
structures using pointers instead of subscript notation.
First Array (Input)
123456
Second Array (Output)
1 3 3 15 5 5

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

// Structure to hold two integer pointers


typedef struct {
int* arr1;
int* arr2;
} ArrayHolder;

int main() {
int n;

//size of the array


printf("Enter the size of the array: ");
scanf("%d", &n);

// Dynamically allocate memory for the structure


ArrayHolder* holder = (ArrayHolder*)malloc(sizeof(ArrayHolder));

// Dynamically allocate memory for the two arrays


holder->arr1 = (int*)malloc(n * sizeof(int));
holder->arr2 = (int*)malloc(n * sizeof(int));

// To enter first array


printf("Enter the elements of the first array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", holder->arr1 + i);
}
// Copy the elements of the first array to the second array
// and replace the odd positioned elements with the product of its
adjacent elements
for (int i = 0; i < n; i++) {
if (i % 2 != 0) {
if (i == n - 1) {
*(holder->arr2 + i) = *(holder->arr1 + i-1) *
*(holder->arr1 + n-i-1);
} else {
*(holder->arr2 + i) = *(holder->arr1 + i - 1) *
*(holder->arr1 + i + 1);
}
} else {
*(holder->arr2 + i) = *(holder->arr1 + i);
}
}

// Print the second array


printf("Second array: ");
for (int i = 0; i < n; i++) {
printf("%d ", *(holder->arr2 + i));
}
printf("\n");

// Free the dynamically allocated memory


free(holder->arr1);
free(holder->arr2);
free(holder);

return 0;
}
OUTPUT:
Q3) Create a Structure called BankMgmt with AccNumber, CustName, AvlBalance,
AccType as members. Implement a Bank management Application as menu driven
program for the above said Structure scenario.
Menu Option:
1. Withdrawal 2. Deposit 3. Display Balance 4. Exit
If option
1 is chosen- Amount can be withdrawn from the account (Withdrawn amount
should be given as input). For withdrawal the condition is- the AvlBalance must be
greater than withdrawn amount).
2 is chosen- Amount can be deposited to the account (the deposited amount
should be given as input). The deposited amount should be reflected in AvlBalance of
the account.
3 is chosen- Current available balance (AvlBalance) of the AccNumber should
be Displayed with other details

4 is chosen- Exit from the application

Sample Input:
SB 100155 VenkatKrishna 4500.00 Saving

CODE:

#include <stdio.h>

// Structure to represent a bank account


typedef struct {
char AccNumber[20];
char CustName[50];
float AvlBalance;
char AccType[20];
} BankMgmt;

// Function to display the account details


void displayDetails(BankMgmt account) {
printf("Account Number: %s\n", account.AccNumber);
printf("Customer Name: %s\n", account.CustName);
printf("Available Balance: %.2f\n", account.AvlBalance);
printf("Account Type: %s\n", account.AccType);
}

// Function to deposit money into the account


void deposit(BankMgmt *account) {
float amount;
printf("Enter the amount to deposit: ");
scanf("%f", &amount);
account->AvlBalance += amount;
printf("Deposit successful. New balance: %.2f\n",
account->AvlBalance);
}

// Function to withdraw money from the account


void withdraw(BankMgmt *account) {
float amount;
printf("Enter the amount to withdraw: ");
scanf("%f", &amount);
if (account->AvlBalance >= amount) {
account->AvlBalance -= amount;
printf("Withdrawal successful. New balance: %.2f\n",
account->AvlBalance);
} else {
printf("Insufficient balance.\n");
}
}

int main() {
BankMgmt account;
printf("Enter account number: ");
scanf("%s", account.AccNumber);
printf("Enter customer name: ");
scanf(" %[^\n]s", account.CustName);
printf("Enter available balance: ");
scanf("%f", &account.AvlBalance);
printf("Enter account type: ");
scanf("%s", account.AccType);

int choice;
do {
printf("\nBank Management Application\n");
printf("1. Withdrawal\n");
printf("2. Deposit\n");
printf("3. Display Balance\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
withdraw(&account);
break;
case 2:
deposit(&account);
break;
case 3:
displayDetails(account);
break;
case 4:
printf("Exiting the application.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);

return 0;
}

OUTPUT:

You might also like