Menu

Help with vectors

2009-03-04
2012-09-26
  • Jordan Ferguson

    Jordan Ferguson - 2009-03-04

    Hey everybody
    I have just started learning c++ in my class but I hit a bit of a mental wall.
    I had a assignment to initialize a vector where you input ten values into it and then the program shows how many numbers are bigger than the first one. I managed to do this fine.
    He then wanted the program to show the inputted numbers from largest to smallest.
    I just can't seem to figure this out.
    Help would be very appreciated!

     
    • cpns

      cpns - 2009-03-04

      To be honest I do not understand teh algorithm you described. I suggest that the course material and lessons should provide a clue as to what your tutor expects. It is quite possible that he would not want you to use the library provided sort algorithm

      With only ten values to deal with you could use a brute force approach rather than the sort algorithm. Create a new empty vector, write a function to find the largest value in the forst vector and remove that from the first and add it to the second, then repeat until the first vector is empty. Efficiency would only be an issue when the number of values is much larger.

      Clifford

       
    • Jordan Ferguson

      Jordan Ferguson - 2009-03-04

      Oops, i'm sorry.
      I should post some general ideas i guess.
      I thought of a way that would work but it would be rather nasty.
      I would take the first value and have it check values 2 through 10 and for each time it's bigger, I would increment a value relating to that. I would then due this long process with numbers 2 through ten.
      I would then check each respective value to see if the ++ equals nine and cout that related number via an if statement. I would THEN check for 8 and so on.
      As you can see, it would take forever. Doe anyone have a more efficient manner?

       
    • cpns

      cpns - 2009-03-04
       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.