help needed to sort the array for maximum and minimum values to be found
hi i want to sort this array using c# to find maximum and minimum values from the 10 numbers entered.
Code:
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);
}
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
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.
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.
Code:
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);
}
Re: help needed for new programmer
can anybody help me pllzz i need it to be done soon
Re: help needed for new programmer
I've found something in MSDN
Code:
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
Re: help needed for new programmer
Code:
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
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
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
Re: help needed for new programmer
Quote:
Originally Posted by shahina
thanks for ur suggestions but i need sorting to be done with numbers to find max and min values in an array
Sounds 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 iz´ts 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.
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.
Re: help needed for new programmer
Quote:
Originally Posted by CJ1
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:
Re: help needed for new programmer
hi every one thanks for showing me different methods of sorting i used the bubble sort method to sort the arrays and it shows from ascending order to descending order but not the max and min value so what do i need to do ?
Code:
int[] marks = new int [9];
int rate=0;
int x = 0;
int y = 0;
int z = 0;
Console.WriteLine("enter the marks of ten students and i will tell u which is maximum and minimum");
for ( int x = 0; x < marks.Length; x++)
{
marks[x] = Convert.ToInt32(Console.ReadLine());
for (int y = 0; y < marks.Length; y++)
{
for(int z=0;z<marks.Length;z++)
{
if (marks[y]<marks[z])
{
rate=marks[y];
marks[y]=marks[z];
marks[z]=rate;
}
}
}
Console.WriteLine("the number in the sorted order are:");
for(int x =0;x<marks.Length;x++)
{
Console.WriteLine("{0}",marks[x]);
//Console.WriteLine("the minimum value is {0}", marks[x]);
}
Console.ReadLine();
Re: help needed for new programmer
There is absolutly no need for any sorting to find max and min values of an array. Re-read my post #10 and study what I have written there how to do it ! Which word of what I described havn't you fully understood ?
I wrote there
Quote:
If you need to find max value use a loop go through each value. if iz´ts bigger it is the new max value if its less forget it, test the next one
when done you will have your mayvalue.
Re: help needed for new programmer
There are actually many different methods to solve your problem.
To solve your problem, you may even not need to explicitly sort the array after you got all the students' marks. You can compose a sorted array when you insert each of the student mark. You can achieve this by comparing each of the members in the array when inserting new member.
Re: help needed for new programmer
shahina, please learn how to use CODE tags before you post any more code
http://www.codeguru.com/forum/misc.php?do=bbcode
Re: help needed for new programmer
hi everybody thanks for helping me solve the program i got the max and min values
int[] marks = new int[10];
int temp = 0;
int max = 0;
int min = 0;
Console.WriteLine("enter the marks of ten students and i will tell u which is maximum and minimum");
for (int x = 0; x < marks.Length; x++)
{
marks[x] =Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < marks.Length; i++)
{
for (int y = 0; y < marks.Length; y++)
{
if (marks[i] < marks[y])
{
temp = marks[i];
marks[i] = marks[y];
marks[y] = temp;
}
}
}
Console.WriteLine("The numbers in the sorted order are : ");
for (int x = 0; x < marks.Length; x++)
{
Console.WriteLine("{0}", marks[x]);
}
Console.ReadLine();
min = marks[0];
max = marks[9];
Console.WriteLine("{0} is the minimum value and {1} is the maximum value ", marks[0], marks[9]);
Console.ReadLine();
Re: help needed for new programmer
Code:
for (int i = 0; i < marks.Length; i++)
{
// Don't sort, just test for marks[i] > max and marks[i] < min
// and set the min and max values.
}