David W's Profile User Rating: ***--

Reputation: 281 Architect
Group:
Authors
Active Posts:
1,791 (0.79 per day)
Joined:
20-September 08
Profile Views:
21,467
Last Active:
User is offline Private
Currently:
Offline

Previous Fields

Country:
CA
OS Preference:
Windows
Favorite Browser:
FireFox
Favorite Processor:
Intel
Favorite Gaming Platform:
Who Cares
Your Car:
Chevrolet
Dream Kudos:
1225

Latest Visitors

Icon   David W is off-line ...

Posts I've Made

  1. In Topic: simple doubt on basic sorting in c++

    Posted 14 Aug 2014

    01	#include<iostream.h> // use #include <iostream>
    02	#include<conio.h>    // don't use this to keep code portable
    03	void main()          // the standard is int main() ... i.e. main returns an int
    04	{
    05	   clrscr();            // don't use this or any parts of conio.h for the aabove mentioned reason
    06	   int a[6],k,i,j,temp;
    07	   cout << "Enter 6 numbers : " << endl ;
    08	   for(i=0;i<6;i++)
    09	   {
    10	      cin >> a[6]; // 2 BIG errors here ... but both fixed by changing 6 to i
    11	   }
               // ...
    
    


    just to get you started ...


    Also ... you may want to do some web research until you 'see' how a bubble sort works ...?

    It is really easy and obvious ... 'when you see it' .
  2. In Topic: Help on Class and Client Program

    Posted 30 Jul 2014

    Take a simple example:

    int a = 5, b = 6, c = 7, d;

    // this is ok and will compile:
    d = a + b + c;

    // this is NOT ok and will NOT compile:
    d + a = b + c; // bad line // why ? //


    You had tried above ...

    Quote

    double GiftWrap::calcTotal(double subtotal, double tax)
    {
        double total;
        subtotal + tax = total;
        return total;
    }
    
    
  3. In Topic: factorial of large numbers

    Posted 30 Jul 2014

    What you really need ... is ... a BigInt class ...

    and ...
    if your check at my signature web links (below) you will find several simple examples there.

    Or Google ...
    Big Numbers ... Big Int (Integers) ... via a C++ class David
  4. In Topic: Anyone know how to use alphabets to break a do while loop? UEGENT!

    Posted 30 Jul 2014

    If you mean 'how to break out of an input loop, using cin to input integers, if a non-integer was entered' ... ?

    // ...
    string prompt = "Enter an int (or enter 'x' to exit input loop) : ";
    cout << prompt;
    int tmp;
    while( cin >> tmp )
    {
       cout << "You entered: " << tmp << endl;
       cout << prompt <<;
    }
    
    cin.clear(); // clear cin error state flags
    cin.sync(); // 'flush' cin stream ...
    // ..
    
    .


    But here, I don't think that is what you want to do.

    It is probably a good idea to use a container to hold the names.
    That way changes to the names and the number of names can easily be accommodated ...
    like this:


    const string CHOICES[] =
        {
            "Lional Messi",
            "Argen Roben",
            "Thomas Miller",
            "James Roduguez",
            "Neymar Da Silva Santos junior",
            "Christiana Ronaldo",
            "Jan Vertonghen",
            "Robin van Persie",
            "Miralem Pjanic",
            "Karim Mostafa Benzema"
        } ;
    const int NUM_CHOICES = sizeof CHOICES / sizeof *CHOICES;
    
    void showChoices()
    {
        for( int i = 0; i < NUM_CHOICES; ++ i )
             cout << setw( 3 ) << (i+1) << ": " << CHOICES[i] << endl;
    }
    



    Now if you want to print out the top names with the most votes ...
    sorting an array of pairs holding an index and count_of_votes
    could be used to facilitate that:

    // used to help sort in decreasing 'count' order ...
    struct MyPair
    {
        // ctor...
        MyPair() : index(0), count(0) {}
    
        int index;
        int count;
    } ;
    
    // bubble sort into descending count order ... 
    // Note: bubble sort is ok to use for *small* sized arrays
    // function prototype ... 
    // Note: you WILL need to define this bubble sort before it is called
    void bsort( MyPair ary[], int size );
    




    Now ... to facilitate getting input and prompting for input,
    you may like to add these utilities to your 'student tool kit' ...


    // some utilities that could be used here ...
    
    string takeInString( const string& msg = "" )
    {
        cout << msg << flush;
        string val;
        getline( cin, val );
        return val;
    }
    char takeInChr( const std::string& msg = "" )
    {
        string reply = takeInString( msg );
        if( reply.size() )
    		return reply[0];
        // else ...
        return 0;
    }
    bool more()
    {
        if( tolower( takeInChr( "More (y/n) ? " )) == 'n' )
    		return false;
        // else ...
        return true;
    }
    int takeInValidInt( const string& msg, int min, int max )
    {
        int val;
        while( true )
        {
            cout << msg << flush;
            if( cin >> val && cin.get() == '\n' )
            {
                if( val >= min && val <= max )
                    break;
                // else ...
                cout << "\nERROR! Valid ranhe here is " << min
                     << ".." << max << endl;
            }
            else
            {
                cout << "\nERROR!  Only integers valid here ...\n";
                cin.clear(); // clear error flags
                cin.sync(); // 'flush' cin stream ...
            }
        }
        return val;
    }
    




    Now in main, your logic flow could simply be like this:


    	// get some array space to handle vote counts and sorting
    
    	int votes[NUM_CHOICES] = { 0 };
    	MyPair myPairs[NUM_CHOICES];
    	
    	// populate array holding frequency of vote for each person ...
    	do
    	{
        	showChoices();
            int index = takeInValidInt( "Your choice is : ", 1, NUM_CHOICES );
            ++ votes[ index-1 ];
        }
        while( more() );
        
        // now update array of pairs ...
        for( int i = 0; i < NUM_CHOICES; ++ i )
        {
            myPairs[i].index = i ;
            myPairs[i].count = votes[i] ;
        }
        
    // used in debugging ...
    /*
        cout << "\nBefore sorting ... \n";
        for( int i = 0; i < NUM_CHOICES; ++ i )
             cout << myPairs[i].index << ", votes = "
                  << myPairs[i].count << endl;
    */
        // now sort array of pairs by count in descending order ...
        bsort( myPairs, NUM_CHOICES );
    
    // used in debugging ...
    /*
        cout << "\nAfter sorting ... \n";
        for( int i = 0; i < NUM_CHOICES; ++ i )
             cout << myPairs[i].index << ", votes = "
                  << myPairs[i].count << endl;
    */
        // now can print top 5  (or all in sorted order ...)
        for( int i = 0; i < NUM_CHOICES; ++ i )
        {
            cout << "votes = " << myPairs[i].count << ", "
                 << CHOICES[ myPairs[i].index ] << endl;
        }
        
        cout << "\nPress 'Enter' to continue/exit ... ";
        cin.get();
    


    These all ... really ...
    are just some 'baby steps' ...
    to hopefully help your student coding days to get started on a good design footing.
  5. In Topic: I dont know how to sort the names in aphabetical orders.

    Posted 29 Jul 2014

    View PostSkydiver, on 29 July 2014 - 11:44 PM, said:

    Cross-posted here.


    Could you please update/remind us all about the problems with 'x-posting' ?

    @Bull95: did you understand the hint at the other forum about using (well named) variables that are already defined so that the compiler 'knows it already' and can then do the job at hand when it next 'sees' that variable ...

    for example:

    //if you define an int array as:
    int ary[] = { 1,2,3 );
    
    //then you can ACCESS and PRINT the values in that array like this:
    
    const int ary_size = sizeof ary / sizeof ary[0];
    for( int i = 0; i < ary_size; ++ i )
       cout << "ary[" << i << "] = " << ary[i] << endl;
    


    Hint 2:

    For small arrays, a simple bubble sort works just fine. You can see many examples of code for a bubble sort if you Google "C++ examples of bubble sort" ... (or something like that.)

My Information

Member Title:
DIC supporter
Age:
67 years old
Birthday:
June 29, 1947
Gender:
Location:
Toronto Ontario Canada
Interests:
The events that lead up to the soon return of Yeshua (Jesus) ... and being ready
Full Name:
David Wayne Zavitz
Programming Languages:
Basic
6502 Assembly
Comal
C++
HLA (High Level Assembly)
Pascal
C
Python
Sqlite3

Comments

Page 1 of 1
  1. Photo

    Invincible-99 Icon

    29 Jun 2012 - 06:10
    Oh, sorry!
    That link doesn't work.
    http://i48.tinypic.com/34qqrg3.jpg
  2. Photo

    Invincible-99 Icon

    29 Jun 2012 - 06:06
    Hi!
    [IMG]http://i48.tinypic.com/34qqrg3.jpg[/IMG]
  3. Photo

    Anarion Icon

    24 Jun 2012 - 13:44
    Regards to you, awesome friend! ;)
  4. Photo

    v0rtex Icon

    07 Jun 2012 - 03:04
    Thanks for the link! Why would u advise I look at HLA?
  5. Photo

    caleb123 Icon

    22 Jun 2010 - 19:37
    Would you like to be my website designing buddy? If you do, you can email me at cheryman22@yahoo.com
    You and I can give each other tips and answer question to make us both better at designing websites.
  6. Photo

    skyhawk133 Icon

    08 Jun 2010 - 09:43
    Great to have you David! Thanks for the kind comment.
  7. Photo

    xxxpriya Icon

    20 Feb 2010 - 21:25
    thanx a lot david
Page 1 of 1