Help Loading And Displaying Arrays with Loops

Hi I have a homework assignment that requires me to first load an array of 15 numbers with a loop. Afterwards I need to display the array I just loaded with a loop first in the ordered it was loaded then in ascending order. My teacher was sick this week for class and the instructions and examples were very unclear and I am very lost. Im just a beginner so the more details the better I can follow you. Thanks for the help!!

Edit: I understand how to display the array in the order that it was entered. Im jsut having trouble with the loading and the display in order.
I have this to display the array once it gets loaded:

While $x < 14 do
Display $A [$x]
$x++
EndWhile

Break it up into pieces and complete one task at a time. Then for + points (at least if I were the teacher) - reflect on the choices you’ve made and see if there are any pros/cons to your solution (any other possibilities to achieve the same / a better result?).

  1. load an array with a loop
  2. display the array I just loaded with a loop in the ordered it was loaded
  3. display the array I just loaded with a loop in ascending order

Please complete (at least) task 1 and post your code back here, showing effort to solve your own problem(s) make it much easier to bother helping (tbh).

Some helpful links for what you’re doing.
http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/control-structures.for.php
http://php.net/manual/en/control-structures.foreach.php
http://php.net/manual/en/control-structures.while.php

This is the code for task 2:

$x = 0
While $x < 14 do
Display $A [$x]
$x++
EndWhile

Task 1 is what Im having the most trouble with. I see how to create arrays with comma separated values but we have to load it with a while loop. I cant seem to find an example of this in either his notes or in the array notes in this site. Im assuming task 3 will be like task 2 with an if or conditional statement so Im not to worried about that but with out an array loaded I cant test anything.

You can use whichever of the loops you prefer, it doesn’t really matter which one you use.

Adding values to an array is referenced in the first link I sent
http://php.net/manual/en/language.types.array.php#language.types.array.syntax.modifying

You probably want to omit the key in this case, creating a simple list of values.

This list can later be sorted in a different order

So I need to have something like this Im pretty sure:

While my last element in my array is empty…
add a random number value to the current array slot with a 3 character limit…
Update loop to the next array slot
EndWhile

I see the examples of creating an array with an array statement but I really think he wants something like I outlined above. Im not sure of the coding to get me there however

How can I designate the last array as empty? Would null work?

Could you add the exact assignment text? You are suddenly mixing in a lot more requirements which doesn’t really add up.

I’d do something like this

for loop using an incrementor value less than a number of elements I want to push to the array
add number to array using empty brackets (does the assignment say it should be random? does it say how random it must be?)

foreach value in array
echo value

sort array values in descending order

foreach value in array
echo value

Why do you want an empty value for the last element in the array? That sounds more like a linked list than an array.

Is the homework to use php or pseudo code?

[member=71845]JimL[/member], my work up was an exact match to yours… just wondering where this is going

The assignment doesnt say but I think this will work if I can get it to run:

[php]<?php
$x = 0
While $x < 14 do
$A [$x] = int rand (0, 999)
$x++
EndWhile

$x = 0
While $x < 14 do
Display $A [$x]
$x++
EndWhile

?>[/php]

I just need to get that to display in ascending order.
Unfortuantly Im getting an error when I try to run that code but Im not sure of the error.

Here is the exact assignment:
Create a PHP program that creates an array containing 15 elements. Display the
15 elements in the order they were entered. Sort the array in ascending sequence
the display the 15 elements in ascending order

I was confusing myself. I went back to modifying the loop I knew worked and got what I have above that I think will do the job if I can get it running.

If the assignment is in PHP I’m pretty sure you should create working code.

Are you allowed to use a proper editor or is this supposed to be written in notepad? A proper editor will highlight errors in the code so you don’t have to run the code to spot obvious mistakes (like missing semi colons).

For extra learning/knowledge (and perhaps even extra points). I suggest the following:

Use curly braces around control structures (if, loops, etc).

The assignment says add 15 elements to the array, can you modify your code in any way to make it clear you want exactly 15 elements?

When reading from the array is there any way to loop over every entry without hardcoding the length of the array? atm line 9 breaks if you change line 3.

Which loop of for/foreach/while makes the code most readable (perhaps a combination)? Why?

Why should we care about readable code?

Yes this needs to be working code. Yes I should be using notepad ++ I didnt realize what I was typing into. My error seems to be at line 3 currently.
Im pretty sure that code will only create 15 elements. if x starts at 0 and the code ends when x becomes greater that 14 the I get 15 elements every time right?
I added a semicolon after both of my “do” statements.

I know there are three loops available and one might e better suited here but he wants us using while loops for this assignment. (he said that in class)

Ok, so not a proper editor then.

If you have error reporting on then just run the code and it will tell you (at least) the first error

Just run it and see ^^

Im trying to run it but I just get a FATAL ERROR. Unexpected variable at line 3. The $x apparently. This is what I have right now:
[php]<?php
$x = 0;
While $x < 14 do
$A [$x] = int rand (0,999);
$x++;
EndWhile

$x = 0;
While $x < 14 do
Display $A [$x];
$x++;
EndWhile
?>[/php]

I linked to the php manual page for while loops earlier
http://php.net/manual/en/control-structures.while.php

Your code doesn’t really reflect the correct syntax.

If you want (should) use do-while you should have a look here
http://php.net/manual/en/control-structures.do.while.php

I expect you to try navigating the php manual before asking from now.

Alright sorry I was missing brackets and such. My bad. I updated the code and my new error is with my ‘rand’ on line 4. Its saying its and unexpected string. Its probably syntax but Im not to sure? I tried removing the parenthesis and the colon but Im getting the same error.

[php]<?php
$x = 0;
While ($x < 14) do {
$A [$x] = int rand (0,999); }
$x++;
EndWhile

$x = 0;
While ($x < 14) do {
Display $A [$x]; }
$x++;
EndWhile
?>[/php]

I understand that the formatting is a little backwards compared to the manual but there should be no functional difference. I can change it over but if I added the parenthesis in the correct places I dont see why I would need to.

That isn’t working code. Where did you see that particular syntax or a while do loop?

This is just the way that the teacher taught the class. I converted it over to what was in the manual. Im not sure if I need the x++ statement or not.

[php]<?php
$x = 0;
do {
$A [$x] = rand (0,999);
} while (x < 14)

$x = 0;
do {
Display $A [$x];
} while (x < 14)

?>[/php]

[php]<?php
$x = 0;
do {
$A [$x] = rand (0,999);
} while (x < 14)
$x++;

$x = 0;
do {
Display $A [$x];
} while (x < 14)
$x++;

?>[/php]

Still getting fatal errors though

You’re missing them semi-colons after the loop.

Added them getting unexpected variable line 11 on the ‘$A’

Do I need the update statements in this syntax of the while do loop? The $x++?

Display, doesn’t mean anything. You want to look up print or echo

Sponsor our Newsletter | Privacy Policy | Terms of Service