How to sort entered integer into ascending & find the average ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • reon
    New Member
    • Feb 2007
    • 80

    How to sort entered integer into ascending & find the average ?

    Here is my source code....
    And any one pls help me how can we find the output integers sort by ascending and find there average...

    Code:
    #include<iostream.h>
    #include<conio.h>
    void main() {
       int num[10];
       clrscr();
       cout<<"ENTER NUMBERS TO SORT THEM IN ASCENDING ORDER & FIND THE AVERAGE"<<endl;
       for(int times=0;times<=10;times++)
       {
          cout<<"Enter a number";
          cin>>num[times];
       }
       for(int z=0;z<=10;z++)
       {
          cout<<"Ouput is : "<<num[z]<<endl;
       }
       getch();
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    have a look at
    http://en.wikipedia.or g/wiki/Sorting_algorit hm
    for sort algorithms

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Code:
      #include<iostream.h>
      #include<conio.h>
      void main() {
         int num[10];
         clrscr();
         cout<<"ENTER NUMBERS TO SORT THEM IN ASCENDING ORDER & FIND THE AVERAGE"<<endl;
         for(int times=0;times<=10;times++)
         {
            cout<<"Enter a number";
            cin>>num[times];
         }
         for(int z=0;z<=10;z++)
         {
            cout<<"Ouput is : "<<num[z]<<endl;
         }
         getch();
      }
      There's one problem with your code. You start times and z at 0 to access elements of your array, but your end condition is <= 10 - you will try to access num[10] twice, which is out of bounds of the array.

      How do you find the average of any set of numbers? You add them all together, then divide that total by the number of elements. Repeat this with your array, making sure to cast the element number to a double before dividing.

      There are plenty of sorting algorithms out there, or you can make your own - since you have the numbers being entered one by one, every time a number is entered you can look for the proper place for that number to go.

      Comment

      • reon
        New Member
        • Feb 2007
        • 80

        #4
        I only know conio,iostream & fstream..is there any probability to complete the program in that way....

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Yes - in fact, for the actual sorting, you won't need any of these libraries. iostream will be handy for seeing what you're doing, though.

          Comment

          • reon
            New Member
            • Feb 2007
            • 80

            #6
            then pls help me out how can i complete this program and get output in ascending orders and find the average...

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              I have already told you how to get the average - look up and read my first post. In fact, I also told you an easy method of sorting the numbers. Now it's your turn to try the methods I've outlined and see what problems you are having.

              Comment

              Working...