Like Tree2Likes
  • 1 Post By cpjust
  • 1 Post By iMalc

Need help sorting many values from highest to lowest

This is a discussion on Need help sorting many values from highest to lowest within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a program that sorts 26 different values in order. e.x: D: 7 E: 5 A: ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    Need help sorting many values from highest to lowest

    I am trying to make a program that sorts 26 different values in order.
    e.x:
    D: 7
    E: 5
    A: 4
    C: 2
    B: 1

    Here is my program code for getting the values(This code works):

    Code:
    int main
    {
        char num=0;
        int end=0;
        int a=0;
        int b=0;
        int c=0;
        int d=0;
        int e=0;
        int f=0;
        int g=0;
        int h=0;
        int i=0;
        int j=0;
        int k=0;
        int l=0;
        int m=0;
        int n=0;
        int o=0;
        int p=0;
        int q=0;
        int r=0;
        int s=0;
        int t=0;
        int u=0;
        int v=0;
        int w=0;
        int x=0;
        int y=0;
        int z=0;
    
        printf("Press a keyboard button: ");
        for (end == end; end == 0;){
            num = getch();
            switch (num)
            {
            case '0': end=1; break;
            case 'a': a++; break;
            case 'b': b++; break;
            case 'c': c++; break;
            case 'd': d++; break;
            case 'e': e++; break;
            case 'f': f++; break;
            case 'g': g++; break;
            case 'h': h++; break;
            case 'i': i++; break;
            case 'j': j++; break;
            case 'k': k++; break;
            case 'l': l++; break;
            case 'm': m++; break;
            case 'n': n++; break;
            case 'o': o++; break;
            case 'p': p++; break;
            case 'q': q++; break;
            case 'r': r++; break;
            case 's': s++; break;
            case 't': t++; break;
            case 'u': u++; break;
            case 'v': v++; break;
            case 'w': w++; break;
            case 'x': x++; break;
            case 'y': y++; break;
            case 'z': z++; break;
            }
        }
    
        printf("\nA: %d\nB: %d\nC: %d\nD: %d\nE: %d\nF: %d\nG: %d\nH: %d\nI: %d\nJ: %d\nK: %d\nL: %d\nM: %d\nN: %d\nO: %d\nP: %d\nQ: %d\nR: %d\nS: %d\nT: %d\nU: %d\nV: %d\nW: %d\nX: %d\nY: %d\nZ: %d\n", a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z);
        getchar();
        return 0;
    }
    I get my values, but I can't sort them in order. Please help!

    Thanks.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Don't use 26 different variables. Use an array or better yet, a vector. There should be info about arrays & vectors in the FAQ section if you haven't used them yet.
    Salem likes this.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Oh my gosh; A thousand times "No!"

    Lack of thinking "There must be a better way" will get you nowhere as a programmer.
    Go forth and seek out how other people solve this kind of thing.
    Salem likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating the lowest and highest values
    By Charak in forum C Programming
    Replies: 8
    Last Post: 01-30-2011, 08:57 PM
  2. Array (range lowest to highest)
    By flutegirl516 in forum C Programming
    Replies: 7
    Last Post: 02-18-2010, 09:20 AM
  3. Highest and lowest value
    By manolo in forum C Programming
    Replies: 1
    Last Post: 01-23-2010, 09:57 AM
  4. Sorting an int array highest to lowest
    By switchback in forum C Programming
    Replies: 3
    Last Post: 07-27-2008, 03:30 AM
  5. Displaying highest and lowest values
    By beastofhonor in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2006, 07:24 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21