decrementing arrays

S

santosh

Bill said:
I can't suggest what you should use, because I don't know what you
want 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 <stdio.h>
#include <string.h>

#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, example_num);

/* print numbers with %s specifier */
for (i = 0; i < NUM_CARDS; i++)
printf("card %d: %s\n", i, card_nos);

/* 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);

return 0;
}
 
R

Richard

santosh said:
Bill said:
I can't suggest what you should use, because I don't know what you
want 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 <stdio.h>
#include <string.h>

#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, example_num);

/* print numbers with %s specifier */
for (i = 0; i < NUM_CARDS; i++)
printf("card %d: %s\n", i, card_nos);

/* 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);

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.
 
B

Bill Cunningham

Richard said:
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 one
linear run?

One at a time yes. If I am understanding correctly. A 16 digit number
generated off the system time.

Bill
 
R

Richard

Bill Cunningham said:
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.
 
S

santosh

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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#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;
}
 
B

Bill Cunningham

Richard said:
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
 
B

Bill Cunningham

santosh said:
Here is some code. After compiling invoke the program with an argument
specifying the amount of random number you want.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#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
 
R

Richard

Bill Cunningham said:
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.
 
R

Richard

Bill Cunningham said:
santosh said:
Here is some code. After compiling invoke the program with an argument
specifying the amount of random number you want.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#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

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.
 
S

santosh

Bill said:

[snip code for generating "random" 16 character strings]
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.

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 <stdio.h>
#include <stdlib.h>

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 != '\0'; i++) {
seq_buf[seq_pos] = sym_set;
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 */
 
S

santosh

Richard said:
santosh said:
Bill said:

[snip code for generating "random" 16 character strings]
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.

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.

<snip>
 
S

santosh

Richard said:
santosh said:



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.
 
R

Richard

santosh said:
Richard said:
santosh said:
Bill Cunningham wrote:

[snip code for generating "random" 16 character strings]

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.

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.

This is my point. You are throwing code at someone who (a) has no idea
what it does and (b) has no idea what he wants.
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?
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.

<snip>

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.
 
B

Bartc

...
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 <stdio.h>
#include <stdlib.h>

int main(void) {
#define digits 16
char number[digits]={0};
int i;

while (1) {

for (i=0; i<digits; ++i) {
printf("%1d",number);
if ((i&3)==3) printf(" ");
};
puts("");

for (i=digits-1; i>=0; --i) {
number+=1;
if (number<10) break;
if (i==0) exit(0);
number-=10;

};
};
}
 
S

santosh

Bartc said:
..
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 <stdio.h>
#include <stdlib.h>

int main(void) {
#define digits 16
char number[digits]={0};
int i;

while (1) {

for (i=0; i<digits; ++i) {
printf("%1d",number);
if ((i&3)==3) printf(" ");
};
puts("");

for (i=digits-1; i>=0; --i) {
number+=1;
if (number<10) break;
if (i==0) exit(0);
number-=10;

};
};
}


You don't need the three null statements after the while and for loops.
 
B

Bartc

You don't need the three null statements after the while and for loops.

OK that's cleared up one thing I've had difficulty with: compound statements
don't need to be terminated by the statement terminator ";".

-- Bartc
 
B

Bill Cunningham

santosh said:
[snip]

It will take a _very_ _very_ long time to finish.

/* permute.c */
#include <stdio.h>
#include <stdlib.h>

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 != '\0'; i++) {
seq_buf[seq_pos] = sym_set;
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
 
B

Bill Cunningham

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...

Dang that sounds like a line from Mr. Spock outta Star Trek. ;-)

Bill
 
B

Bill Cunningham

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 <stdio.h>
#include <stdlib.h>

int main(void) {
#define digits 16
char number[digits]={0};
int i;

while (1) {

for (i=0; i<digits; ++i) {
printf("%1d",number);
if ((i&3)==3) printf(" ");
};
puts("");

for (i=digits-1; i>=0; --i) {
number+=1;
if (number<10) break;
if (i==0) exit(0);
number-=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
 
B

Bill Cunningham

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. I was beginning a sentence in English.

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,118
Latest member
LatishaWhy
Top