decrementing arrays

B

Bill Cunningham

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
 
S

santosh

Bill said:
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[j] = 0;
 
B

Bill Cunningham

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[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
[/QUOTE]
 
B

Bill Cunningham

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[j] = 0;


Why is there a =0 after num [j] ?

Bill
 
B

Ben Bacarisse

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

Bill Cunningham

Ben Bacarisse said:
"Bill Cunningham" <[email protected]> writes:
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
 
B

Bill Cunningham

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
 
R

Richard

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

santosh

Bill said:
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[j] = 0;


Why is there a =0 after num [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.
 
B

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

Ben Bacarisse

Bill Cunningham said:
Ben Bacarisse said:
"Bill Cunningham" <[email protected]> writes:
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.
 
B

Ben Bacarisse

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

Richard

Bill Cunningham said:
Is that good or bad :)


What do you mean zero terminated or not? Doesn't C stick \0
automatically ? The only exception I know is strlen.

Bill

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

Bill Cunningham

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
 
K

Keith Thompson

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

Keith Thompson

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

Richard

Keith Thompson said:
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.
 
B

Ben Bacarisse

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

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]);
 
B

Bill Cunningham

Keith Thompson said:
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
 
R

Richard

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

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top