I am a newbie and would like to write a method which will take int array of 100 elements(unsorted array) I dont want to use any sort algorithm , I would like to compare the numbers and print the top ten numbers.
The signature would look like this :
public int[] FindTopTen(int[] intarray)
{}
I could only write this for top two numbers.
Any help is highly appreciated.
Regards
Sangeetha
Sangeetha HaudakariPosted Nov 6, 2008, 6:59 PM
Rod PerdidoPosted Nov 6, 2008, 5:17 PM
AlanPosted Nov 6, 2008, 6:07 AM
You've got a number of problems there, Sangeetha, which I've attempted to correct and comment below:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace FindLargestTenNumbers
{
class Program
{
public int[] FindLargestNumbers(int[] intarray)
{
ArrayList list = new ArrayList(intarray);
for (int k = 0; k
{
Console.WriteLine(list[k]);
}
int[] topTen = new int[10];
int i = 0;
while (i < 10)
{
int max = (int)list[0]; // needs to be within while loop
int pos = 0; // record index of initial max
for (int j = 0; j < list.Count; j++) // need to reset loop to zero for each i
{
if ((int)list[j] >= max)
{
max = (int)list[j];
pos = j; // record index of new max
}
}
Console.WriteLine(max);
topTen[i] = max;
Console.WriteLine(topTen[i]);
Console.WriteLine("the index of max is " + pos);
list.RemoveAt(pos); // remove max value from arraylist by position
i++;
}
return topTen;
}
static void Main(string[] args)
{
int[] A = new int[20] {1,10,20,2,3,4,2,5,56,76,4,3,8,78,90,12,16,18,19,20};
Program p = new Program();
//p.FindLargestNumbers(A);
int[] resArray = p.FindLargestNumbers(A);
for (int i = 0; i <= resArray.Length - 1; i++)
{
Console.WriteLine(resArray[i]);
}
Console.ReadLine();
}
}
}
Sangeetha HaudakariPosted Nov 5, 2008, 1:43 AM
Can some one look at my code and let me know how could i remove max value from an arraylist.
using
System;using
System.Collections.Generic;using
System.Text;using
System.Collections;namespace
FindLargestTenNumbers{
class Program{
public int[] FindLargestNumbers(int[] intarray){
ArrayList list = new ArrayList(intarray); for (int k = 0; k{
}
int[] topTen = new int[10]; int max = (int)list[0]; int i = 0; int j = 0; while (i < 10){
for (; j < list.Count; j++){
if ((int)list[j] >= max)max = (
int)list[j];}
Console.WriteLine(max);topTen[i] = max;
Console.WriteLine(topTen[i]); Console.WriteLine("the indexof max is " +list.IndexOf(max)); //find the index of the max valuelist.Remove(max); remove max value from an arraylist
i++;
}
return topTen;}
static void Main(string[] args){
int[] A = new int[20] {1,10,20,2,3,4,2,5,56,76,4,3,8,78,90,12,16,18,19,20}; Program p = new Program(); //p.FindLargestNumbers(A); int[] resArray = p.FindLargestNumbers(A); for (int i = 0; i <= resArray.Length - 1; i++){
Console.WriteLine(resArray[i]);}
Console.ReadLine();}
}
}
Sangeetha HaudakariPosted Nov 4, 2008, 8:10 PM
Hi Alan,
Thanks for your suggestions/pointers. I followed them and wrote a function but am kind of lost could you please help me .
using
System;using
System.Collections.Generic;using
System.Text;using
System.Collections;namespace
FindLargestTenNumbers{
class Program{
public void FindLarestNumbers(int[] intarray){
ArrayList list = new ArrayList(intarray); int max=(int)list[0]; foreach (int i in list){
if ((int)list[i] >= max)max = (
int)list[i];}
ReturnTopTen(max);
}
public int[] ReturnTopTen(int x){
int[] topTen = new int[10]; for (int i = 0; i <= topTen.Length - 1; i++){
topTen[i] = x;
}
return topTen;}
static void Main(string[] args){
int[] A = new int[20] {1,10,20,2,3,4,2,5,56,76,4,3,8,78,90,12,16,18,19,20}; Program p = new Program();p.FindLarestNumbers(A);
//for (int i = 0; i <= resarray.Length - 1; i++) //{ // Console.WriteLine(resarray[i]); //} Console.ReadLine();}
}
}
AlanPosted Nov 4, 2008, 7:22 AM
To do this without sorting, I'd start by copying the array to an ArrayList (or List if you've done generics).
Then create an array to hold the 10 highest numbers.
Now iterate through the ArrayList to find the highest number. When you've found the number, enter it in the 'highest' array and then remove it from the ArrayList.
Repeat the iteration/removal ten times and then return the 'highest' array.