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
Page 1 of 4 • 1, 2, 3, 4
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
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.
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.
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.
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.
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.
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.
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.
Figure C
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.
- LouiseSebastianeYagiExpert
- Posts : 29
Join date : 2018-08-02
Age : 24
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
Sent from Topic'it App
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.
- LouiseSebastianeYagiExpert
- Posts : 29
Join date : 2018-08-02
Age : 24
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
Sent from Topic'it App
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
- LouiseSebastianeYagiExpert
- Posts : 29
Join date : 2018-08-02
Age : 24
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
Sent from Topic'it App
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.
- LouiseSebastianeYagiExpert
- Posts : 29
Join date : 2018-08-02
Age : 24
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
Sent from Topic'it App
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
- LouiseSebastianeYagiExpert
- Posts : 29
Join date : 2018-08-02
Age : 24
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
Sent from Topic'it App
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
- LouiseSebastianeYagiExpert
- Posts : 29
Join date : 2018-08-02
Age : 24
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
Sent from Topic'it App
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
- Sean MonacilloJourneyman
- Posts : 7
Join date : 2018-08-02
Re: How an Array Can Replace Nested Decisions?
Thu Aug 02, 2018 9:10 pm
I think applicable naman ata preLouiseSebastianeYagi 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
- Sean MonacilloJourneyman
- Posts : 7
Join date : 2018-08-02
Re: How an Array Can Replace Nested Decisions?
Thu Aug 02, 2018 9:11 pm
hindi pre . isang array lang ata kailangan consisting of 3 elements pre . Sa pagkaka alam ko .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
- Sean MonacilloJourneyman
- Posts : 7
Join date : 2018-08-02
Re: How an Array Can Replace Nested Decisions?
Thu Aug 02, 2018 9:13 pm
baka yung 998 pre element siya sa isang array pre .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
- justjoking10Expert
- Posts : 13
Join date : 2018-08-02
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?
- justjoking10Expert
- Posts : 13
Join date : 2018-08-02
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.
- LouiseSebastianeYagiExpert
- Posts : 29
Join date : 2018-08-02
Age : 24
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
Sent from Topic'it App
- justjoking10Expert
- Posts : 13
Join date : 2018-08-02
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]?
Array[3] = (price1, price2, price3)
then Array[0] = price1
variable po b ung Array[0]?
- SeanExpert
- Posts : 16
Join date : 2018-08-02
Re: How an Array Can Replace Nested Decisions?
Fri Aug 03, 2018 5:56 pm
kaya nga . yung 998 yung element pre sa loob ng Array .LouiseSebastianeYagi wrote:Hindi Sean. Yung 998 is yung SIZE ng array eh.
Sent from Topic'it App
- SeanExpert
- Posts : 16
Join date : 2018-08-02
Re: How an Array Can Replace Nested Decisions?
Fri Aug 03, 2018 5:57 pm
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 arrayjustjoking10 wrote:If may array po maam na
Array[3] = (price1, price2, price3)
then Array[0] = price1
variable po b ung Array[0]?
Sent from Topic'it App
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
Re: How an Array Can Replace Nested Decisions?
Fri Aug 03, 2018 7:41 pm
Sean wrote: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 arrayjustjoking10 wrote:If may array po maam na
Array[3] = (price1, price2, price3)
then Array[0] = price1
variable po b ung Array[0]?
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
- LouiseSebastianeYagiExpert
- Posts : 29
Join date : 2018-08-02
Age : 24
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
Sent from Topic'it App
Page 1 of 4 • 1, 2, 3, 4
Permissions in this forum:
You cannot reply to topics in this forum
|
|