0% found this document useful (0 votes)
3 views

C Assignment

The document presents a C program that defines a structure to represent a bank account, including fields for account type, holder's name, branch, account number, and balance. It includes functions to initialize an account and find the account with the maximum balance. The main function demonstrates the initialization of two accounts and identifies the one with the highest balance.

Uploaded by

sourabsourab554
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C Assignment

The document presents a C program that defines a structure to represent a bank account, including fields for account type, holder's name, branch, account number, and balance. It includes functions to initialize an account and find the account with the maximum balance. The main function demonstrates the initialization of two accounts and identifies the one with the highest balance.

Uploaded by

sourabsourab554
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

DATA STRUCTURES AND APPLIACATION (22CSE32)

SUBMITTEB
BY
USN : Dip08 NAME: HARISHA PATIL

SUBMIITED
TO
Ms Jaishma Kumari B
Assistant Professor

Computer Science and Engineering Department


St Joseph Engineering College
An Autonomous Institution
Mangalore 575028
2023-24
Title of the topics: STRUCTURE

#include <stdio.h>

#include <string.h>

// Structure to represent a bank account

struct account {

char type[10]; // Account type (savings, current)

char holder[30]; // Account holder's name

char branch[20]; // Branch name

char no[11]; // Account number (10 digits + 1 for null-terminator)

unsigned int bal; // Account balance

};

// Function to initialize an account

struct account initAcc(char* name, char* type, char* branch, char* number,
unsigned int balance) {

struct account newAc;


int isErr = 0;

// Set account holder and branch

strcpy(newAc.holder, name);

strcpy(newAc.branch, branch);

// Validate account type

if (strcmp(type, "current") == 0 || strcmp(type, "savings") == 0) {

strcpy(newAc.type, type);

} else {

isErr = 1; // Set error flag for invalid type

// Validate account number length

if (strlen(number) == 10) {

strcpy(newAc.no, number);
} else {

isErr = 1; // Set error flag for invalid account number

// Set balance

newAc.bal = balance;

// Return new account or handle error case

if (!isErr) {

return newAc;

} else {

struct account emptyAc = {0}; // Return an empty account in case of


error

return emptyAc;

}
// Function to find the account with the maximum balance

struct account findMaxBal(struct account src[], int size) {

if (size == 0) {

struct account emptyAc = {0}; // Return an empty account if no accounts

return emptyAc;

int maxBalIndex = 0;

for (int i = 1; i < size; i++) {

if (src[i].bal > src[maxBalIndex].bal) {

maxBalIndex = i;

return src[maxBalIndex];

}
// Example usage

int main() {

// Initialize two accounts

struct account ram = initAcc("Ram", "savings", "Branch1", "1234567890",


100);

struct account shyama = initAcc("Shyama", "current", "Branch2",


"0987654321", 200);

struct account accounts[] = {ram, shyama};

// Find the account with the highest balance

struct account richest = findMaxBal(accounts, 2);

// Display result

if (strcmp(richest.holder, "") != 0) { // Check if the result is a valid account

printf("Richest account holder: %s with balance: %u\n", richest.holder,


richest.bal);

} else {
printf("No valid account found.\n");

return 0;

You might also like