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

Reputation: 298 Architect
Group:
Authors
Active Posts:
2,017 (0.72 per day)
Joined:
20-September 08
Profile Views:
24,888
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: Contacts Book without struct or class

    Posted 9 Jan 2016

    Do you understand the idea behind , or ; (or some char) separated fields?

    If you wish more fields, (on a line),
    than just the name and id fields,
    then note that ...
    each field follows the previous field
    using your chosen char delimiter
    to mark the end of each previous field.

    So just update the record take in function
    to take in all the fields

    Then update the line parsing function
    to parse
    and return by reference,
    all the fields on the line.

    If you need more help, or an example how to
    write and then read back
    char separated data fields ...
    please supply the types of fields that you need
    to take in and write to file
    (and then to read back from file.)
  2. In Topic: Need help for c program

    Posted 5 Jan 2016

    You seemed to have made a pretty good effort to implement most of the functions that I suggested so far ... :)

    but I still do not see what was expected by this:

    Quote

    -Find how many times digit occurs with 2nd largest ASCII code.


    So ... that will be up to you to find out and then to code.

    Below, please see the edits I made to your code, and note the (utility) functions that I added ...
    to facilitate prompting and getting valid (student) user input.

    Note that to help you to see the (utility) functions that I added to your code, I placed them at the top, before the main function.

    You could start off like this ...

    /* menu_driven_example.c */
    
    /*
        * Gets Integer array of 100 elements and displays entered array.
        
        * Records into a new array of all the numbers after the max
          and sorts the new array in ascending order.
          
        * Find how many times digit occurs with 2nd largest ASCII code. ???
        
        * Have a text menu from which the user can choose whether
          to introduce an array
          or process already introduced such.
    */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_SIZE 3 /* keep small while testing out code ... */
    
    const int MIN_INT_IN = 100;
    const int MAX_INT_IN = 999;
    
    const char* MENU =
        " WELCOME TO USER MENU \n"
        ".....................................\n"
        "1. Input ARRAY \n"
        "2. Show ARRAY \n"
        "3. Show SELECTED ARRAY after it was SORTED\n"
        "4. Show MAX ASCII DIGIT APPEARANCE \n"
        "0. Quit \n"
        "Enter your choice (0..4): ";
    
    const int MAX_CHOICE = 4;
    
    int showMenuGetChoice();
    int takeInAry( int* ary, int size );
    void bubbleSort( int* ary, int size );
    void showAry( const int* ary, int size );
    
    /*
        get a new array of all the numbers after the max
        and sort the new array in ascending order,
        and return size of new array (ary2)
    */
    int get2ndAry( const int* ary, int size, int* ary2 );
    



    Then add in some utility functions ...


    /* a simple student way to handle numeric input ...
       so program won't crash on bad input */
    int takeInInt( const char* msg, int myMin, int myMax )
    {
        int val = 0;
        while( 1 )
        {
            printf( msg ); fflush( stdout );
    
            if( scanf( "%d", &val ) == 1 && getchar() == '\n' )
            {
                if( myMin <= val && val <= myMax )
                    break; /*was a  good val... */
                printf( "\nValid input only in range %d..%d\n\n", myMin, myMax );
            }
            else
            {
                printf( "\nInteger input only here please ...\n\n" );
                while( getchar() != '\n' ) ; /* flush stdin ... */
            }
        }
        return val;
    }
    
    /* 2 handy utilities for many C student coding problems ... */
    int takeInChr( const char* msg )
    {
        char chr;
        printf( msg ); fflush( stdout );
        chr = getchar();
        if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
        return chr;
    }
    int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
    {
        int c = takeInChr( "More (y/n) ? " );
    	if( c == 'n' || c == 'N' ) return 0;
    	/* else ... */
    	return 1;
    }
    
    
    




    Then your main function could be quite simple ... like this:

    int main()
    {
        int ary[MAX_SIZE], ary2[MAX_SIZE], size = 0, size2 = 0;
        int choice = -1;
        while( choice )
        {
            choice = showMenuGetChoice();
            switch( choice )
            {
            case 0: break;
            case 1: size = takeInAry( ary, MAX_SIZE ); break;
            case 2: showAry( ary, size ); break;
            case 3: size2 = get2ndAry( ary, size, ary2 ); showAry( ary2, size2 ); break;
            default: printf( "\nChoice %d is not implemented here yet ...\n\n", choice );
            }
        }
    
        printf( "\nPress 'Enter' to continue/exit ... " );
        fflush( stdout );
        getchar();
        return 0;
    }
    




    Then ... you could follow with the definitions for all the function prototypes you had placed at the top:


    int showMenuGetChoice()
    {
        return takeInInt( MENU, 0, MAX_CHOICE );
    }
    
    /* returns sixe of int ...*/
    int takeInAry( int* ary, int max_size )
    {
        char prompt[80];
        int i = 0;
        do
        {
            if( i == max_size )
            {
                printf( "\nYou have reached the max size of %d\n", max_size );
                printf( "If you need to enter more numbers,\n"
                        "increase the 'MAX_SIZE' at the top of the program,\n"
                        "then recompile and run.\n\n" );
                break;
            }
            /* form prompt */
            sprintf( prompt, "\nEnter element %d of %d in range %d..%d: ",
                     i+1, MAX_SIZE, MIN_INT_IN, MAX_INT_IN );
            ary[i] = takeInInt( prompt, MIN_INT_IN, MAX_INT_IN );
            ++i;
        }
        while( more() );
        return i;
    }
    
     void showAry( const int* ary, int size )
     {
        int i;
        putchar('\n');
        for( i = 0; i < size; ++ i )
        {
            printf( "%3d  ",ary[i]);
            if( (i+1)% 10 == 0 ) putchar('\n');
        }
        puts("\n");
     }
    
    void bubbleSort( int* ary, int size )
    {
        int i, swap;
        do
        {
            swap = 0;
            for( i = 1; i < size; ++ i )
            {
                if( ary[i-1] > ary[i] ) /* swap */
                {
                    int tmp = ary[i-1];
                    ary[i-1] = ary[i];
                    ary[i] = tmp;
                    swap = 1;
                }
            }
            --size; /* largest int 'bubbles' to top on each pass */
        }
        while( swap ); /* if NO swaps ,,, DONE! */
    }
    
    /*
        get a new array of all the numbers after the max
        and sort the new array in ascending order,
        and return size of new array (ary2)
    */
    int get2ndAry( const int* ary, int size, int* ary2 )
    {
        /* get i_max ...*/
        int i_max = 0, i, j;
        for( i = 1; i < size; ++ i )
        {
            if( ary[i]> ary[i_max] )
                i_max = i;
        }
        
        /* get all ints in ary above i_max into ary2 ... */
        j = 0;
        for( i = i_max+1; i < size; ++ i )
            ary2[j++] = ary[i];
            
        /* j NOW holds size of ar2 ... */
            
        /* now sort ary2 */
        bubbleSort( ary2, j );
        
        /* return size of ary2 */
        return j;
     }
    
    
    
    
  3. In Topic: C Program to find combination of groups and letters

    Posted 4 Jan 2016

    Quote

    ...why would his professor give a student something that is

    beyond the limits of his knowledge ...


    After working with some U. students ... and seeing what some 'professors' dish out to their students ... my conclusion ... some should be fired, but firstly made to return the tuition to students for ... fraudulent NONE delivery of what was advertised ... and/or ... for teaching lies as truth.
  4. In Topic: C Program to find combination of groups and letters

    Posted 3 Jan 2016

    'Hacks' are all ugly code ...

    If/when I get time to re-visit this ... back-tracking through all the possible paths ... that running program could then also run for a very long time ... and finally ... after day? years? lifetimes? ... report that there was actually no path that covered all the nodes in the input file :(
  5. In Topic: C Program to find combination of groups and letters

    Posted 3 Jan 2016

    Here is a 'hack' of my previous code ...

    that works for the op's test file and both DIC's own @skydiver test input files.

    It is suggestive that a (more general) 'back-tracking' method
    will handle more general input than just these 3 (or 4) test files.

    So here is the 'hack' that has ok output for these 4 cases:


    /* combinations_step2.c */
    
    #include <stdio.h>
    #include <stdlib.h> /* re. malloc */
    
    
    const char* FNAME = "input.txt";
    /*
    6
    A A
    C B
    A C
    B A
    B D
    D B
    */
    
    const char* FNAME_OUT = "output.txt";
    
    typedef struct
    {
        char pair[2];
        
    } MyPair;
    
    void print( const MyPair mp )
    {
        printf( "%c %c\n", mp.pair[0], mp.pair[1] );
    }
    
    void printAll( const MyPair* ary, int size )
    {
        int i;
        for( i = 0; i < size; ++ i )
            print( ary[i] );
    }
    
    int fileAll( const char* fname, const MyPair* ary, int size, int size2 )
    {
        FILE* fout = fopen( fname, "w" );
        if( fout )
        {
            int i;
            fprintf( fout, "%d\n", size2 );
            for( i = 0; i < size; ++ i )
                fprintf( fout, "%c %c\n", ary[i].pair[0], ary[i].pair[1] );
            fclose( fout );
            return 1;
        }
        printf( "There was a problem printing file %s\n", fname );
        return 0;
    }
    
    
    /* returns the size of ordered pairs ... */
    int orderPairs(  MyPair* pairs, int size );
    
    
    
    
    int main()
    {
        FILE* fin = fopen( "input.txt", "r" );
        if( fin )
        {
            /* read the number of pairs to process */
            int count;
            if( fscanf( fin, "%d", &count ) == 1 )
            {
                MyPair* pairs = malloc( count * sizeof(MyPair) );
                if( pairs )
                {
                    /* read pairs from file ... */
                    int i;
                    for( i = 0; i < count; ++i )
                        if( fscanf( fin," %c %c", &pairs[i].pair[0], &pairs[i].pair[1] ) != 2 ) break;
    
                    if( i == count )
                    {
                        int size2;
                        
                        /* show the pairs */
                        /* printAll( pairs, count ); */
                        printf( "All %d pairs were read from file ok ...\n", count );
    
                        size2 = orderPairs( pairs, count );
                        if( size2 != count ) /* hack for OP's 100000 and both skydiver's 4 set examples */
                        {
                            MyPair tmp = pairs[size2-3];
                            pairs[size2-3] = pairs[count-1];
                            pairs[count-1] = tmp;
                            size2 = orderPairs( pairs, count );
                        }
                        
                        if( size2 != count ) /* hack for above 6 set example*/
                        {
                            MyPair tmp = pairs[size2-1];
                            pairs[size2-1] = pairs[count-1];
                            pairs[count-1] = tmp;
                            size2 = orderPairs( pairs, count );
                        }
                        
    
                        printf( "Filing the %d copied pairs ...\n", size2 );
                        fileAll( FNAME_OUT, pairs, count, size2 );
                    }
                    else printf( "Only %d of %d pairs were read\n", i, count );
    
                    free( pairs );
                }
            }
            else
                printf( "There was a problem allocating pairs memory ...\n" );
        }
        else
            printf( "There was a problem opening file %s\n", FNAME );
            
        printf( "\nPress 'Enter' to continue/exit ..." );
        fflush( stdout );
        getchar();
        return 0;
    }
    
    
    
    /* returns the size of ordered pairs ... */
    int orderPairs(  MyPair* pairs, int size )
    {
        int i, j, swap = 1, size2 = 1;
    
    
        /* order - link up pairs ...  */
        for( i = 0; i < size; ++ i )
        {
            swap = 0;
            for( j = size2; j < size; ++ j )
            {
                if( pairs[i].pair[1] == pairs[j].pair[0] )
                {
                    /* swap pairs[j] with pairs[size2] */
                    
                    if( size2 != j )
                    {
                        MyPair tmp = pairs[size2];
                        pairs[size2] = pairs[j];
                        pairs[j] = tmp;
                    }
    
                    swap = 1;
                    ++size2;
                    break;
                }
            }
            if( !swap ) break;
        }
    
        return size2;
    }
    
    

My Information

Member Title:
DIC supporter
Age:
68 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
Java
C#

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 [email protected]
    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