help needed for new programmer
hi i want to sort this array using c# to find maximum and minimum values from the 10 numbers entered.
int[] marks = new int [9];
int max=0;
int min=0;
int x;
int y;
Console.WriteLine("enter the marks of ten students and i will tell u which is maximum and minimum");
for (x = 0; x < marks.Length; x++)
{
marks[x] = Convert.ToInt32(Console.ReadLine());
//min = marks[0];
}
for (y = 0; y < marks.Length; y++)
{
//here if starting no is greater than ending no than it will display the lowest and highest no
if (min<marks[0])
{
min = marks[0];
Console.WriteLine("{0} is the minimum", min);
Console.ReadLine();
}
else if (min>marks[1])
{
max = marks[1];
Console.WriteLine("{0} is the maximum", max);
Console.ReadLine();
}
//}
// Console.WriteLine("the highest is {0} and the lowest is {1}", marks,min);
}
[1472 byte] By [
shahina] at [2007-11-20 11:45:27]

# 1 Re: help needed for new programmer
So... what is the problem? :D
You want to sort or just find the max and min value? :p
saktya at 2007-11-9 11:37:00 >

# 2 Re: help needed for new programmer
the problem is i have to enter marks of 10 students and then find the maximum and minimum marks from those 10 numbers.
# 3 Re: help needed for new programmer
the problem is the code i used doesnt show the sorting of numbers to give the actual max and min values it just show the first positioned value as max i need to sort them and give the result.
int[] marks = new int [9];
int max=0;
int min=0;
int x;
int y;
Console.WriteLine("enter the marks of ten students and i will tell u which is maximum and minimum");
for (x = 0; x < marks.Length; x++)
{
marks[x] = Convert.ToInt32(Console.ReadLine());
//min = marks[0];
}
for (y = 0; y < marks.Length; y++)
{
//here if starting no is greater than ending no than it will display the lowest and highest no
if (min<marks[0])
{
min = marks[0];
Console.WriteLine("{0} is the minimum", min);
Console.ReadLine();
}
else if (min>marks[1])
{
max = marks[1];
Console.WriteLine("{0} is the maximum", max);
Console.ReadLine();
}
//}
// Console.WriteLine("the highest is {0} and the lowest is {1}", marks,min);
}
# 4 Re: help needed for new programmer
can anybody help me pllzz i need it to be done soon
# 5 Re: help needed for new programmer
I've found something in MSDN
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Pachycephalosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nSort");
dinosaurs.Sort();
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nBinarySearch and Insert \"Coelophysis\":");
int index = dinosaurs.BinarySearch("Coelophysis");
if (index < 0)
{
dinosaurs.Insert(~index, "Coelophysis");
}
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nBinarySearch and Insert \"Tyrannosaurus\":");
index = dinosaurs.BinarySearch("Tyrannosaurus");
if (index < 0)
{
dinosaurs.Insert(~index, "Tyrannosaurus");
}
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
}
}
hth
# 6 Re: help needed for new programmer
for (y = 0; y < marks.Length; y++)
{
//here if starting no is greater than ending no than it will display the lowest and highest no
if (min<marks[0]) <-- Why always 0? :p
{
min = marks[0];
Console.WriteLine("{0} is the minimum", min); <-- Why print to console in every iteration
Console.ReadLine();
}
else if (min>marks[1]) <- Why always 1?
{
max = marks[1];
Console.WriteLine("{0} is the maximum", max);<-- Why print to console in every iteration
Console.ReadLine();
}
:P find the answer by yourself... Programming is fun... :D
saktya at 2007-11-9 11:42:05 >

# 7 Re: help needed for new programmer
thanks for ur suggestions but i need sorting to be done with numbers to find max and min values in an array
# 8 Re: help needed for new programmer
If you need to sort you can learn about bubble sort or quick sort algorithm this algorithm is easy to understand. :p
saktya at 2007-11-9 11:44:14 >

# 9 Re: help needed for new programmer
thanks for ur suggestions but i need sorting to be done with numbers to find max and min values in an arraySounds me to be a homework which comes again every year in this forum- And we dont do homework. If you need to find max value use a loop go through each value. if izts bigger it is the new max value if its less forget it, test the next one
when done you will have your mayvalue.
then use a second loop going through each value and test them again. Now if its less then the other store it as new Minvalue test every row of your loop if the value is smaller then your minvalue if yes you have your new Min value.
So you ned two loops one array with all the values stored and a max and a min field to do it.
Such simple now code it.
# 10 Re: help needed for new programmer
Or, in the real world you might use Array.Sort, but if this is an assignment you'll get an F for that solution.
CJ1 at 2007-11-9 11:46:13 >

# 11 Re: help needed for new programmer
Or, in the real world you might use Array.Sort, but if this is an assignment you'll get an F for that solution.rofl :lol: