CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Oct 2007
    Posts
    7

    Smile 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

  2. #2
    Join Date
    Jul 2003
    Location
    Bali, Indonesia
    Posts
    103

    Re: help needed for new programmer

    So... what is the problem?
    You want to sort or just find the max and min value?

  3. #3
    Join Date
    Oct 2007
    Posts
    7

    Question 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.

  4. #4
    Join Date
    Oct 2007
    Posts
    7

    Question 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!!

  5. #5
    Join Date
    Oct 2007
    Posts
    7

    Re: help needed for new programmer

    can anybody help me pllzz i need it to be done soon

  6. #6
    Join Date
    Apr 2007
    Posts
    9

    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

  7. #7
    Join Date
    Jul 2003
    Location
    Bali, Indonesia
    Posts
    103

    Resolved 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...

  8. #8
    Join Date
    Oct 2007
    Posts
    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

  9. #9
    Join Date
    Jul 2003
    Location
    Bali, Indonesia
    Posts
    103

    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.

  10. #10
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    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.
    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

  11. #11
    Join Date
    Dec 2003
    Location
    Montreal
    Posts
    58

    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.

  12. #12
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    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
    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

  13. #13
    Join Date
    Oct 2007
    Posts
    7

    Smile 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..)

  14. #14
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    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

  15. #15
    Join Date
    Nov 2007
    Posts
    4

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured