|
-
October 29th, 2007, 11:11 PM
#1
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);
}
Last edited by cjard; November 8th, 2007 at 10:55 AM.
Reason: learn how to use CODE tags please
-
October 30th, 2007, 12:13 AM
#2
Re: help needed for new programmer
So... what is the problem? 
You want to sort or just find the max and min value?
-
October 30th, 2007, 01:43 AM
#3
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.
-
October 30th, 2007, 02:02 AM
#4
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);
}
Last edited by cjard; November 8th, 2007 at 10:56 AM.
Reason: CODE tags!!
-
October 30th, 2007, 03:46 AM
#5
Re: help needed for new programmer
can anybody help me pllzz i need it to be done soon
-
October 30th, 2007, 03:55 AM
#6
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
-
October 30th, 2007, 04:42 AM
#7
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...
-
October 30th, 2007, 11:41 PM
#8
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
-
October 31st, 2007, 12:08 AM
#9
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.
-
October 31st, 2007, 06:35 PM
#10
Re: help needed for new programmer
 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.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
November 1st, 2007, 10:28 AM
#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.
-
November 1st, 2007, 06:42 PM
#12
Re: help needed for new programmer
 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
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
November 7th, 2007, 06:43 PM
#13
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();
Last edited by cjard; November 8th, 2007 at 10:57 AM.
Reason: code tags.. (bored now..)
-
November 7th, 2007, 07:39 PM
#14
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
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.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
November 8th, 2007, 10:04 AM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|