User Profile

Collapse

Profile Sidebar

Collapse
Chris3020
Chris3020
Last Activity: Mar 17 '23, 08:46 AM
Joined: May 1 '22
Location:
  •  
  • Time
  • Show
  • Source
Clear All
new posts

  • Autoscale large integer calculations to avoid overflow

    I have some code that will (frequently) generate S sets of C count of relatively large integers N.
    I need the set with the lowest product.

    int S ranges 7 .. 15 (run-time constant defined at startup).
    int C ranges 2 .. 13 (defined by caller for each call).
    int N ranges 0 .. 2000 (values cannot be predicted by caller).
    …any N==0 in a set aborts the generation of that set (S—)
    …if all...
    See more | Go to post

  • Chris3020
    replied to C Validating >=1 numeric argv[]
    in C
    ...sorry for radio-silence: some stuff came up in the real-world.

    Turned out that I was the only user for the C-version of the utility!
    A friend asked for something to calculate the product of the first N prime-numbers after some number X.
    ...run-times of a few micro-seconds did not overcome his dislike of the command-line.
    Once the algorithm was ironed out in C it got translated into VBA! He's happier with that....
    See more | Go to post

    Leave a comment:


  • Chris3020
    replied to C Validating >=1 numeric argv[]
    in C
    @Luuk

    I admit to being crazy, but I'm not quite crazy enough to devote 1000 lines of code to input validation for an integer!
    Was just looking for a neat way that didn't involve a huge (1000 lines...) library designed to handle every nit-picky facet of argv[] parsing.

    Since posting, I remembered abs() ...doh!

    Input validation is now simple:
    Code:
      unsigned this = abs(atoi(argv[2]));
    ...
    See more | Go to post
    Last edited by Chris3020; Aug 17 '22, 07:23 PM. Reason: delete un wanted <space>

    Leave a comment:


  • Chris3020
    started a topic C Validating >=1 numeric argv[]
    in C

    C Validating >=1 numeric argv[]

    I have a simple utility expecting three command-line arguments:
    ./myutil -k 123 45

    argv[1] is a text flag of some sort: memcmp() against allowed flags; works!

    argv[2] and argv[3] need to sit in unsigned 32-bit integers and are required to be >= 1.

    For now I'm using:
    strstr() to look for "-" ...and assuming absence of "-" means +ve number-as-string.
    atoi() to...
    See more | Go to post

  • @zmbd:
    I think we leave it as is :)

    +1 vote. Chris
    See more | Go to post

    Leave a comment:


  • silly error:

    Code:
    static void dict_free(void) {
      for (int i = 0; i < MAX_WORDLEN; ++i)...  // should be:  i < MAX_WORDLEN[B]+1[/B];
    See more | Go to post
    Last edited by zmbd; May 8 '22, 10:30 PM.

    Leave a comment:


  • Bah!
    It just happened again:

    open page: missing some (not all) underscores!
    refresh page: hey ho, underscores are back!
    eh?

    Is it me?
    MacBook Air (M1 2020)
    MacOS BigSur 11.6.5
    Safari 15.4 (16613.1.17.1.1 3, 16613)
    // this is not the appropriate forum in which to discuss the most absurd version nomenclature on the planet!

    Or you?

    Chris
    See more | Go to post

    Leave a comment:


  • !!ASHAMED!! that you found 30 residual <tab> ...I went through line-by-line manually replacing <tab> with <space><space >.
    I was using Mac TextEdit for this exercise (an app that I don't normally use, so I didn't try auto find/replace).
    My only hope for salvation is that those errant <tab> were between meaningful char and \n.

    !!BAFFLED!! by the underscores.
    Looking again now, they...
    See more | Go to post

    Leave a comment:


  • Thanks zmbd.
    Had a go at fixing the <tab> issue.
    Chris

    ...just noticed: code seems to have dropped some underscores! between copy and paste.
    ...ah no! some underscores don't show, but copy from posted code to text-editor and they are back again.
    [#r!§
    See more | Go to post
    Last edited by Chris3020; May 8 '22, 09:39 AM. Reason: "disappeared" underscores! ...are actually (invisibly) there.

    Leave a comment:


  • Apparently I can't edit stuff inside code ... /code tags.
    Ho hum!

    Chris
    See more | Go to post

    Leave a comment:


  • mmm; straight copy/paste code from BBEdit is __NOT__ a good idea: the <tab>s went crazy. Sorry!
    ...will try to edit!
    See more | Go to post

    Leave a comment:


  • I went with 2)
    It was very close to my "latest effort" above.

    I didn't like the extra 8 bytes per word in dicts[] of 1), though it was "pretty".

    Thanks for all your suggestions, Chris

    Current test rig looks like this:

    []
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_WORDLEN 10 // maximum
    ...
    See more | Go to post
    Last edited by zmbd; May 8 '22, 03:16 PM. Reason: [Chris3020[Reason: kill all the wretched <tab>s]] [Z{had a go at it - removed another 30 tabs not sure about anu missing _ }]

    Leave a comment:


  • Mmmmmm interesting!
    **words just has to be more correct in the struct: I have a dozens of variations using ** (and hundreds of errors/warnings to go with).
    I will study (and experiment with) your stuff over the weekend.
    Thank you!
    Chris

    My latest effort is below. It still uses *words in the struct, it compiles, it even works (...but it is not pretty):
    Code:
    #include <stdio.h>
    #include
    ...
    See more | Go to post

    Leave a comment:


  • Thanks for the suggestion ...sorry, not tested yet - but it makes sense.
    Will experiment tomorrow morning.

    Is there no way to get this done with a single malloc per dicts[n] ?
    ...many thousands of malloc() seems less "efficient" .

    All this happens in a "setup-phase", so it doesn't need to be fast ...run-times for the application that uses dicts[] can be seconds to hours depending on...
    See more | Go to post

    Leave a comment:


  • 3D Array of (Array of Strings) -- my malloc attempts fail miserably

    I'm trying to make a sort-of-dictionary (don't think "hashmap" when you see that d-word, my dictionary is just simple lists of words in a 3D array)

    Code:
    #define MAX_WORDLENGTH 20
    
    struct dict{
    	int wordcount;
    	char **words;
    };
    
    struct dict dicts[MAX_WORDLENGTH + 1]; // +1 to get an index for MAX_WORDLENGTH
    e.g. dicts[7] is the struct dict for 7-letter words (7 excludes...
    See more | Go to post
No activity results to display
Show More
Working...