Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 26-01-2009
Member
 
Join Date: Feb 2008
Posts: 1,337
Problem printing like integers in C# ARRAYS

I want to create an array and then print all the numbers that appear multiple times, and index the case. The same combination should only be printed immediately. Joined so but it does not works.

Code:
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class IntegerCombinations: System.Web.UI.Page 
{
protected void Page_Load (object sender, EventArgs e) 
{ 
int [] integer = {7,2,4,5,6,6,7,8,6,7,8,5,7,5,6,6,4,6,5,4,3,5, 3,2,1,2,3,4,5,6,7,8,9,0,9,8,7,6,5,4,3,2,1}; 

for (int i = 0; i < integer.Length; i + +) 
{ 
for (int j = 0; j < integer.Length; j + +) 
{ 
if (integer [i] == integer [j]) 
{ 
labInteger.Text += Convert.ToString (integer [i]) + "" + Convert.ToString (integer [j]); 
} 
} 
}
} 
}
Reply With Quote
  #2  
Old 26-01-2009
Member
 
Join Date: May 2008
Posts: 945
Re: Problem printing like integers in C# ARRAYS

I do not see any direct error now. So can you tell me what is wrong with this? Is there any error message? Or is there anything else?
Reply With Quote
  #3  
Old 26-01-2009
Member
 
Join Date: Feb 2008
Posts: 1,337
Re: Problem printing like integers in C# ARRAYS

Quote:
7 77 77 77 77 77 72 22 22 22 24 44 44 44 44 45 55 55 55 55 55 55 56 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 67 77 77 77 77 77 78 88 88 88 86 66 66 66 66 66 66 66 67 77 77 77 77 77 78 88 88 88 85 55 55 55 55 55 55 57 77 77 77 77 77 75 55 55 55 55 55 55 56 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 64 44 44 44 44 46 66 66 66 66 66 66 66 65 55 55 55 55 55 55 54 44 44 44 44 43 33 33 33 35 55 55 55 55 55 55 53 33 33 33 32 22 22 22 21 11 12 22 22 22 23 33 33 33 34 44 44 44 44 45 55 55 55 55 55 55 56 66 66 66 66 66 66 66 67 77 77 77 77 77 78 88 88 88 89 99 90 09 99 98 88 88 88 87 77 77 77 77 77 76 66 66 66 66 66 66 66 65 55 55 55 55 55 55 54 44 44 44 44 43 33 33 33 32 22 22 22 21 11 1
I am getting this as the output. Can anyone explain me why is this happening?
Reply With Quote
  #4  
Old 26-01-2009
Member
 
Join Date: May 2008
Posts: 945
Re: Problem printing like integers in C# ARRAYS

You have repeated the code again and again. You are checking each numbers with all other numbers which is not necessary. Only check with the remaining numbers.

In other words, if you are having the numbers 1,2,3,4,5 then only check as follows:

1-2, 1-3, 1-4, 1-5, 2-3, 2-4, 2-5, 3-4, 3-5, 4-5

Code:
for (int i = 0; i <integer.length; i ++) 
{
for (int o = i +1; o <integer.length; o ++) 
{
if (integer [i] == integer [o]) 
{
// Your code
}
}
}
Reply With Quote
  #5  
Old 26-01-2009
Member
 
Join Date: Feb 2008
Posts: 1,337
Re: Problem printing like integers in C# ARRAYS

Thanks Lemog, thanks for helping me

Code:
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class HeltallKombinasjoner: System.Web.UI.Page 
{
protected void Page_Load (object sender, EventArgs e) 
{ 
int [] integers = {7, 2, 4, 5, 6, 6, 7, 8, 6, 7, 8, 5, 7, 5, 6, 6, 4, 6, 5, 4, 3, 5, 3, 2, 2, 3, 4, 5, 6, 7, 8, 9, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1}; 

int [] number = new int [11]; 

for (int i = 0; i < number.Length; i + +) 
{
for (int j = i +1; j <= number.Length; j + +) 
{
if (integer [i] == integer [j]) 
{
labHeltall.Text + = "Number" + i + "is:" + integer [j] + "<br />"; 
} 
} 
} 
} 
}
This works almost. Get this:

Quote:
Number 0 is: 7
Number 0 is: 7
Number 0 is: 7
Number 1 is: 2
Number 2 is: 4
Number 3 is: 5
Number 4 is: 6
Number 4 is: 6
Number 4 is: 6
Number 5 is: 6
Number 5 is: 6
Number 6 is: 7
Number 6 is: 7
Number 7 is: 8
Number 7 is: 8
Number 8 is: 6
Number 9 is: 7
Number 10 is: 8
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem printing like integers in C# ARRAYS"
Thread Thread Starter Forum Replies Last Post
Problem with 4+ dimension allocatable arrays and optimized code Tamonash Software Development 6 29-09-2010 11:59 PM
Why to use integers as loop counter variables rather than char or short? KALANI84 Software Development 5 11-02-2010 04:31 PM
Reading integers from a file into a character array afidelino Software Development 3 08-08-2009 11:32 AM
JAVA- divide an array of integers into 2 parts machok Software Development 3 18-03-2009 02:20 PM
Problem with arrays in C++ fellah Software Development 3 05-09-2008 04:45 PM


All times are GMT +5.5. The time now is 05:19 AM.