Something You Want to Know About Arrays
Would you like to react to this message? Create an account in a few clicks or log in to continue.

How an Array Can Replace Nested Decisions?

+12
Jericho
Myra Monique Acuna
Pox
Lawrence
judedominguiano
airojames
Emie Ligutan
Sean
justjoking10
Sean Monacillo
LouiseSebastianeYagi
iramae
16 posters
Go down
iramae
iramae
Admin
Posts : 139
Join date : 2018-07-19
Age : 24
https://iramaecomp.forumotion.com

How an Array Can Replace Nested Decisions? Empty How an Array Can Replace Nested Decisions?

Sat Jul 28, 2018 3:34 pm
Consider an application requested by a company’s human resources department to produce statistics on employees’ claimed dependents. The department wants a report that lists the number of employees who have claimed 0, 1, 2, 3, 4, or 5 dependents. (Assume that you know that no employees have more than five dependents.) For example




How an Array Can Replace Nested Decisions? 210




The true benefit of using an array lies in your ability to use a variable as a subscript to the array, instead of using a literal constant such as 0 or 5. Notice in the logic in figure A that within each decision, the value compared to dep and the constant that is the subscript in the resulting Yes process are always identical. That is, when dep is 0, the subscript used to add 1 to the counts array is 0; when dep is 1, the subscript used for the counts array is 1, and so on. Therefore, you can just use dep as a subscript to the array. You can rewrite the decision-making process as shown in Figure A.




How an Array Can Replace Nested Decisions? Figure11


Figure A





However, notice the shaded statements in Figure A—the process that occurs after each decision is exactly the same. In each case, no matter what the value of dep is, you always add 1 to counts [dep] . If you always will take the same action no matter what the answer to a question is, there is no need to ask the question. Instead, you can rewrite the decision-making process as shown in Figure B.





How an Array Can Replace Nested Decisions? Figure12


Figure B





The single statement in Figure B eliminates the entire decision-making process that was the original highlighted section in Figure A! When dep is 2, 1 is added to counts[2] ; when dep is 4, 1 is added to counts[4] , and so on. Now you have significantly improved the original logic. What’s more, this process does not change whether there are 20, 30, or any other number of possible categories. To use more than five accumulators, you would declare additional counts elements in the array, but the categorizing logic would remain the same as it is in Figure B.

Figure C shows an entire program that takes advantage of the array to produce the report that shows counts for dependent categories. Variables and constants are declared and, in the getReady() module, a first value for dep is entered into the program. In the countDependents() module, 1 is added to the appropriate element of the count array and the next value is input. The loop in the mainline logic in Figure C is an indefinite loop; it continues as long as the user does not enter the sentinel value. When data entry is complete, the finishUp() module displays the report. First, the heading is output, then dep is reset to 0, and then each dep and counts[dep] are output in a loop. The first output statement contains 0 (as the number of dependents) and the value stored in counts[0] . Then, 1 is added to dep and the same set of instructions is used again to display the counts for each number of dependents. The loop in the finishUp() module is a definite loop; it executes precisely six times.





How an Array Can Replace Nested Decisions? Figure13


Figure C




How an Array Can Replace Nested Decisions? Figure14


Figure C.1






The dependent-counting program would have worked if it contained a long series of decisions and output statements, but the program is easier to write when you use an array and access its values using the number of dependents as a subscript. Additionally, the new program is more efficient, easier for other programmers to understand, and easier to maintain. Arrays are never mandatory, but often they can drastically cut down on your programming time and make your logic easier to understand. Learning to use arrays properly can make many programming tasks far more efficient and professional. When you understand how to use arrays, you will be able to provide elegant solutions to problems that otherwise would require tedious programming steps.


Last edited by iramae on Thu Aug 02, 2018 5:14 pm; edited 2 times in total
LouiseSebastianeYagi
LouiseSebastianeYagi
Expert
Posts : 29
Join date : 2018-08-02
Age : 24

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 4:50 pm
Ma’am, based po dun sa problem na ibinigay about sa time, applicable padin po ba ang array dun imbis na gamitin yung nested loop?

Sent from Topic'it App
iramae
iramae
Admin
Posts : 139
Join date : 2018-07-19
Age : 24
https://iramaecomp.forumotion.com

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 4:52 pm
LouiseSebastianeYagi wrote:Ma’am, based po dun sa problem na ibinigay about sa time, applicable padin po ba ang array dun imbis na gamitin yung nested loop?

Sent from Topic'it App

Aling time yung tinutukoy mo, yung naging activity ba last time?
Yes, applicable siya sa time na problem, pero kung yun yung gagawin mo using arrays, maraming pwedeng paraan paano mo siya magagawa.

LouiseSebastianeYagi
LouiseSebastianeYagi
Expert
Posts : 29
Join date : 2018-08-02
Age : 24

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 6:37 pm
So bale po ma’am since 24 hours yung binigay, 25 po yung arrays namin na ilaagay sa declaration?

Sent from Topic'it App
iramae
iramae
Admin
Posts : 139
Join date : 2018-07-19
Age : 24
https://iramaecomp.forumotion.com

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 7:04 pm
iramae wrote:
LouiseSebastianeYagi wrote:So bale po ma’am since 24 hours yung binigay, 25 po yung arrays namin na ilaagay sa declaration?

Sent from Topic'it App

Uhm, para mas clear, wag mo nalang isa-isahin yung hours mo kase hahaba program mo, pwede mong gawin yung ganto

num time[3]=0,0,0

yung 0 na una, para sa hours mo, yung pangalawa na 0 para sa minutes mo, yung pangatlong 0 yung seconds mo
then kapag nag condition kana yung maka-count mo, store mo siya sa array element mo para kapag nag output kana, tatawagin mo na lng yung array element ng hours, minutes and seconds. Tapos kapag 60 na yung seconds mo, tsaka ka lang magcount ng isa sa minutes mo then mag initialize ka nlng ulit na 0 para sa seconds mo.

sa output mo, pwedeng magiging ganto,
output time[0]," : ", time[1], " : ", time[2], ang lalabas jan ay 0:0:0 sa una habang wala pang process na nagagawa


LouiseSebastianeYagi
LouiseSebastianeYagi
Expert
Posts : 29
Join date : 2018-08-02
Age : 24

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 7:11 pm
Bale sa pseudo-coding po niya ma’am same process lang ang gagawin ko?

Sent from Topic'it App
iramae
iramae
Admin
Posts : 139
Join date : 2018-07-19
Age : 24
https://iramaecomp.forumotion.com

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 7:24 pm
LouiseSebastianeYagi wrote:Bale sa pseudo-coding po niya ma’am same process lang ang gagawin ko?

Sent from Topic'it App

Kung gagamitin mo yung binigay ko na array, yes same process lang din. Basta wag lang kakalimutan na mag initialize ng 0 ulit. Once na 60 na yung seconds, count ka ng isa sa minute mo then initialize mo ulit yung seconds mo sa zero. Kapag 60 na yung minutes mo, count ka ng isa sa hours tapos initialize ka ulit sa minute mo ng 0. Tapos ganung process lang din.
LouiseSebastianeYagi
LouiseSebastianeYagi
Expert
Posts : 29
Join date : 2018-08-02
Age : 24

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 7:27 pm
Eh ma’am what about po dun sa pseudocode ng FOR Loop na sinasabi niyo po ma’am na 0=i to 999-1 step 1 po?

Sent from Topic'it App
iramae
iramae
Admin
Posts : 139
Join date : 2018-07-19
Age : 24
https://iramaecomp.forumotion.com

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 7:45 pm
LouiseSebastianeYagi wrote:Eh ma’am what about po dun sa pseudocode ng FOR Loop na sinasabi niyo po ma’am na 0=i to 999-1 step 1 po?

Sent from Topic'it App

Ah XD yung pala gusto ko ipaliwanag pa sa forum ko pero sige explain ko na. Baliktad yung i at 0 ko jan.
Halimbawa
num SIZE= 998
num count[998]={0}

para macheck mo lang kung wala ba talagang laman yung array element mo, gagamit ka ng for loop.
for i = 0 to SIZE - 1 step 1

yung i = 0, nag-initialize ka ng 0 muna, ibig sabihin, eto yung magiging laman ng bawat array element na dadaanan mo
yung SIZE - 1, eto yung array element na ichecheck mo kung zero ba siya o hindi, bale ito yung index ng array mo
yung step 1, kapag nagtrue kana, another loop ulit to check hanggang sa matapos ka sa zero na array element


Last edited by iramae on Thu Aug 02, 2018 7:57 pm; edited 1 time in total
LouiseSebastianeYagi
LouiseSebastianeYagi
Expert
Posts : 29
Join date : 2018-08-02
Age : 24

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 7:56 pm
Hindi naman po kailangan na i-check po talaga lahat diba? Kasi kung for instance po na 998 nga po yung array, possible naman po na magtrue lahat kung i=0 po diba?

Sent from Topic'it App
iramae
iramae
Admin
Posts : 139
Join date : 2018-07-19
Age : 24
https://iramaecomp.forumotion.com

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 7:59 pm
LouiseSebastianeYagi wrote:Hindi naman po kailangan na i-check po talaga lahat diba? Kasi kung for instance po na 998 nga po yung array, possible naman po na magtrue lahat kung i=0 po diba?

Sent from Topic'it App

may binago ako sa explanation sa taas, paki check nalng ulit kase mali yung natype ko, sorry for typo, mahirap sa phone magtype ee
LouiseSebastianeYagi
LouiseSebastianeYagi
Expert
Posts : 29
Join date : 2018-08-02
Age : 24

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 8:04 pm
Okay lang po ma’am. I-checheck ko po.

Sent from Topic'it App
iramae
iramae
Admin
Posts : 139
Join date : 2018-07-19
Age : 24
https://iramaecomp.forumotion.com

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 8:23 pm
LouiseSebastianeYagi wrote:Okay lang po ma’am. I-checheck ko po.

Sent from Topic'it App

okay sige sige salamat Smile
Sean Monacillo
Sean Monacillo
Journeyman
Posts : 7
Join date : 2018-08-02

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 9:10 pm
LouiseSebastianeYagi wrote:Ma’am, based po dun sa problem na ibinigay about sa time, applicable padin po ba ang array dun imbis na gamitin yung nested loop?

I think applicable naman ata pre

Sent from Topic'it App
Sean Monacillo
Sean Monacillo
Journeyman
Posts : 7
Join date : 2018-08-02

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 9:11 pm
LouiseSebastianeYagi wrote:So bale po ma’am since 24 hours yung binigay, 25 po yung arrays namin na ilaagay sa declaration?

hindi pre . isang array lang ata kailangan consisting of 3 elements pre . Sa pagkaka alam ko .

Sent from Topic'it App
Sean Monacillo
Sean Monacillo
Journeyman
Posts : 7
Join date : 2018-08-02

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 9:13 pm
LouiseSebastianeYagi wrote:Hindi naman po kailangan na i-check po talaga lahat diba? Kasi kung for instance po na 998 nga po yung array, possible naman po na magtrue lahat kung i=0 po diba?

baka yung 998 pre element siya sa isang array pre .

Sent from Topic'it App
avatar
justjoking10
Expert
Posts : 13
Join date : 2018-08-02

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 9:28 pm
Maam. Is Array a type of list or database or both?
avatar
justjoking10
Expert
Posts : 13
Join date : 2018-08-02

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 9:30 pm
Nasagot n po pala ung tungkol s list. Sorry po.
LouiseSebastianeYagi
LouiseSebastianeYagi
Expert
Posts : 29
Join date : 2018-08-02
Age : 24

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 9:40 pm
Hindi Sean. Yung 998 is yung SIZE ng array eh.

Sent from Topic'it App
avatar
justjoking10
Expert
Posts : 13
Join date : 2018-08-02

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Thu Aug 02, 2018 9:40 pm
If may array po maam na
Array[3] = (price1, price2, price3)
then Array[0] = price1
variable po b ung Array[0]?
Sean
Sean
Expert
Posts : 16
Join date : 2018-08-02

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Fri Aug 03, 2018 5:56 pm
LouiseSebastianeYagi wrote:Hindi Sean. Yung 998 is yung SIZE ng array eh.

kaya nga . yung 998 yung element pre sa loob ng Array .

Sent from Topic'it App
Sean
Sean
Expert
Posts : 16
Join date : 2018-08-02

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Fri Aug 03, 2018 5:57 pm
justjoking10 wrote:If may array po maam na
Array[3] = (price1, price2, price3)
then Array[0] = price1
variable po b ung Array[0]?
Alam ko pre dapat Array[1] = price1 dapat yan pre kasi diba dapat mas malaki yung number size ng array kesa sa element na nasa loob ng array

Sent from Topic'it App
iramae
iramae
Admin
Posts : 139
Join date : 2018-07-19
Age : 24
https://iramaecomp.forumotion.com

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Fri Aug 03, 2018 7:26 pm
justjoking10 wrote:If may array po maam na
Array[3] = (price1, price2, price3)
then Array[0] = price1
variable po b ung Array[0]?

Yes, variable din ang tawag sa kanya, Element lang ang tawag sa kanya kapag parts ng array yung usapan
iramae
iramae
Admin
Posts : 139
Join date : 2018-07-19
Age : 24
https://iramaecomp.forumotion.com

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Fri Aug 03, 2018 7:41 pm
Sean wrote:
justjoking10 wrote:If may array po maam na
Array[3] = (price1, price2, price3)
then Array[0] = price1
variable po b ung Array[0]?
Alam ko pre dapat Array[1] = price1 dapat yan pre kasi diba dapat mas malaki yung number size ng array kesa sa element na nasa loob ng array

Sent from Topic'it App

Ayy lilinawin ko ah,
Array [3]=price1,price2,price3
Array[0]=price1

bakit? Kase sa Array[3] , yung 3 jan, ayan yung bilang kung ilan ang laman ng array mo, sa normal na pagbibilang, ibig sabihin magbibilang ka simula 1. Kaya naging Array[0] yung price one kase di ba yung element ng array mo, nasa memory, kapag memory, magbibilang ka mula 0 kaya naging Array[0] yung price1
LouiseSebastianeYagi
LouiseSebastianeYagi
Expert
Posts : 29
Join date : 2018-08-02
Age : 24

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Fri Aug 03, 2018 7:45 pm
Ma’am eh di ang mangyayari lang po diyan ay hanggang Array[2] kasi po ma’am hanggang price3 lang po siya eh?

Sent from Topic'it App
Sponsored content

How an Array Can Replace Nested Decisions? Empty Re: How an Array Can Replace Nested Decisions?

Back to top
Permissions in this forum:
You cannot reply to topics in this forum