449,149 Members | 1,049 Online
Need help? Post your question and get tips & solutions from a community of 449,149 IT Pros & Developers. It's quick & easy.

# decrementing arrays

 P: n/a I don't know if I'll need pointers for this or not. I wants numbers 10^16. Like a credit card 16 digits of possible 10 numbers, so I guess that would be 10^16. So I have int num [16][10]; These are of course declared and not initialized. Now I want to initialize them all with '\0'. That I'm guessing would involve while ( --). Or maybe for. Would pointers be involved in this? With this excercise I would learn working with multi-dimensional arrary, maybe pointers and initializing to zero. I tried to do this using putchar so I obviously don't know what to do. Can someone help me learn this? Bill ----- "You Big Dummy" -- Fred Sanford Jun 27 '08 #1
82 Replies

 P: n/a Bill Cunningham wrote: I don't know if I'll need pointers for this or not. I wants numbers 10^16. Like a credit card 16 digits of possible 10 numbers, Your question is not clear. Do you want an integer type that can store values upto 1e16? Do you want an array of 16 integers each of which can hold a ten digit decimal value? so I guess that would be 10^16. So I have int num [16][10]; These are of course declared and not initialized. Now I want to initialize them all with '\0'. Why? Why not just zero? That I'm guessing would involve while ( --). Or maybe for. Would pointers be involved in this? Why should they be explicitly involved. With this excercise I would learn working with multi-dimensional arrary, maybe pointers and initializing to zero. I tried to do this using putchar so I obviously don't know what to do. Can someone help me learn this? So you want to set that array to zero? Do: for (int i = 0; i < 16; i++) for (int j = 0; j < 10; j++) num[i][j] = 0; Jun 27 '08 #2

 P: n/a "santosh" Jun 27 '08 #3

 P: n/a "santosh"

 P: n/a "Bill Cunningham"

 P: n/a "Ben Bacarisse" Not for initialising: int num[16][10] = {'\0'}; and it is done. Are you sure Ben? I am not the one to be questioning you but I was told or atleast my understanding from the past has been to to this. Ok char char num[10]={0,0,0,0,0,0,0,0,0,0}; but char num[10]={'\0'}; Is certainly easier. Bill ----- "You big dummy" --Fred Sanford Jun 27 '08 #6

 P: n/a What if I wanted to look at the value of one element in the array. One dimensional and multi dimensional? printf("%c\n",num[5]); ? Bill ----- "You big dummy" --Fred Sanford Jun 27 '08 #7

 P: n/a "Bill Cunningham"

 P: n/a Bill Cunningham wrote: > "santosh" So you want to set that array to zero? Do: for (int i = 0; i < 16; i++) for (int j = 0; j < 10; j++) num[i][j] = 0; Why is there a =0 after num [i][j] ? Because it's the same as using '\0' and more logical. Use '\0' for char arrays and 0 for arrays of short, int, long, long long etc., and use NULL to initialise arrays of pointers. Jun 27 '08 #9

 P: n/a "Richard"

 P: n/a "Bill Cunningham" "Bill Cunningham" >Not for initialising:int num[16][10] = {'\0'};and it is done. Are you sure Ben? Yes. And, by the way, to set to 0 later you don't need a loop: memset(num, 0, sizeof num); will do it if num is an array of integer types (chars, ints, etc) but check back here if num is a parameter -- that complicates things. -- Ben. Jun 27 '08 #11

 P: n/a "Bill Cunningham"

 P: n/a "Bill Cunningham" You surprise me more and more. Is that good or bad :-) >Firstly what you want is a single credit card number. Which is 16 digitsof 0-9.I doubt very very much you are doing any calculations with this number.So simply store it as a string : zero terminated or not. What do you mean zero terminated or not? Doesn't C stick \0 automatically ? The only exception I know is strlen. Bill ----- "You big dummy" --Fred Sanford I mean you might not need it to be zero terminated since you know its length is always 16. But better to be I dare say in your code and makes it easier for display. But I guess you understood what I sad? Solving a problem is all about analysing the problem. And you are making problems there for yourself that are not there. All that 10^16 stuff was nonsense. Jun 27 '08 #13

 P: n/a "Ben Bacarisse"

 P: n/a "Bill Cunningham" So simply store it as a string : zero terminated or not. What do you mean zero terminated or not? Doesn't C stick \0 automatically ? The only exception I know is strlen. You're right, and Richard is wrong. A string is zero-terminated *by definition*. I think what Richard meant is to use an array of char. The array can be zero-terminated (making it a string), or not zero-terminated (in which case it's not a string). I'm not convinced a character array is the best approach; I'll post a separate followup. -- Keith Thompson (The_Other_Keith) ks***@mib.org Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Jun 27 '08 #15

 P: n/a "Bill Cunningham" Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Jun 27 '08 #16

 P: n/a Keith Thompson "Richard" >So simply store it as a string : zero terminated or not. What do you mean zero terminated or not? Doesn't C stick \0automatically ? The only exception I know is strlen. You're right, and Richard is wrong. A string is zero-terminated *by definition*. Yes. I should have been more careful with my words and guessed someone would pick it apart. One tends to assume a certain literacy with C would allow it to pass. I think what Richard meant is to use an array of char. The array can be zero-terminated (making it a string), or not zero-terminated (in which case it's not a string). Correct. > I'm not convinced a character array is the best approach; I'll post a separate followup. Jun 27 '08 #17

 P: n/a "Bill Cunningham" I can't suggest what you should use, because I don't know what youwant to do. I used %c because it was suggested I use an array of type char. If char num [10][17] contained different numbers how could I view a specific element's value of the array using printf? You have changed the base type of num from int to char and the 17 suggests that you have taken my advice that each element will have 16 digits and a null. If you do that, each one is a string so you can print an element using %s: printf("Number is %s\n", num[5]); -- Ben. Jun 27 '08 #18

 P: n/a Bill Cunningham wrote: "Ben Bacarisse" I can't suggest what you should use, because I don't know what youwant to do. I used %c because it was suggested I use an array of type char. If char num [10][17] contained different numbers how could I view a specific element's value of the array using printf? I think that might answer all my questions if I could know this. #include #include #define NUM_CARDS 10 #define CARD_ID_LENGTH 16 int main(void) { char card_nos[NUM_CARDS][CARD_ID_LENGTH + 1]; char *example_num = "1234567890123456"; int i; for (i = 0; i < NUM_CARDS; i++) strcpy(card_nos[i], example_num); /* print numbers with %s specifier */ for (i = 0; i < NUM_CARDS; i++) printf("card %d: %s\n", i, card_nos[i]); /* print numbers with a precision modifier to exactly 16 characters. This method works even if the char sequence is not null terminated */ for (i = 0; i < NUM_CARDS; i++) printf("card %d: %.16s\n",i,card_nos[i]); return 0; } Jun 27 '08 #21

 P: n/a santosh "Ben Bacarisse" >I can't suggest what you should use, because I don't know what youwant to do. I used %c because it was suggested I use an array of type char. Ifchar num [10][17] contained different numbers how could I view aspecific element's value of the array using printf? I think that mightanswer all my questions if I could know this. #include #include #define NUM_CARDS 10 #define CARD_ID_LENGTH 16 int main(void) { char card_nos[NUM_CARDS][CARD_ID_LENGTH + 1]; char *example_num = "1234567890123456"; int i; for (i = 0; i < NUM_CARDS; i++) strcpy(card_nos[i], example_num); /* print numbers with %s specifier */ for (i = 0; i < NUM_CARDS; i++) printf("card %d: %s\n", i, card_nos[i]); /* print numbers with a precision modifier to exactly 16 characters. This method works even if the char sequence is not null terminated */ for (i = 0; i < NUM_CARDS; i++) printf("card %d: %.16s\n",i,card_nos[i]); return 0; } What do you think this does? Do you think this helps Bill in any way whatsoever? It is total nonsense - you have 10 card numbers. He was talking about having 10^16 of them! The 10 he was talking about was base 10 e.g 0-9. A right answer to a wrong question doesn't get any points I am afraid. You would be better advised to help Bill analyse what his problem really is. Currently he doesn't have a clue. Jun 27 '08 #22

 P: n/a "Richard"

 P: n/a "Bill Cunningham" You really have no idea of what you want. How on earth can you"randomly" generate all 10^16 numbers? Why not just generate them in onelinear run? One at a time yes. If I am understanding correctly. A 16 digit number generated off the system time. Bill LOL! What a guy you are Bill! You crack me up. You can not be serious! "One linear run" 0-1-2-3-4-5-6-7-8-9-10-11 etc etc etc. You really haven't a clue what you want do you? Hint : requirements analysis in 90% of the battle. Jun 27 '08 #24

 P: n/a Bill Cunningham wrote: I want to use srand and rand to randomly generate 10**16 numbers like that on a credit card. I will learn more about random numbers that way in C. Why numbers like that on a credit card? They seem to be pretty unique numbers since some many of them are out there. Here is some code. After compiling invoke the program with an argument specifying the amount of random number you want. #include #include #include #include #define CCARD_NUM_LENGTH 16 int main(int argc, char **argv) { char buf[CCARD_NUM_LENGTH + 1]; unsigned long nos, ctr; int rndno, chunk; if (argc 1) { nos = strtoul(argv[1], NULL, 0); srand((unsigned int)time(NULL)); } else return EXIT_FAILURE; for (ctr = 0; ctr < nos; ctr++) { for (chunk = 0; chunk < 4; chunk++) { do { rndno = rand(); } while (rndno < 1000 || rndno 9999); sprintf(buf+chunk*4, "%d", rndno); } printf("%lu: %s\n", ctr, buf); } return 0; } Jun 27 '08 #25

 P: n/a "Richard"

 P: n/a "santosh" #include #include #include #define CCARD_NUM_LENGTH 16 int main(int argc, char **argv) { char buf[CCARD_NUM_LENGTH + 1]; unsigned long nos, ctr; int rndno, chunk; if (argc 1) { nos = strtoul(argv[1], NULL, 0); srand((unsigned int)time(NULL)); } else return EXIT_FAILURE; for (ctr = 0; ctr < nos; ctr++) { for (chunk = 0; chunk < 4; chunk++) { do { rndno = rand(); } while (rndno < 1000 || rndno 9999); sprintf(buf+chunk*4, "%d", rndno); } printf("%lu: %s\n", ctr, buf); } return 0; } Thanks Santosh for your time and effort. I will compile and run this. This is a first for me too. Never used sprintf. Bill -- "You big dummy" --Fred Sanford www.youtube.com/watch?v=tOuFA0jNaac Jun 27 '08 #27

 P: n/a "Bill Cunningham" LOL! What a guy you are Bill! You crack me up. You can not be serious!"One linear run"0-1-2-3-4-5-6-7-8-9-10-11 etc etc etc. Ok let's use big words can you do that geometrically exponentially or logarithmically. No wonder some call you a troll. I tollerate you because I think you do know what your talking about and you do help me sometimes. Bill You can not see that the only sure way to generate your 10^16 numbers is to just loop from 0 to MAX? Once more for the hard of thinking : sort out what you want to do. Then we can go and do it. Throwing code at a moving target is not wise. Jun 27 '08 #28

 P: n/a "Bill Cunningham" Here is some code. After compiling invoke the program with an argumentspecifying the amount of random number you want.#include #include #include #include #define CCARD_NUM_LENGTH 16int main(int argc, char **argv) { char buf[CCARD_NUM_LENGTH + 1]; unsigned long nos, ctr; int rndno, chunk; if (argc 1) { nos = strtoul(argv[1], NULL, 0); srand((unsigned int)time(NULL)); } else return EXIT_FAILURE; for (ctr = 0; ctr < nos; ctr++) { for (chunk = 0; chunk < 4; chunk++) { do { rndno = rand(); } while (rndno < 1000 || rndno 9999); sprintf(buf+chunk*4, "%d", rndno); } printf("%lu: %s\n", ctr, buf); } return 0;} Thanks Santosh for your time and effort. I will compile and run this. This is a first for me too. Never used sprintf. Bill Don't compile it. Read the code and figure out it does and then think if its what you want. Either that or give up. Jun 27 '08 #29

 P: n/a Bill Cunningham wrote: "santosh" #include void help(void) { printf("Usage: Program Symbol_set Sequence_length\n"); exit(EXIT_FAILURE); } void print_permutations (char *sym_set, char *seq_buf, unsigned seq_len, unsigned seq_pos) { unsigned i; for (i = 0; sym_set[i] != '\0'; i++) { seq_buf[seq_pos] = sym_set[i]; if (seq_len - seq_pos == 1) printf("%.*s\n", seq_len, seq_buf); else print_permutations(sym_set, seq_buf, seq_len, seq_pos+1); } } int main(int argc, char **argv) { unsigned seq_len, seq_pos = 0; char *seq_buf; if (argc < 3) help(); if (sscanf(argv[2], "%u", &seq_len) != 1 || seq_len < 1) help(); if ((seq_buf = malloc(seq_len)) == 0) { fputs("Malloc failed.\n", stderr); exit(EXIT_FAILURE); } print_permutations(argv[1], seq_buf, seq_len, seq_pos); } /* end permute.c */ Jun 27 '08 #30

 P: n/a santosh "santosh" Thanks Santosh for your time and effort. I will compile and run this. This is a first for me too. Never used sprintf. You also mentioned that you wanted a program for all possible permutations of a 16 character string (in this case a credit card number). Here is code for generating permutations of a given set of symbols for a given length. Which totally ignores any rules which may, or may not, exist for legal permutations for a valid credit card number. Throwing code at Bill is not helping him formulate his needs. It is just dazzling him. Why do you do this? > Invoke this program as: program symbol_set sequence_length where 'symbol_set' should be the set of characters which can appear in the permutations and 'sequence_length' is the maximum length of the permutations. For example: \$ ./permute 123 3 111 112 113 121 122 123 131 132 133 211 212 213 221 222 223 231 232 233 311 312 313 321 322 323 331 332 333 \$ To compute all possible permutations of a 16 digit decimal number you can do: permute 0123456789 16 It will take a _very_ _very_ long time to finish. /* permute.c */ #include #include void help(void) { printf("Usage: Program Symbol_set Sequence_length\n"); exit(EXIT_FAILURE); } void print_permutations (char *sym_set, char *seq_buf, unsigned seq_len, unsigned seq_pos) { unsigned i; for (i = 0; sym_set[i] != '\0'; i++) { seq_buf[seq_pos] = sym_set[i]; if (seq_len - seq_pos == 1) printf("%.*s\n", seq_len, seq_buf); else print_permutations(sym_set, seq_buf, seq_len, seq_pos+1); } } int main(int argc, char **argv) { unsigned seq_len, seq_pos = 0; char *seq_buf; if (argc < 3) help(); if (sscanf(argv[2], "%u", &seq_len) != 1 || seq_len < 1) help(); if ((seq_buf = malloc(seq_len)) == 0) { fputs("Malloc failed.\n", stderr); exit(EXIT_FAILURE); } print_permutations(argv[1], seq_buf, seq_len, seq_pos); } /* end permute.c */ And is of course a load of nonsense for his requirement. Since you do no rule processing, and we are looking at base 10, all he has to do is use a bignum library and loop from 0 to 10^16-1. Bill is trying to learn C. How do you think throwing a load of complicated , recursive code with zero comments will in *any* way help him? he has already shown that he doesn't read any code he is given. In his own words "i,j and k just confuse things" or whatever it was. Jun 27 '08 #31

 P: n/a Richard wrote: santosh Bill Cunningham wrote: >>"santosh" > Thanks Santosh for your time and effort. I will compile and run this. This is a first for me too. Never used sprintf. You also mentioned that you wanted a program for all possiblepermutations of a 16 character string (in this case a credit cardnumber). Here is code for generating permutations of a given set ofsymbols for a given length. Which totally ignores any rules which may, or may not, exist for legal permutations for a valid credit card number. He didn't mention any rules whatsoever. In fact, he isn't making much sense at all, so I can only have a stab in the dark. Throwing code at Bill is not helping him formulate his needs. He has shown that he is unable to specify his requirements rigorously unless he is prompted with considerable guesswork. It is just dazzling him. Why do you do this? Well, if he cannot understand the code, he can always ignore. Asking questions back at him (as you are doing) seems to only confuse him further. Jun 27 '08 #32

 P: n/a santosh said: To compute all possible permutations of a 16 digit decimal number you can do: permute 0123456789 16 It will take a _very_ _very_ long time to finish. Permuting doesn't allow any one element to be used more than once, but from your sample output it seems that you meant counting rather than permuting. If we assume that the machine can iterate through 1e9 counts per second (not including display or adding to storage), it will take 115 days and some hours to reach 1e16. Coulda been worse... -- Richard Heathfield Email: -http://www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Jun 27 '08 #33

 P: n/a Richard Heathfield wrote: santosh said: >To compute all possible permutations of a 16 digit decimal number youcan do: permute 0123456789 16It will take a _very_ _very_ long time to finish. Permuting doesn't allow any one element to be used more than once, but from your sample output it seems that you meant counting rather than permuting. Yes, you're right. If we assume that the machine can iterate through 1e9 counts per second It takes more than four seconds here. (not including display or adding to storage), it will take 115 days and some hours to reach 1e16. Coulda been worse... Yes. It would be helpful if Bill could state in plain language what exactly he is trying to do. His disjointed thoughts and statements often make very little sense. Jun 27 '08 #34

 P: n/a santosh santosh >Bill Cunningham wrote:"santosh" >Throwing code at Bill is not helping him formulate his needs. He has shown that he is unable to specify his requirements rigorously unless he is prompted with considerable guesswork. And throwing answers to non questions helps? > >It is just dazzling him. Why do you do this? Well, if he cannot understand the code, he can always ignore. Asking questions back at him (as you are doing) seems to only confuse him further. I am asking him to STOP: THINK: CONSIDER. One thing is for sure. He certainly does not want an array of 10^16 credit card numbers. Jun 27 '08 #35

 P: n/a "Bill Cunningham" #define CCARD_NUM_LENGTH 16 ... Thanks Santosh for your time and effort. I will compile and run this. This is a first for me too. Never used sprintf. Here's some more code. A bit simpler. It displays all possible credit-card-style numbers, legal or not (including all of mine so please take care distributing the output): #include #include int main(void) { #define digits 16 char number[digits]={0}; int i; while (1) { for (i=0; i=0; --i) { number[i]+=1; if (number[i]<10) break; if (i==0) exit(0); number[i]-=10; }; }; } Jun 27 '08 #36

 P: n/a Bartc wrote: > "Bill Cunningham" "santosh" >#define CCARD_NUM_LENGTH 16 .. > Thanks Santosh for your time and effort. I will compile and run this.This is a first for me too. Never used sprintf. Here's some more code. A bit simpler. It displays all possible credit-card-style numbers, legal or not (including all of mine so please take care distributing the output): #include #include int main(void) { #define digits 16 char number[digits]={0}; int i; while (1) { for (i=0; i=0; --i) { number[i]+=1; if (number[i]<10) break; if (i==0) exit(0); number[i]-=10; }; }; } You don't need the three null statements after the while and for loops. Jun 27 '08 #37

 P: n/a "santosh"

 P: n/a "santosh" [snip] It will take a _very_ _very_ long time to finish. /* permute.c */ #include #include void help(void) { printf("Usage: Program Symbol_set Sequence_length\n"); exit(EXIT_FAILURE); } void print_permutations (char *sym_set, char *seq_buf, unsigned seq_len, unsigned seq_pos) { unsigned i; for (i = 0; sym_set[i] != '\0'; i++) { seq_buf[seq_pos] = sym_set[i]; if (seq_len - seq_pos == 1) printf("%.*s\n", seq_len, seq_buf); else print_permutations(sym_set, seq_buf, seq_len, seq_pos+1); } } int main(int argc, char **argv) { unsigned seq_len, seq_pos = 0; char *seq_buf; if (argc < 3) help(); if (sscanf(argv[2], "%u", &seq_len) != 1 || seq_len < 1) help(); if ((seq_buf = malloc(seq_len)) == 0) { fputs("Malloc failed.\n", stderr); exit(EXIT_FAILURE); } print_permutations(argv[1], seq_buf, seq_len, seq_pos); } /* end permute.c */ Wow. I think Richard says later in the thread that this kind of code is dazzling and it is. Maybe something simple like the permutations of 1234. That can be done by hand but how does C do it is the question. Bill -- "You big dummy" --Fred Sanford www.youtube.com/watch?v=tOuFA0jNaac Jun 27 '08 #39

 P: n/a "Richard Heathfield"

 P: n/a "santosh" Here's some more code. A bit simpler. It displays all possiblecredit-card-style numbers, legal or not (including all of mine soplease take care distributing the output):#include #include int main(void) {#define digits 16char number[digits]={0};int i;while (1) { for (i=0; i=0; --i) { number[i]+=1; if (number[i]<10) break; if (i==0) exit(0); number[i]-=10; };};} While(1). What's that mean? Puts (""); What's this function doing to your code? I have to break this down line by line to understand what's going on. Bill -- "You big dummy" --Fred Sanford www.youtube.com/watch?v=tOuFA0jNaac Jun 27 '08 #41

 P: n/a Bill Cunningham said: "santosh" >>>while (1) { > While(1). What's that mean? Nothing, but he didn't write While(1). He wrote while (1). Your lesson for today is: C is case-sensitive. -- Richard Heathfield Email: -http://www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Jun 27 '08 #42

 P: n/a "Richard Heathfield"

 P: n/a Bill Cunningham said: > "Richard Heathfield" Nothing, but he didn't write While(1). He wrote while (1).Your lesson for today is: C is case-sensitive. I understand that Richard. Good. I was beginning a sentence in English. That's good too, insofar as it improves clarity - but if you can get /more/ clarity by breaking the rules of English, that's better still. Because C is case-sensitive and because it matters and because it confuses some people, it's generally worth at least considering allowing C rules to trump English rules. Now let's look at what while(1) means. The while keyword takes a parenthesised expression, which is evaluated to determine whether the loop body will be executed. If the expression evaluates to 0, the loop body will not be executed - otherwise, it will. Some examples: int x = 0; while(x < 10) { printf("%d\n", x++); } This loop body will execute 10 times. After the tenth time, the value of x will be 10. If x is 10, then x < 10 is not true, so it's false, so it evaluates to 0, so the loop body stops. char foo[] = "Now is the time for all good men"; char *p = strtok(foo, " "); while(p != NULL) { printf("%.2s ", p); p = strtok(NULL, " "); } This loop body will execute eight times. The output will be: No is th ti fo al go me and on the eighth iteration, strtok will return NULL, p will be set to NULL, and the expression p != NULL will be false, i.e. have the value 0. So the loop will stop there. Now for while(1): The expression 1 evaluates, would you believe, to 1 - so the loop body is always executed. It's an idiom that I don't have a lot of time for - the whole point of the expression is to determine when the loop stops, and confusing the issue with break statements is not an idea that I like. Nevertheless, it is a valid and even common C idiom, and you should at least be able to understand it, even if (as I hope) you are never unwise enough to use it. -- Richard Heathfield Email: -http://www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Jun 27 '08 #44

 P: n/a On Sun, 22 Jun 2008 01:28:15 GMT, "Bill Cunningham" wrote: snip >Puts ("");What's this function doing to your code? I have to break this down line byline to understand what's going on. You claimed in a previous post that you have a reference manual. Look up puts. What does puts do with its argument? What else does it do independent of its argument? What is the net effect of these two activities given the original code of puts(""); which you misquoted? Remove del for email Jun 27 '08 #45

 P: n/a "Bill Cunningham" >while (1) { >> puts(""); While(1). What's that mean? Puts (""); What's this function doing to your code? I have to break this down line by line to understand what's going on. while(1) is one way of writing an endless loop in C. I use this sort of loop a lot. It gives me extra thinking time while I figure out how to break out of the loop, which is easier if not constrained to be at the beginning or end. So you start with this: while (1) { } then fill in the space between { and }. In this case, you want to stop going when the output shows 9999 9999 9999 9999. Ie. you're just about to roll over to 0000 0000 0000 0000. (Although, for 16 digits, that would have taken some 3000000 years on my computer, I of course tested with fewer digits first.) puts("") I assumed was a way of generating a new-line which is easier to type than printf("\n") (however fputs("",F) is *not* equivalent to fprintf(F,"\n"), as I've found out). -- Bartc Jun 27 '08 #46

 P: n/a Bartc wrote: > "Bill Cunningham" >>while (1) { >>> puts(""); >While(1). What's that mean?Puts ("");What's this function doing to your code? I have to break this downline by line to understand what's going on. while(1) is one way of writing an endless loop in C. I use this sort of loop a lot. It gives me extra thinking time while I figure out how to break out of the loop, which is easier if not constrained to be at the beginning or end. So you start with this: while (1) { } then fill in the space between { and }. In this case, you want to stop going when the output shows 9999 9999 9999 9999. Ie. you're just about to roll over to 0000 0000 0000 0000. (Although, for 16 digits, that would have taken some 3000000 years on my computer, I of course tested with fewer digits first.) Interesting. Can you tell me how you calculated that figure of 3000000 years? For me on this machine the calculation came to just over 115 days. puts("") I assumed was a way of generating a new-line which is easier to type than printf("\n") (however fputs("",F) is *not* equivalent to fprintf(F,"\n"), as I've found out). Recalls to mind that famous quote: "C is quirky, flawed, and an enormous success." :-) Jun 27 '08 #47

 P: n/a "Bill Cunningham" "santosh" >Here's some more code. A bit simpler. It displays all possiblecredit-card-style numbers, legal or not (including all of mine soplease take care distributing the output):#include #include int main(void) {#define digits 16char number[digits]={0};int i;while (1) { for (i=0; i=0; --i) { number[i]+=1; if (number[i]<10) break; if (i==0) exit(0); number[i]-=10; };};} The iterations statement blocks above are alittle too much to chew in one bite. Can you break them down alittle for me. Maybe instead of 16 digits we can go with 4 or 5 that's easy to show permutations of by hand, C code should be simple. Bill -- "You big dummy" --Fred Sanford www.youtube.com/watch?v=tOuFA0jNaac Jun 27 '08 #48

 P: n/a "santosh" then fill in the space between { and }. In this case, you want to stopgoing when the output shows 9999 9999 9999 9999. Ie. you're just aboutto roll over to 0000 0000 0000 0000. (Although, for 16 digits, thatwould have taken some 3000000 years on my computer, I of course testedwith fewer digits first.) Interesting. Can you tell me how you calculated that figure of 3000000 years? For me on this machine the calculation came to just over 115 days. Did you allow for any output? My console can scroll text at some 700 lines per second (which I rounded up to 1000; no need to show how slow my machine is..) So showing 1e16 lines would take 1e13 seconds. Since there are only about 3e7 seconds in a year, there must be something amiss in your calculations, or have a super-fast display. > >puts("") I assumed was a way of generating a new-line which is easierto type than printf("\n") (however fputs("",F) is *not* equivalent tofprintf(F,"\n"), as I've found out). Recalls to mind that famous quote: "C is quirky, flawed, and an enormous success." :-) That's good, not just me thinking it was odd. -- Bartc Jun 27 '08 #49

 P: n/a "Bill Cunningham" >>#include #include int main(void) {#define digits 16char number[digits]={0};int i;while (1) { for (i=0; i=0; --i) { number[i]+=1; if (number[i]<10) break; if (i==0) exit(0); number[i]-=10; };};} The iterations statement blocks above are alittle too much to chew in one bite. Can you break them down alittle for me. Maybe instead of 16 digits we can go with 4 or 5 that's easy to show permutations of by hand, C code should be simple. Try changing the digits macro to 2 (or maybe just 1). The while loop shows one combination per loop. The first for loop just prints the digits in array number[]. The second for loop does the real work: adding one to the number, starting from the right, and propagating any carry leftwards. -- Bartc Jun 27 '08 #50

82 Replies

### This discussion thread is closed

Replies have been disabled for this discussion.