C Arrays - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

C Arrays

Description:

if(account1.Withdraw(amount)==0) cout 'withdraw succeeds!'; else. cout 'withdraw fails. ... account1.GetBalance(); break; Default: cout 'Incorrect ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 16
Provided by: yxie
Category:
Tags: account1 | arrays

less

Transcript and Presenter's Notes

Title: C Arrays


1
C Arrays
  • Dr. Ying Xie

2
Recap last class
  • First OO program in C
  • First multiple-file program in C

3
  • //Account.h
  • ifndef ACCOUNT_H
  • define ACCOUNT_H
  • include ltstringgt
  • using namespace std
  • class Account
  • public
  • //member functions
  • Account()
  • Account(string initID, string initPIN,
    double initBalance)
  • void Deposit(double amount)
  • int Withdraw(double amount)
  • double GetBalance()
  • string GetID()
  • string GetPIN()

4
  • //Account.cpp
  • include"Account.h
  • AccountAccount()
  • balance 0
  • id ""
  • pin ""
  • AccountAccount(string initID, string initPIN,
    double initBalance)
  • id initID
  • pin initPIN
  • balance initBalance
  • void AccountDeposit(double amount)
  • balance amount
  • int AccountWithdraw(double amount)
  • if (balance gt amount)
  • balance - amount
  • return 0
  • else
  • return -1
  • double AccountGetBalance()
  • return balance
  • string AccountGetID()
  • return id

5
  • //Client.cpp
  • includeltiostreamgt
  • include "Account.h"
  • char showMenu()
  • int login(Account)
  • int main()
  • double amount
  • char request
  • Account account1("00001", "aaaaa", 600.00)
  • while (true)
  • if (login(account1) 0)
  • request showMenu()
  • while(request! 'T')
  • switch(request)
  • case 'S'
  • break

case 'D' cout
ltlt"PLease input the amount " cin gtgt
amount account1.Deposit(amount) break
case 'C' cout ltlt"\nYour current balance
is " ltlt account1.GetBalance() break
Default cout ltlt "Incorrect function
code entered. " break
//end of switch request
showMenu() //end of
while(request!T) //end of if
else cout ltlt "\n\nLogin failed, try
again! " ltlt endl //end of while //
to be continued
6
  • //to continue
  • int login(Account acc)
  • string id, pin
  • cout ltlt "\n\n"
  • cout ltlt "Please type your id " ltlt endl
  • cin gtgt id
  • cout ltlt "Please type your pin " ltlt endl
  • cin gtgt pin
  • if ((acc.GetID() id)(acc.GetPIN()
    pin))
  • return 0
  • else
  • return -1
  • char showMenu()
  • char request
  • cout ltlt "\n\n------------function
    Menu----------------"


7
  • //Client.cpp
  • includeltiostreamgt
  • include "Account.h"
  • char showMenu()
  • int login(Account)
  • int main()
  • double amount
  • char request
  • Account account1("00001", "aaaaa", 600.00)
  • while (true)
  • if (login(account1) 0)
  • request showMenu()
  • while(request! 'T')
  • switch(request)
  • case 'S'
  • break

case 'D' cout
ltlt"PLease input the amount " cin gtgt
amount account1.Deposit(amount) break
case 'C' cout ltlt"\nYour current balance
is " ltlt account1.GetBalance() break
Default cout ltlt "Incorrect function
code entered. " break
//end of switch request
showMenu() //end of
while(request!T) //end of if
else cout ltlt "\n\nLogin failed, try
again! " ltlt endl //end of while //
to be continued
8
C Arrays
  • Static data structure consisting of related data
    items of the same type.
  • Array of integer
  • Array of character
  • Array of accounts
  • Why static data structure the size of an array
    remains the same throughout the program
    execution.
  • Later on, we will introduce some of so-called
    dynamic data structure such as list, queues,
    stacks, whose size can grow and shrink as program
    executes.

9
Array consecutive memory locations which share
the same name and store the same data type.
The ith element of this array is referred to as
ci-1.
Print out the sum of the first three elements of
array c cout ltlt c0 c1 c2 ltlt endl
x c6/2
10
Use Array 1. Declare
  • int c12
  • int b100, x27
  • Account acc_array100
  • (default constructor will be called)

11
Using Arrays 2. Initialization
  • int arr110
  • for (int i0 ilt 10 i)
  • arr1i 0
  • int arr25 32, 27, 64, 18, 95
  • int arr36 0 ?

12
  • Account acc_array100
  • How to initialize this one?

13
  • accinfo.dat
  • 00001 12345 600.00
  • 00002 23456 1000.00
  • 00003 34567 200.00
  • 00004 45678 20000.00
  • 00005 56789 100.00
  • 00006 67890 30.00

14
  • includeltfstreamgt
  • includeltstringgt
  • include "Account.h"
  • using namespace std
  • int main()
  • string id, pin
  • double amount
  • Account acc_array100
  • int index -1
  • ifstream accInfo("accInfo.dat")
  • while(accInfo gtgt id gtgt pin gtgt amount)
  • index
  • Account acc(id, pin, amount)
  • acc_arrayindex acc

15
Using Arrays 3. Passing Array to a Function
  • void modifyArray(int , int)
  • int main()
  • int arr15 0,1,2,3,4
  • modifyArray(arr1, 5)
  • void modifyArray(int b, int size)
  • for (int i0 iltsize i)
  • bi bi 2
Write a Comment
User Comments (0)
About PowerShow.com