Hello,

I am working on a project and I am completely stuck. I basically need to read in text data from a file that is formatted like so:

4 numbers per line separated by a comma (there is no hard set of how many rows, this program should be able to work on any number of rows)

2, 32, 4, 7
4, 8, 3, 8
33, 11, 56, 65

Basically all I need are the 0 elements and the 3rd(last) elements within this text document and I need to convert them into ints and place them into their own arrays:

so for the 0 elements I would have an array (if using the above text example):
zeroArray[] = 2, 4, 33
thirdArray[] = 7, 8, 65

So if i said zeroArray[1] it would be 4, if i said thirdArray[0] it would say 7 (*This is using the above example data, it needs to be able to work with any number)

I need this data to be converted to ints and in their own arrays so that I can do calcuations to each element later on. I need to be able to call methods (that I will write) that will perform these calculations.

This is the problem I am having with this.

1. I cannot pull the intArray out of the while loop because it is declared within the while loop and that is its scope. But if I try and declare the intArray out of the while loop, it gives me the error 'use of unassigned local variable 'intArray'. When I declare it within the while loop, and attempt to use intArray outside of the while loop, it gives me the error "the name 'intArray' does not exist in the current context'. So my main problem is that I need to be able to access intArray so that I can use it as a parameter for my methods to perform calculations on it.

2. My second problem is intArray basically holds all 4 lines of data. If I print intArray[0], it prints out all of the 1st numbers listed, (using the example text from above) it prints:
2
4
33
If i print intArray[3], it prints out all of the 4th integers within the text data like so:
7
8
65
I'm assuming what I need to do is make two more arrays, one to hold the 1st elements of text, and a second to hold the 4th elements of text, because these need to be processed differently.

I have attempted to make arrays to store this data but no matter what I try, it doesn't work.
Instead of zeroArray[0] = 2, when I print it, it gives me the whole list:
2
4
33
If I then print zeroArray[1], expecting 4, it once again prints out the whole list
2
4
33

This is no longer in the code I will attach, I have tried several methods, but I am hoping someone knows of a solution

Unfortunately I can really only use loops and 1D arrays (no OOP or 2D arrays, which makes things easier)

Any help would be greatly appreciated, thank you.

` static void Main(string[] args)
{

``````        //declarations
string inputString = "";
string[] results;
int holder = 0;

FileStream inFile = new FileStream("input.txt", FileMode.Open, FileAccess.Read);

while (inputString != null)
{
//split the line
results = inputString.Split(',');
int[] intArray = new int[results.Length];

//do whatever processing you need to do to store it
for (int index = 0; index < results.Length; index++)
{
if (int.TryParse(results[index], out holder))
{
intArray[index] = holder;
}//end if
}//end for

}//end while

//This is a test to see if the ints are correctly stored
Console.WriteLine(intArray[0]);  //<--error here stating that 'The name intArray' does not exist in the current context'

}//end main`
``````

Once you parse the line you never store the value into your other arrays (that you haven't declared). Since you don't know the size that these arrays will be, I'd use a List<T> to hold the values.

I was storing the values into the array 'intArray' because I wasn't sure how to create a for loop that would be able to store the different values into two different arrays. I can't really think of a way to do so.

Unfortunartely I cannot use lists or 2D arrays or any sort of OOP, which really makes this difficult for me, for this project I can only use loops and 1D arrays. (even though this wouldn't be the most efficient way to do so)

Can I use two arrays instead of a List? 'intArray' does hold the converted integer values (4 different sections and I only need 2 of them for calculcations). Is there anyway to create two arrays that hold intArray[0] and intArray[3] values so that I can proceed to do calculations with them? Also how can I make it so I can use these two arrays outside the loop? If i create an array outside the loop, it won't let me use it within the loop, and if I create an array inside the loop, it won't let me use it outside of the loop.

Thanks

Not sure why you think you can't use them inside the loop if they are declared outside. They would still be in scope. The issue you'll have is you don't know how many lines are in the file, so you don't know how many you'll have to store.

Something like this (leaving out a lot):

``````int[] array1 = new int[1000];
int[] array2 = new int[1000];
int position = 0;
// stuff omitted

while (inputString != null) {
// stuff ommited

array1[position] = results[0];
array2[position] = results[3];
position++;
}
``````

This does limit you to 1000 entries. You could add before line 9 this:

``````if (position >= array1.Length) {
Array.Resize(ref array1, array1.Length*2);
Array.Resize(ref array2, array2.Length*2);
}
``````

Which will dynamically resize the arrays to hold more items. The 2nd value in the call is the new size, you don't need to double, you could make it just add 10, for example.

I'm guessing this is an assignment for a class? I'd ask the instructor why you have to learn the worst way to do something instead of the right way :)

Momerath, thank you so much, that fixed my problem! I was able to almost completely finish my program, but I came upon on last error that I have worked on for hours but haven't been able to figure out.

I have mArray right now which has a number of integers ranging from 1-35. What I need to do is create a loop that compares each element to the values 1-35. Then I need to store the results within another array

So for example let's say these are the elements within mArray[] = 1, 3, 25, 17, 30, 22, 3, 1, 20, 1
I need to keep a total of how many of each number there is.
For example, in the text above there are (3) 1s, (2), 3s, (1) 25, and so on and so for.
I need to store these results within a new array that can be hard coded to hold 35 elements since that is the range.
mTotalArray[0] could hold the number of 1s so
mTotalArray[0] = 3
mTotalArray[2] can hold all the 3s so
mTotalArray[2] = 2

I have tried numerous methods (foreach, for, while) loops but I always receive the same error, that the index is out of bounds.

Here are is one of the codes that I have attempted to implement but have just given me the index out of bounds error:

mArray is the one that we initalized based on the text data, and mTotalArray has been initialized like so (because the count needs to start at 0 and only increase if a match is found):

``````        int[] mArrayTotal = new int[35];
//Initialize mArrayTotal
for (int i = 0; i < mArrayTotal.Length; i = i + 1)
{
mArrayTotal[i] = 0;
}//end for initialization
``````

Here is one attempt:

``````        int index = 0;
int count = 0;
foreach (int element in mArray)
{
if (mArray[index] == count)
{
mTotalArray[count] = mTotalArray[count] + 1;
index = index + 1; <-- this is what causes the error but I need to   be able to move to the next element within my array
}//end if
else
{
count = count + 1;
}//end else
}//end foreach
``````

I have also tried moving the int vars index and count within the foreach loop but that just resets the counter and nothing is stored.

I have tried others as well, (while, for, if) but no success. Once again I cannot use anything but loops and 1D arrays :(. This is a starting programming class so other topics haven't been presented to us so we have to use the things that we have actually been taught.

Any tips or pointers would be appreciated!

The foreach statement will already give you each element in the mArray, there is no need to have the index variable at all.

``````foreach(int element in mArray) {
mTotalArray[element] = mTotalArray[element]+1;
}
``````

You will need to declare your array size to be 36 (so you have a range of 0-35) and ignore the zero index since you can't get a zero :)