New to C# (How to use while loops to check through multiple arrays)
First, this is a school assignment however I only need help with the specific issue in the title. I will paste the instruction regarding this specific task.
"This project will contain while loops to step through the dormData Array to find the selected dorm
and its price and to step through the meal Data Array to find the selected meal plan and price."
These are my arrays, the DormCostArray correalates with the DormListArray and the same for the MealPlanArray. If I want "Allen Hall" from the DormListArray, I want to make a while loop that will run through the DormCostArray until it gets to 1500. Both of these are the first elements in their arrays, but like I said I do not know how to make a loop to make it work. I can provide the entirety of the prompt as well as my code if needed but I just do not know where to go from here and don't have much experience reaching out to people on the interenet for help
24 Replies
Have you been introduced to any loops in school / looked up any yourself?
i have but this one just seems to really get me, either im not sure how to word my question when I search it or I'm not looking at the most optimal way of using them
either way, i did find a workaround that I can't use, but might be easier in explaining what i'm trying to do
that was just to satisfy any errors and it does get the job done but I am required to use while loops in particular
idk how to make it look right with the colors and all
So do you get the premise of how whole loops work?
yeah
So without thinking specifically about the code can you think about what you need to achieve "while... do...." and the basically translate those steps into code?
im finding it really difficult to word this right
so I need to run the while loop as many times as the selected index is in the order of elements and have it stop on the same position for a separate array
Yes, so you need to search through the array until you've found the element you're looking for
Or while the element is not found, search through the array
so essentially set a while loop that actually has nothing to do with positioning and only looks for the specific value I want
You will need to do something for the position/index within the while loop, but the logic of the while loop itself is just that you want to keep running it until you've found the element you're looking for
so while the selected index is not found, have the loop check the other array until it finds the same position as the selected index in the first array
Well, once you've found the index in the first array you shouldn't need to loop anything else, you should just be able to use that index in any other arrays (unless your assignment specifically says that you have to loop every array)
and
for example
If I choose "Farthing Hall", have the loop check dgDormCostArray and iterate until it reaches 1800
both are the 3rd element
So I would say:
1) Farthing Hall is chosen
2) Loop through dgDormListArray to find Farthing Hall, save its index/position in the array to a variable
3) Use the variable from step 2 to get the element at the same position in dgDormCostArray (no loop required)
that makes a lot of sense, i didnt think of saving the index to a variable itself
You'll have a variable to store the index you're currently at when looping through dgDormListArray, so you can just use that
so would i need to recode where I use the selected index?
because that seems to just give me the position of the first array instead of using a loop to find it
Ill see what I can come up with a report back before confusing myself further
Are they expecting you to use a ListBox and therefore have an index to select already there?
yeah
Strange then, as yes the way you've said is obviously more straightforward, they might just be wanting you to intentionally take the long way round for the sake of using while loops
thats what im thinking, thats why i was having trouble wording the question
could I store the selected index position in a variable?
then just have a loop run through the second array the way that you said the run the first one
You could create the variable for whether you've found the dorm, the index variable, and price variable before your loop,
then create your loop to find the dorm the user has selected, inside the loop check if the element at that position is the dorm they selected, if not increment the index, if it is then get the price from your other array and store it in the price variable and update your variable that tells you whether you've found the dorm yet so that your loop stops
that I do not understand
Which bit?
i figured it out