Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,083 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,525 people online right now. Registration is fast and FREE... Join Now!




Counting same integer values

 
Reply to this topicStart new topic

Counting same integer values, Got to be a better way.....right?

Sharkadder
10 Apr, 2008 - 04:21 AM
Post #1

New D.I.C Head
*

Joined: 17 Mar, 2008
Posts: 36


My Contributions
Hi, recently i have imported aload of data from a text file into my application, this data was then shown on screen within a rich textbox.

After which i wished to count the amount of same integers that occur on each line of the text file and then display the outcome onto the form in my GUI application.

This i managed to do but my code is so messy and they simply must be an easier way of doing things.

I have made a good attempt at this, so what i need to know is how can i make my 45 line code down to about 15 lines and also make it capable if a file is uploaded which has more than 4 lines of code?

I will explain the concept of my code below firslty. Basically i have a class, this class will load the data from the imported CSV file and then put it into a textbox. So within the class firslty a streamreader is created which will look through the file one line at a time using readline.

Next i create an array of the line and look through each element of the array. I then have a switch statement to look for the different numbers in the array depending on the case. e.g. case '0' will look for all 0's on the current line in the code.

I then do a check and add it to what row number it is on e.g. i have 4 row numbers, one of each line of the imported file. If the row is equal to 1 and the column is 1, it will add all values toether from that line with the value 1 and then display them.

It does this check for all numbers in the range 1-5 on each line/row. Once it reaches row 4 it finishes and displays the values within a textbox.

If this isn't clear my code can explain more.......and yes you will be sat there right now thinking "oh my god, what has he done here". which is why i need to know how i can get around the tangle i am in. What if a file with 20 lines was imported and each line had 100 numbers ranging from 1-100? yep that's alot of coding so i need a better solution.

MY COMMENTED CODE - Not very good effort
=============================

CODE

//first we create each row and let it be an array of 5 numbers

            int [] row1 = new int [5];
            int [] row2 = new int [5];
            int [] row3 = new int [5];
            int [] row4 = new int [5];

//next we assign a value to each number in the 4 arrays, i.e. 0

            row1[0] = 0; row1[1] = 0; row1[2] = 0; row1[3] = 0; row1[4] = 0;
            row2[0] = 0; row2[1] = 0; row2[2] = 0; row2[3] = 0; row2[4] = 0;
            row3[0] = 0; row3[1] = 0; row3[2] = 0; row3[3] = 0; row3[4] = 0;
            row4[0] = 0; row4[1] = 0; row4[2] = 0; row4[3] = 0; row4[4] = 0;
            
//we create a streamreader to read in the file from a path in a seperate object
            StreamReader sr = new StreamReader(dataclass.path);
//create a string currentline which will equal the line the read is on, also create a cariable to trakc line number
            string currentLine = sr.ReadLine();
            int linenumber = 1;
//while the following is happening we do our case check on the chars in lines
            while ((currentLine = sr.ReadLine()) != null)
            {
                linenumber++;
                char[] chars = currentLine.ToCharArray();
                foreach (char c in chars)
                    switch ©
                    {
                        case '0':
                            // 0 found
                            break;
                        case '1':
//if we find a number 1 we then do checks and begin to count the number of occurances of that integer, this is then added to the correct row array element
                            if (linenumber == 1) { row1[0] += 1; } if (linenumber == 2) { row2[0] += 1; } if (linenumber == 3) { row3[0] += 1; } if (linenumber == 4) { row4[0] += 1; }
                            break;
                        case '2':
                            if (linenumber == 1) { row1[1] += 1; } if (linenumber == 2) { row2[1] += 1; } if (linenumber == 3) { row3[1] += 1; } if (linenumber == 4) { row4[1] += 1; }
                            break;
                        case '3':
                            if (linenumber == 1) { row1[2] += 1; } if (linenumber == 2) { row2[2] += 1; } if (linenumber == 3) { row3[2] += 1; } if (linenumber == 4) { row4[2] += 1; }
                            break;
                        case '4':
                            if (linenumber == 1) { row1[3] += 1; } if (linenumber == 2) { row2[3] += 1; } if (linenumber == 3) { row3[3] += 1; } if (linenumber == 4) { row4[3] += 1; }
                            break;
                        case '5':
                            if (linenumber == 1) { row1[4] += 1; } if (linenumber == 2) { row2[4] += 1; } if (linenumber == 3) { row3[4] += 1; } if (linenumber == 4) { row4[4] += 1; }
                            break;
                    }
            }
//once we have done checks on all lines for each integer in the range 1-5 we then write up the values within a rich textbox.

                richTextBox1.Text += sr.ReadToEnd();
                richTextBox1.Text += row1[0] + ":1 " + row1[1] + ":1 " + row1[2] + ":1 " + row1[3] + ":1 " + row1[4]+":1"+"\r";
                richTextBox1.Text += row2[0] + ":2 " + row2[1] + ":2 " + row2[2] + ":2 " + row2[3] + ":2 " + row2[4]+":2"+"\r";
                richTextBox1.Text += row3[0] + ":3 " + row3[1] + ":3 " + row3[2] + ":3 " + row3[3] + ":3 " + row3[4]+":3"+"\r";
                richTextBox1.Text += row4[0] + ":4 " + row4[1] + ":4 " + row4[2] + ":4 " + row4[3] + ":4 " + row4[4]+":4";
        }




//once this is done the values are displayed on screen.



Ok that is what i have done, as you can see the code is messy, what can i do to make the code alot less hastle? Again as i said earlier, the new code i would like the ability for it to handle more than 4 lines and more than 5 integers on each line. You think it is possible in much less code? if you can somebody please try and help as i know what i have done is the wrong way about doing it.

p.s. the file is comma seperated value and the fist element on each line is a name e.g. iamrow1,1,2,3,4,5,6
iamrownumber2,2,3,4,5,6,7

etc. you get the idea

thanks

This post has been edited by Sharkadder: 10 Apr, 2008 - 04:27 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Counting Same Integer Values
10 Apr, 2008 - 04:41 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Near as I can figure out, you're doing something like this:
csharp

//first we create each row and let it be an array of 5 numbers
// instead, 2D array
int cols = 5;
int rows = 4;
int [,] data = new int [rows,cols];

//next we assign a value to each number in the 4 arrays, i.e. 0
for(int row=0; row<rows; row++) {
for(int col=0; col<cols; col++) {
data[row,col]=0;
}
}

//we create a streamreader to read in the file from a path in a seperate object
StreamReader sr = new StreamReader(dataclass.path);
//create a string currentline which will equal the line the read is on, also create a cariable to trakc line number
string currentLine = sr.ReadLine();
int linenumber = 1;
//while the following is happening we do our case check on the chars in lines
while ((currentLine = sr.ReadLine()) != null) {
// ?? your line will never be one?
linenumber++;
foreach (char c in currentLine.ToCharArray()) {
int i = Convert.Int32©;
for(int row=0; row<rows; row++) {
data[i-1,linenumber-1] = 0;
}
}
}
//once we have done checks on all lines for each integer in the range 1-5 we then write up the values within a rich textbox.

richTextBox1.Text += sr.ReadToEnd();
for(int col=0; col<cols; col++) {
if (col<0) { richTextBox1.Text += "\r"; }
for(int row=0; row<rows; row++) {
richTextBox1.Text += data[row,col] + ":" + (col + 1);
}

}
sr.Close();


Hope this helps.

User is offlineProfile CardPM
+Quote Post

Sharkadder
RE: Counting Same Integer Values
10 Apr, 2008 - 10:33 AM
Post #3

New D.I.C Head
*

Joined: 17 Mar, 2008
Posts: 36


My Contributions
EDIT
===

Sorry about that, i know what the problem was, i forgot to give the int32 a string to convert, but that isn't the problem.

the problem is that it says the index was outside the bounds of the array on this line:

data[i-1,linenumber-1] = 0;

any idea's guys? I've had a look and i should be able to use the index from where the code is at :-/ hmmm

This post has been edited by Sharkadder: 10 Apr, 2008 - 11:20 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 07:51PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month