C
C#12mo ago
Sytang

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."
int[] dgDormCostArray = { 1500, 1600, 1800, 2500 };
int[] dgMealPlanArray = { 600, 1200, 1700 };
string[] dgDormListArray = { "Allen Hall", "Pike Hall", "Farthing Hall", "University Suites" };
string[] dgMealPlanChoiceArray = { "7 meals per week", "14 meals per week", "Unlimited meals" };
int[] dgDormCostArray = { 1500, 1600, 1800, 2500 };
int[] dgMealPlanArray = { 600, 1200, 1700 };
string[] dgDormListArray = { "Allen Hall", "Pike Hall", "Farthing Hall", "University Suites" };
string[] dgMealPlanChoiceArray = { "7 meals per week", "14 meals per week", "Unlimited meals" };
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
SinFluxx
SinFluxx12mo ago
Have you been introduced to any loops in school / looked up any yourself?
Sytang
SytangOP12mo ago
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
dgTotalPrice = dgDormCostArray[dgDormListBox.SelectedIndex] + dgMealPlanArray[dgMealPlanListBox.SelectedIndex];
dgTotalPrice = dgDormCostArray[dgDormListBox.SelectedIndex] + dgMealPlanArray[dgMealPlanListBox.SelectedIndex];
that was just to satisfy any errors and it does get the job done but I am required to use while loops in particular
namespace DanielGurr_FinalProject_10_5_2_
{
public partial class DG_MainForm : Form
{
int[] dgDormCostArray = { 1500, 1600, 1800, 2500 };
int[] dgMealPlanArray = { 600, 1200, 1700 };
string[] dgDormListArray = { "Allen Hall", "Pike Hall", "Farthing Hall", "University Suites" };
string[] dgMealPlanChoiceArray = { "7 meals per week", "14 meals per week", "Unlimited meals" };
int dgTotalPrice;
public DG_MainForm()
{
InitializeComponent();
}

private void DG_MainForm_Load(object sender, EventArgs e)
{
dgDormListBox.Items.Clear();
dgMealPlanListBox.Items.Clear();

for (int i = 0; i < dgDormListArray.Length; i++)
{
dgDormListBox.Items.Add(dgDormListArray[i].ToString());
}

for (int i = 0; i < dgMealPlanChoiceArray.Length; i++)
{
dgMealPlanListBox.Items.Add(dgMealPlanChoiceArray[i].ToString());
}
}

private void dgDisplayCostButton_Click(object sender, EventArgs e)
{
dgTotalPrice = 0;
dgTotalPrice = dgDormCostArray[dgDormListBox.SelectedIndex] + dgMealPlanArray[dgMealPlanListBox.SelectedIndex];

}
}
}
namespace DanielGurr_FinalProject_10_5_2_
{
public partial class DG_MainForm : Form
{
int[] dgDormCostArray = { 1500, 1600, 1800, 2500 };
int[] dgMealPlanArray = { 600, 1200, 1700 };
string[] dgDormListArray = { "Allen Hall", "Pike Hall", "Farthing Hall", "University Suites" };
string[] dgMealPlanChoiceArray = { "7 meals per week", "14 meals per week", "Unlimited meals" };
int dgTotalPrice;
public DG_MainForm()
{
InitializeComponent();
}

private void DG_MainForm_Load(object sender, EventArgs e)
{
dgDormListBox.Items.Clear();
dgMealPlanListBox.Items.Clear();

for (int i = 0; i < dgDormListArray.Length; i++)
{
dgDormListBox.Items.Add(dgDormListArray[i].ToString());
}

for (int i = 0; i < dgMealPlanChoiceArray.Length; i++)
{
dgMealPlanListBox.Items.Add(dgMealPlanChoiceArray[i].ToString());
}
}

private void dgDisplayCostButton_Click(object sender, EventArgs e)
{
dgTotalPrice = 0;
dgTotalPrice = dgDormCostArray[dgDormListBox.SelectedIndex] + dgMealPlanArray[dgMealPlanListBox.SelectedIndex];

}
}
}
idk how to make it look right with the colors and all
SinFluxx
SinFluxx12mo ago
So do you get the premise of how whole loops work?
Sytang
SytangOP12mo ago
yeah
SinFluxx
SinFluxx12mo ago
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?
Sytang
SytangOP12mo ago
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
SinFluxx
SinFluxx12mo ago
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
Sytang
SytangOP12mo ago
so essentially set a while loop that actually has nothing to do with positioning and only looks for the specific value I want
SinFluxx
SinFluxx12mo ago
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
while(element isn't found)
{
// code in here to check array element, increment index if not found
}
while(element isn't found)
{
// code in here to check array element, increment index if not found
}
Sytang
SytangOP12mo ago
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
SinFluxx
SinFluxx12mo ago
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)
Sytang
SytangOP12mo ago
string[] dgDormListArray = { "Allen Hall", "Pike Hall", "Farthing Hall", "University Suites" };
string[] dgDormListArray = { "Allen Hall", "Pike Hall", "Farthing Hall", "University Suites" };
and
int[] dgDormCostArray = { 1500, 1600, 1800, 2500 };
int[] dgDormCostArray = { 1500, 1600, 1800, 2500 };
for example If I choose "Farthing Hall", have the loop check dgDormCostArray and iterate until it reaches 1800 both are the 3rd element
SinFluxx
SinFluxx12mo ago
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)
Sytang
SytangOP12mo ago
that makes a lot of sense, i didnt think of saving the index to a variable itself
SinFluxx
SinFluxx12mo ago
You'll have a variable to store the index you're currently at when looping through dgDormListArray, so you can just use that
Sytang
SytangOP12mo ago
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
SinFluxx
SinFluxx12mo ago
Are they expecting you to use a ListBox and therefore have an index to select already there?
Sytang
SytangOP12mo ago
yeah
SinFluxx
SinFluxx12mo ago
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
Sytang
SytangOP12mo ago
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
SinFluxx
SinFluxx12mo ago
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
Sytang
SytangOP12mo ago
that I do not understand
SinFluxx
SinFluxx12mo ago
Which bit?
Sytang
SytangOP12mo ago
i figured it out
Want results from more Discord servers?
Add your server