divide number list in 4

This is a discussion on divide number list in 4 within the C++ Programming forums, part of the General Programming Boards category; Hi I'm currently learning c++ from a book which asks a question: Write a program to compute and print the ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    2

    divide number list in 4

    Hi I'm currently learning c++ from a book which asks a question:
    Write a program to compute and print the quartiles (that is, the quarter of the numbers with the largest values, the next highest quarter, and so on) of a set of integers.

    I have to do this using std::vector but depending on the list's size it is unable to be divided evenly into 4. Could anyone please tell me the solution?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,199
    This seems like homework , so do it yourself!!

    Come back when you have trouble with a specific piece of code!

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by mudmonkey
    ...depending on the list's size it is unable to be divided evenly into 4. Could anyone please tell me the solution?
    actually, the best way to go about a problem like this is in a non-programatic way--think about it this way--you're given 30 coins, and you have to split them as per your instructions. how would you go about doin that? (remember, we're talking about change on your desk--this has nothing to do with programming yet)

    after you do it, think about what you just did, and write it down. you now have the pseudocode for your algorithm. now you just have to know how to write code to fit that algorithm.

    believe it or not, the "figuring out how to do it" part is the most challenging part of programming. "learning C++" is simply learning a language, but the actual science behind it is what makes the field fun and interesting.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I'm trying to understand what you mean. As an example...



    Say you have a list of numbers.

    3,5,7,1,89,4,6,3:


    First quarter: 89, 7

    Second quarter: 6, 5

    Thrid quarter: 4, 3

    Fourth quarter: 3, 1



    Is this what you mean? In order for the
    list to be split into quarters the
    total number of elements in the list
    has to be a exactly divisible by 4.

    Let's say I had nine instead of eight
    elements- using the first example again.

    3,5,7,1,89,4,6,3,12

    First quarter: 89, 12

    second quarter:7,6

    Third quarter:5,4

    Fourth quarter:3,3

    Left over(s): 1

    What you intend to do with the left overs
    is up to you. But clearly, you cannot divide
    a list of nine elements evenly into four.

    I have written a code for you which does as you ask, if indeed I
    have interpreted what you mean correctly. It should be easy to
    understand and you may want to use a function to call each quarter.


    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
      int array[81];
      int count=0;
      int terminate=0;
    
      // store numbers inside an array
      while (terminate==0)
      {   
        
        cout<<"Enter number or 0 to quit:";
        cin>>array[count];
        
        
            if(array[count]==0)
            {
              terminate=1;
            }    
    
             count++;
      
      }
    
    // Find out what each quarter of the total list is
    // Note it has to be 'int' rather than 'float'
     
     int quarter=(count-1)/4;
     int quarter_2= 2*quarter;
     int quarter_3= 3*quarter;
     int quarter_4= 4*quarter;
     int left_over= (count-1)-quarter_4;  // if the list is not divisible by 4
     
    
     
    
     
     
     //Begin to order list of numbers in descending order
     //Use method similar to bubble sort
     
     
     for (int a=0; a<count-1; a++)
     {
         for (int b=0; b<count-1; b++)
         {
             
             if(array[a]>array[b])
             {   
                 int temp,temp_2;
                 temp=array[b];
                 temp_2=array[a];
                 array[b]=temp_2;
                 array[a]=temp;
             }
         }
     }
                 // You can put this into a function if you want
                 cout<<""<<endl;
                 cout<<""<<endl;
                 cout<<"First quarter:"<<endl;
                 for (int m=0; m<quarter; m++)
                 {
                     
                     cout<<array[m]<<",";
                 }  
                 cout<<""<<endl;              
                 
                 
                 cout<<"Second quarter:"<<endl;
                 for (int n=quarter; n<quarter_2; n++)
                 {
                     
                     cout<<array[n]<<",";
                 }
                 cout<<""<<endl;
                  
                  
                 cout<<"Third quarter:"<<endl;
                 for (int o=quarter_2; o<quarter_3; o++)
                 {
                     
                     cout<<array[o]<<",";
                 }
                 cout<<""<<endl;
                 
                 cout<<"Fourth quarter:"<<endl;
                 for (int p=quarter_3; p<quarter_4; p++)
                 {
                     
                     cout<<array[p]<<",";
                 }
                 cout<<""<<endl;
                 
                 
                 cout<<"Left overs:"<<endl;
                 for (int q=quarter_4; q<(quarter_4)+left_over; q++)
                 {
                     
                     cout<<array[q]<<",";
                 }
                 cout<<""<<endl;
                 
                 
      
    
    
    int stop;
    cin>>stop;    
    
    
    }

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,199
    And another one gives out full answer......*sigh*!
    Last edited by Shakti; 03-18-2005 at 01:10 PM.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    62
    Shakti ,

    Do you know I was going to ask why are you telling him to do it himself then complain when someone else gave him the total answer, but as I wrote down that I myself would try to work it out first then only ask for help on the actual bit I would be stuck on, so I guess your right, but I do feel that just saying do it yourself donít help anyone.

    Chris
    Last edited by chrismax2; 03-18-2005 at 05:55 PM.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    2
    To clarify my question I'm not sure what the book is asking me to do. It isn't very clear. It seems to be asking me to be able to divide any list of numbers stored in a vector into quarters arranged by size. I know how to do this for an evenly divisible list but I can't fathom how I can divide an uneven list? Do I make up data like say averaging the two numbers nearest the quartiles to make it even or leave over the remaining data as one of you suggested?

    And no this is not homework I'm simply learning on my own out of a book and am trying to figure out what a question is asking of me. Oh and thanks for all the replies everyone.

  8. #8
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    well . . . since its not homework . . . how do you want to do it?

    figure out what you want to do, making sure that you learn something out of it, but also making sure your not making it too complicated.

    good luck
    Keyboard Not Found! Press any key to continue. . .

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,199
    >>but I do feel that just saying do it yourself donít help anyone.

    It helps more than you probably think it does. I suggest you have a look at the this and this thread.

    You see so many "could you please post the code" in all different forms so this is the way I naturally react to that.

    To the OP, since this doesnt seem to be homework, I can gladly admit I overreacted. But as I said, you will do yourself a favor if you attempt to solve it in some way or another, come here with code and ask us specifc questions on specific parts of your code.
    Last edited by Shakti; 03-19-2005 at 01:38 AM.

  10. #10
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    hmmm,

    To clarify my question I'm not sure what the book is asking me to do. It isn't very clear. It seems to be asking me to be able to divide any list of numbers stored in a vector into quarters arranged by size. I know how to do this for an evenly divisible list but I can't fathom how I can divide an uneven list? Do I make up data like say averaging the two numbers nearest the quartiles to make it even or leave over the remaining data as one of you suggested?
    If there is a way to divide an uneven list into quarters there would be so many possible ways to do it, how do you decide which one the book is asking for?



    On the other hand, the idea of 'quartiles' is closely linked to cummulative frequency diagrams. E.g ( lower-quartile, median and upper-quartile) Perhaps you may have realised this.

    Read up on this subject since it may give you ideas as to how you wish to continue this project.

    After all I can only see a use for splitting a list into quarters when using 'cummulative frequency diagrams.'

    good luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  5. Replace a list with a new entry?
    By niponki in forum C Programming
    Replies: 4
    Last Post: 08-17-2005, 10:41 AM

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