By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,149 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
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;
Wow thanks Santosh. I've seen things like this in code but the i's j's k's
and so on start to blur after awhile and I just give up. So now I know what
that means. What about randomizing ? So all is set to zero. Would srand be
involved ?

Bill

-----
"You big Dummy" --Fred Sanford
>

Jun 27 '08 #3

P: n/a

"santosh" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
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] ?

Bill

-----
"You big dummy" --Fred Sanford
Jun 27 '08 #4

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
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];
That is an array of 16 arrays of 10 ints. To store ten things, each
of which is 16 elements long you would write:

T num[10][16];

where T is the element type. int is not the obvious choice here.
char is quite big enough to store a single digit. You will have
trouble if you think these arrays will behave like strings. To so
that you'd need 17 elements in each to allow for a null at the end of
even the longest ones.
These are of course declared and not initialized. Now I want to initialize
them all with '\0'.
That is an odd thing to initial an integer array with, but it works.
It is the same as 0.
That I'm guessing would involve while ( --). Or maybe
for. Would pointers be involved in this?
Not for initialising:

int num[16][10] = {'\0'};

and it is done.
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?
Are you sure you know enough about 1-dimensional arrays? I will be
hard to learn about arrays of arrays if the base concept is at all
hazy.

--
Ben.
Jun 27 '08 #5

P: n/a

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
"Bill Cunningham" <no****@nspam.comwrites:
Would pointers be involved in this?
>
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" <no****@nspam.comwrites:
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
You surprise me more and more.

Firstly what you want is a single credit card number. Which is 16 digits
of 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.

char CCNum[] ="0123456789012345"; /* zero terminated credit card number as a
string */

If you want to learn pointers simply go out and buy some and wave them
around.
Jun 27 '08 #8

P: n/a
Bill Cunningham wrote:
>
"santosh" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
>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" <rg****@gmail.comwrote in message
news:g3**********@registered.motzarella.org...

You surprise me more and more.
Is that good or bad :-)
Firstly what you want is a single credit card number. Which is 16 digits
of 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
Jun 27 '08 #10

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>"Bill Cunningham" <no****@nspam.comwrites:
Would pointers be involved in this?
>>
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" <no****@nspam.comwrites:
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]); ?
No. num[5] is an array-valued expression -- it will be converted to a
pointer to the first element. %c can't be used with an int * argument
(num was declared int num[16][10]).

I can't suggest what you should use, because I don't know what you
want to do.

--
Ben.
Jun 27 '08 #12

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Richard" <rg****@gmail.comwrote in message
news:g3**********@registered.motzarella.org...

>You surprise me more and more.

Is that good or bad :-)
>Firstly what you want is a single credit card number. Which is 16 digits
of 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" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
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.

Thanks

Bill
--
"You big dummy" --Fred Sanford

www.youtube.com/watch?v=tOuFA0jNaac


Jun 27 '08 #14

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Richard" <rg****@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
[...]
>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 <http://www.ghoti.net/~kst>
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" <no****@nspam.comwrites:
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?
Your subject header is "decrementing arrays". Nothing you're asking
about involves decrementing anything (which is fortunate, since
decrementing an array makes no sense). Why did you use the word
"decrementing"?

You wrote: "I wants numbers 10^16." That doesn't make any sense; I
can't even parse it. Since you later mention 16-digit credit card
numbers, I guess you mean you want to be able to represent 10**16
different values.

You're using ^ to denote exponentiation, which is ok if it's clear
from the context. (I prefer **, since it's harder to mistake for an
existing C operator.)

Your array "int num [16][10]" has 16*10 elements. That has nothing to
do with what you were asking about. You're just guessing.

There are a couple of obvious ways to represent 16-digit decimal
numbers.

First, 16 decimal digits correspond to just over 53 bits. If use an
integral type of more than 53 bits, you can represent the numbers
directly. This doesn't give you separate representations for each
digit; I don't know whether you need that or not.

Or you can represent them the way they'll be displayed, as an array of
characters (most conveniently as a string, which means you'll need 17
characters).

Which representation makes the most sense depends largely on what you
want to do with these numbers.

(Forget about your two-dimensional array; that's a blind alley.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
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 <ks***@mib.orgwrites:
"Bill Cunningham" <no****@nspam.comwrites:
>"Richard" <rg****@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
[...]
>>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*.
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" <no****@nspam.comwrites:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>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?
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

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>
Your subject header is "decrementing arrays". Nothing you're asking
about involves decrementing anything (which is fortunate, since
decrementing an array makes no sense). Why did you use the word
"decrementing"?
In my original post I thought while (--) and decrementing might be usesd
to accomplish what I wanted. I see I was wrong though. I learn so much from
this group when I explain my problems clearly.
You wrote: "I wants numbers 10^16." That doesn't make any sense; I
can't even parse it. Since you later mention 16-digit credit card
numbers, I guess you mean you want to be able to represent 10**16
different values.
Yes. All the possibilites of a credit card number.
You're using ^ to denote exponentiation, which is ok if it's clear
from the context. (I prefer **, since it's harder to mistake for an
existing C operator.)

Your array "int num [16][10]" has 16*10 elements. That has nothing to
do with what you were asking about. You're just guessing.

There are a couple of obvious ways to represent 16-digit decimal
numbers.

First, 16 decimal digits correspond to just over 53 bits. If use an
integral type of more than 53 bits, you can represent the numbers
directly.
But that's 16 decimal digits, for example, 1234 5678 9999 9999 on a
credit card. That's not all the possible permutations.

I want all permutations 10**16 randomly generated. This is something
I've never been able to catch onto. 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.

Bill

--
"You big dummy" --Fred Sanford

www.youtube.com/watch?v=tOuFA0jNaac
Jun 27 '08 #19

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>>
Your subject header is "decrementing arrays". Nothing you're asking
about involves decrementing anything (which is fortunate, since
decrementing an array makes no sense). Why did you use the word
"decrementing"?

In my original post I thought while (--) and decrementing might be usesd
to accomplish what I wanted. I see I was wrong though. I learn so much from
this group when I explain my problems clearly.
Have you determined what it is you want yet? People are running in and
answering the wrong question. If you can not formulate your requirements
then what do you have to program a solution?
>
>You wrote: "I wants numbers 10^16." That doesn't make any sense; I
can't even parse it. Since you later mention 16-digit credit card
numbers, I guess you mean you want to be able to represent 10**16
different values.

Yes. All the possibilites of a credit card number.
No you don't. Really. basically you would just have all numbers from 0
to 10^16-1. Why on earth would you want to store than in base 10 in
strings or integer arrays?
>
>You're using ^ to denote exponentiation, which is ok if it's clear
from the context. (I prefer **, since it's harder to mistake for an
existing C operator.)

Your array "int num [16][10]" has 16*10 elements. That has nothing to
do with what you were asking about. You're just guessing.

There are a couple of obvious ways to represent 16-digit decimal
numbers.

First, 16 decimal digits correspond to just over 53 bits. If use an
integral type of more than 53 bits, you can represent the numbers
directly.

But that's 16 decimal digits, for example, 1234 5678 9999 9999 on a
credit card. That's not all the possible permutations.
Do you have even the slightest inkling of what Keith meant by the bit
res for your encoding?
>
I want all permutations 10**16 randomly generated. This is something
I've never been able to catch onto. 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.

Bill
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?

Jun 27 '08 #20

P: n/a
Bill Cunningham wrote:
"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>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[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 <sa*********@gmail.comwrites:
Bill Cunningham wrote:
>"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
>>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[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" <rg****@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
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

--
"You big dummy" --Fred Sanford

www.youtube.com/watch?v=tOuFA0jNaac
Jun 27 '08 #23

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Richard" <rg****@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
>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
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:

<snip>
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;
}

Jun 27 '08 #25

P: n/a

"Richard" <rg****@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
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 big dummy" --Fred Sanford

www.youtube.com/watch?v=tOuFA0jNaac
Jun 27 '08 #26

P: n/a

"santosh" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
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
--
"You big dummy" --Fred Sanford

www.youtube.com/watch?v=tOuFA0jNaac
Jun 27 '08 #27

P: n/a
"Bill Cunningham" <no****@nspam.comwrites:
"Richard" <rg****@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
>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" <no****@nspam.comwrites:
"santosh" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
>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.

Jun 27 '08 #29

P: n/a
Bill Cunningham wrote:
"santosh" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
[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[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 <sa*********@gmail.comwrites:
Bill Cunningham wrote:
>"santosh" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...

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

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 <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[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 <sa*********@gmail.comwrites:
>Bill Cunningham wrote:
>>"santosh" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...

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

Jun 27 '08 #32

P: n/a
santosh said:

<snip>
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 <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #33

P: n/a
Richard Heathfield wrote:
santosh said:

<snip>
>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.
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 <sa*********@gmail.comwrites:
Richard wrote:
>santosh <sa*********@gmail.comwrites:
>>Bill Cunningham wrote:
"santosh" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org.. .

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

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

Jun 27 '08 #35

P: n/a

"Bill Cunningham" <no****@nspam.comwrote in message
news:1kY6k.44$IL6.41@trnddc04...
"santosh" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
>#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 <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[i]);
if ((i&3)==3) printf(" ");
};
puts("");

for (i=digits-1; 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" <no****@nspam.comwrote in message
news:1kY6k.44$IL6.41@trnddc04...
>"santosh" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
>>#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 <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[i]);
if ((i&3)==3) printf(" ");
};
puts("");

for (i=digits-1; 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" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...

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
Jun 27 '08 #38

P: n/a

"santosh" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
>
[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[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" <rj*@see.sig.invalidwrote in message
news:6Z******************************@bt.com...

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
--
"You big dummy" --Fred Sanford

www.youtube.com/watch?v=tOuFA0jNaac
Jun 27 '08 #40

P: n/a

"santosh" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
>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[i]);
if ((i&3)==3) printf(" ");
};
puts("");

for (i=digits-1; 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" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
<snip>
>>>
while (1) {
<snip>
>
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 <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #42

P: n/a


"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:wv*********************@bt.com...
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
--
"You big dummy" --Fred Sanford

www.youtube.com/watch?v=tOuFA0jNaac
Jun 27 '08 #43

P: n/a
Bill Cunningham said:
>

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:wv*********************@bt.com...
>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 <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"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" <no****@nspam.com>
wrote:
snip
>Puts ("");

What's this function doing to your code? I have to break this down line by
line 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" <no****@nspam.comwrote in message
news:PUh7k.866$9J.363@trnddc06...
>>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" <no****@nspam.comwrote in message
news:PUh7k.866$9J.363@trnddc06...
>>>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.)
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" <no****@nspam.comwrote in message
news:PUh7k.866$9J.363@trnddc06...
>
"santosh" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
>>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[i]);
if ((i&3)==3) printf(" ");
};
puts("");

for (i=digits-1; 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" <sa*********@gmail.comwrote in message
news:g3**********@registered.motzarella.org...
Bartc wrote:
>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.
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 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."
:-)
That's good, not just me thinking it was odd.

--
Bartc
Jun 27 '08 #49

P: n/a

"Bill Cunningham" <no****@nspam.comwrote in message
news:MLq7k.885$9J.635@trnddc06...
>>>#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[i]);
if ((i&3)==3) printf(" ");
};
puts("");

for (i=digits-1; 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.