Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a program to maintain a list of number operations. You will declare a s

ID: 3812770 • Letter: 1

Question

1. Write a program to maintain a list of number operations. You will declare a struct named Number which has the following member components.

a. int firstNum

b. int secondNum

c. int sum

d. int product

Include two functions in this program. The first function will fill the struct with 2 random numbers(1- 100 inclusive) and calculate its sum and product. The second function will print a list of struct with the numbers, sum and product. The function prototypes are:

void fillStruct(Number[]); void printStruct(Number[]);

To declare this array of struct:

const int SIZE=2; Number myNumber[SIZE];

2. Write a program that maintains a bank account balance. Create a BankAccount class which has the following members:

data members: double balance

member functions:

BankAccount(); //default constructor

BankAccount(double); //initialization constructor

~BankAccount(); //destructor

double getBalance() const; // getter

void setBalance(double); //setter

void credit(double); //add deposit to balance

void debit(double); //subtract withdrawal from balance

void print() const; //display balance

Create a header file, implementation file for BankAccount class.

Create a public static data member (static int totalAccount) to keep track total objects created during testing.

Test your class in another file which contains the main() function

User prompts for starting balance, deposit, and withdrawal are not required. You can give an initial value to test your class.

Answer in c++ please.

Explanation / Answer


// C++ code
#include <iostream>
#include <stdlib.h>
using namespace std;

//structure number

struct Number {
int firstNum;
int secondNum;
int sum;
int product;
};

//fills structure with random number between 1 to 100 and sum and product them

void fillStruct(struct Number n[]) {
int i;
//to generate different random numbers on each run or call
srand(time(NULL));

for (i = 0; i < 2; i++) {
n[i].firstNum = (rand() % (100 - 1)) + 1;
n[i].secondNum = (rand() % (100 - 1)) + 1;
n[i].sum = n[i].firstNum + n[i].secondNum;
n[i].product = n[i].firstNum * n[i].secondNum;
}
}

//display the values stored in the array of struct number

void displayResult(struct Number n[]) {
int i;
cout << "First Number Second Number Sum Product ";
for (i = 0; i < 2; i++) {
cout << " " << n[i].firstNum << " " << n[i].secondNum << " " << n[i].sum << " " << n[i].product << " ";
}

}

int main() {
//create struct number object myNumber
struct Number myNumber[2];
//passes mynumber to fill it with random values
fillStruct(myNumber);
//passes mynumber to display values
displayResult(myNumber);
return (0);
}

/*
output:

First Number   Second Number   Sum   Product
   69       5   74   345
   17       69   86   1173

*/







// header file + implementation file
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
using namespace std;
class BankAccount
{

private:
double balance;
public:
BankAccount() //default constructor
{
balance = 0;
}
BankAccount(double b) //initialization constructor
{
balance = b;
}
~BankAccount() //destructor
{
cout << "Destructor called ";
}
double getBalance() const // getter
{
return balance;
}
void setBalance(double b) //setter
{
balance = b;
}
void credit(double deposit) //add deposit to balance
{
balance = balance + deposit;
cout << "Amount credited ";
}
void debit(double amount) //subtract withdrawal from balance
{
if(amount > balance)
cout << "Insufficient Funds ";
else
{
balance = balance - amount;
cout << "Amount debited ";
}
}
void print() const //display balance
{
cout << "Balance: " << getBalance() << endl;
}
};
#endif // BANKACCOUNT_H


//main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "BankAccount.h"
using namespace std;

//Main function
int main()
{
BankAccount b1;
BankAccount b2;

b1.credit(1000);
b1.debit(100);
b1.print();


b2.credit(100);
b2.debit(10);
b2.print();

return 0;
}

/*
output:

Amount credited
Amount debited
Balance: 900
Amount credited
Amount debited
Balance: 90
Destructor called
Destructor called

*/