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...
User Profile
Collapse
-
Autoscale large integer calculations to avoid overflow
-
...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.... -
@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]));
Leave a comment:
-
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... -
-
silly error:
Code:static void dict_free(void) { for (int i = 0; i < MAX_WORDLEN; ++i)... // should be: i < MAX_WORDLEN[B]+1[/B];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?
ChrisLeave 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...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!§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!
ChrisLeave a comment:
-
mmm; straight copy/paste code from BBEdit is __NOT__ a good idea: the <tab>s went crazy. Sorry!
...will try to edit!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
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
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...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)
e.g. dicts[7] is the struct dict for 7-letter words (7 excludes...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
No activity results to display
Show More
Leave a comment: