newbie array referencing (i think)

P

peacelittleone

I have an array:
double myArray[108]
I have an enum list:
emum myDataList { A_1, B_1, C_1 ... I_1,
A_2,..............I_2,
....
A_12,.............I_12}

Each value of the array is sequentially represented by the enum list.

I have a counter:
int myCounter

Here is my problem...

I need to be able to reference the array one "row" at a time by enum
name.

example:

myA_Term = myArray[A_1];
myB_Term = myArray[B_1];
etc...

I have a loop to go through the array, one row at a time, but I don't
know how to change the array index to call the right row information by
name.

First time through:

myA_Term = myArray[A_1];

Second time through:

myA_Term = myArray[A_2];

QUESTION:

Is there a way to do something like:

myA_Term = myArray[A_myCounter]; ( i know this is wrong )
where I can get to the value of myCounter.
Thanks in advanced!!!!


Heather.
 
L

Louis Krupp

peacelittleone said:
I have an array:
double myArray[108]
I have an enum list:
emum myDataList { A_1, B_1, C_1 ... I_1,
A_2,..............I_2,
...
A_12,.............I_12}

Each value of the array is sequentially represented by the enum list.

I have a counter:
int myCounter

Here is my problem...

I need to be able to reference the array one "row" at a time by enum
name.

example:

myA_Term = myArray[A_1];
myB_Term = myArray[B_1];
etc...

I have a loop to go through the array, one row at a time, but I don't
know how to change the array index to call the right row information by
name.

First time through:

myA_Term = myArray[A_1];

Second time through:

myA_Term = myArray[A_2];

QUESTION:

Is there a way to do something like:

myA_Term = myArray[A_myCounter]; ( i know this is wrong )
where I can get to the value of myCounter.
Thanks in advanced!!!!


Heather.

An enum is basically just an int (although some compilers like it better
when you cast from one to the other).

Could something like this be what you're looking for? (I haven't tried
to compile most of this.)

enum e {A, B, C, e_max};
double array x[(int)e_max]; /* e_max is a symbolic constant */

....
int i;
for (i = 0; i < (int)e_max; i++) {
enum e ei;
double v;
x = i + 123.456; /* or whatever */
v = x;
}

Hope this helps...

Louis Krupp
 
M

Mike Wahler

peacelittleone said:
I have an array:
double myArray[108]
I have an enum list:
emum myDataList { A_1, B_1, C_1 ... I_1,
A_2,..............I_2,
...
A_12,.............I_12}

Each value of the array is sequentially represented by the enum list.

I have a counter:
int myCounter

Here is my problem...

I need to be able to reference the array one "row" at a time by enum
name.

example:

myA_Term = myArray[A_1];
myB_Term = myArray[B_1];
etc...

I have a loop to go through the array, one row at a time, but I don't
know how to change the array index to call the right row information by
name.

First time through:

myA_Term = myArray[A_1];

Second time through:

myA_Term = myArray[A_2];

QUESTION:

Is there a way to do something like:

myA_Term = myArray[A_myCounter]; ( i know this is wrong )
where I can get to the value of myCounter.

A_2 is equal to A_1 + I_1
A_3 is equal to A_2 + I_2
etc.


I'm not sure I understand what you're really trying to do, or
why you can't simply use arithmetic instead of those enum values
to compute your indices, but a guess:

#include <stdio.h>

int main()
{
double myArray[108] = {0};
enum myDataList
{
A_1, B_1, C_1, D_1, E_1, F_1, G_1, H_1, I_1,
A_2, B_2, C_2, D_2, E_2, F_2, G_2, H_2, I_2,
A_3, B_3, C_3, D_3, E_3, F_3, G_3, H_3, I_3,
A_4, B_4, C_4, D_4, E_4, F_4, G_4, H_4, I_4,
A_5, B_5, C_5, D_5, E_5, F_5, G_5, H_5, I_5,
A_6, B_6, C_6, D_6, E_6, F_6, G_6, H_6, I_6,
A_7, B_7, C_7, D_7, E_7, F_7, G_7, H_7, I_7,
A_8, B_8, C_8, D_8, E_8, F_8, G_8, H_8, I_8,
A_9, B_9, C_9, D_9, E_9, F_9, G_9, H_9, I_9,
A_10, B_10, C_10, D_10, E_10, F_10, G_10, H_10, I_10,
A_11, B_11, C_11, D_11, E_11, F_11, G_11, H_11, I_11,
A_12, B_12, C_12, D_12, E_12, F_12, G_12, H_12, I_12
};

size_t idx = 0;
size_t row = 0;
size_t col = 0;
const size_t col_count = I_1 + 1;
const size_t row_count = (I_12 - A_1 + 1) / col_count;

for(idx = 0; idx < sizeof myArray / sizeof *myArray; ++idx)
myArray[idx] = idx;

for(row = A_1; row < row_count; ++row)
for(col = 0; col < col_count; ++col)
{
idx = row * col_count + col;
printf("myArray[%3lu] == %3.0f\n", idx, myArray[idx]);
}

return 0;
}

Note how of all your enum values, only 'A_1', 'I_1', and 'A_12'
were needed to compute an index (and those aren't necessary either;
those values could be computed given the size of your array,
as reported by the 'sizeof' operator.) Also, using the array
size prevents the possiblity of later problems if you change
the size of your array, or your list of enumerator values.

IOW I consider the above code to be very fragile (but perhaps
it's at least close to what you're after).

-Mike
 
P

peacelittleone

ok. I am a little confused. (sorry)

A little background info might help.

Currently the program reads in a 1 row array of doubles - which are
nine unique values.

The change I need to make is to expand the array to 12 rows - each
unique.

The program currently has the enum list to be {A, B, C, D, E, F, G, H,
I}

Later on in the program it calls:

foo = myArray[A];

Not sure how to proceed.

(it's been a long time since I have done any of this so please be
paitent)

TIA!

Heather.
 
K

Kobu

peacelittleone said:
ok. I am a little confused. (sorry)

A little background info might help.

Currently the program reads in a 1 row array of doubles - which are
nine unique values.

The change I need to make is to expand the array to 12 rows - each
unique.

The program currently has the enum list to be {A, B, C, D, E, F, G, H,
I}

Later on in the program it calls:

foo = myArray[A];

Not sure how to proceed.

(it's been a long time since I have done any of this so please be
paitent)

TIA!

Heather.


If you want to loop through array elements, you have to use actual
numeric values. I don't see what purpose the enum serves in your
example.

why don't you just use numbers?

..int i;
..for(i = 0; i < 12; i++)
..{
.. /* do something with myArray; */
..}

If your intension is to have identifier labels that correspond to array
subscripts, then you have to find another method to do this (not with
enum)

Why not use a struct?

#define ARRAYSIZE 100

..struct mystruct
..{
.. double element;
.. char identifier;
..} MyRecord[ARRAYSIZE];


that will declare an array of structs(100 of them), where each element
of that array actually has two parts: storage for a double, storage for
a char

You can initialize the 100 structs like this:

..int i;
..for(i = 0; i < ARRAYSIZE; i++)
..{
.. MyRecord.identifier = 'A' + i;
..}

So effectively, you will be carrying around an identifier(Capitalized
character like you wanted) with every double (each pair part of a
struct.. and 100 of these pairs part of an array of 100 structs)

Why you wanted to use "letters" to identify your double elements is
beyond me. Nevertheless, this accomplishes your task.

So let's say you wanted to search your array of structs (each with a
double and a char identifier), you can do by simply looping through
and comparing:

char find = 'X'; /* going to try and find the 'X' struct */

..for(i = 0; i < ARRAYSIZE; i++)
..{
.. if(MyRecord.identifier = find)
.. break;
..}

/* 'X' has been found, now 'i' will be the subscript of the 'X' struct
*/
/* now you can have your way with the double stored in the 'X' struct
*/

MyRecord.element = 12345; /* updated the 'X' struct's double */


So, enum will not get you anywhere closer to your dream, you should try
structs.
 
K

Kobu

Kobu said:
peacelittleone said:
ok. I am a little confused. (sorry)

A little background info might help.

Currently the program reads in a 1 row array of doubles - which are
nine unique values.

The change I need to make is to expand the array to 12 rows - each
unique.

The program currently has the enum list to be {A, B, C, D, E, F, G, H,
I}

Later on in the program it calls:

foo = myArray[A];

Not sure how to proceed.

(it's been a long time since I have done any of this so please be
paitent)

TIA!

Heather.


If you want to loop through array elements, you have to use actual
numeric values. I don't see what purpose the enum serves in your
example.

why don't you just use numbers?

.int i;
.for(i = 0; i < 12; i++)
.{
. /* do something with myArray; */
.}

If your intension is to have identifier labels that correspond to array
subscripts, then you have to find another method to do this (not with
enum)

Why not use a struct?

#define ARRAYSIZE 100

.struct mystruct
.{
. double element;
. char identifier;
.} MyRecord[ARRAYSIZE];


that will declare an array of structs(100 of them), where each element
of that array actually has two parts: storage for a double, storage for
a char

You can initialize the 100 structs like this:

.int i;
.for(i = 0; i < ARRAYSIZE; i++)
.{
. MyRecord.identifier = 'A' + i;
.}

So effectively, you will be carrying around an identifier(Capitalized
character like you wanted) with every double (each pair part of a
struct.. and 100 of these pairs part of an array of 100 structs)

Why you wanted to use "letters" to identify your double elements is
beyond me. Nevertheless, this accomplishes your task.

So let's say you wanted to search your array of structs (each with a
double and a char identifier), you can do by simply looping through
and comparing:

char find = 'X'; /* going to try and find the 'X' struct */

.for(i = 0; i < ARRAYSIZE; i++)
.{
. if(MyRecord.identifier = find)
. break;
.}


that should be == in the if statement above
/* 'X' has been found, now 'i' will be the subscript of the 'X' struct
*/
/* now you can have your way with the double stored in the 'X' struct
*/

MyRecord.element = 12345; /* updated the 'X' struct's double */


So, enum will not get you anywhere closer to your dream, you should try
structs.
 
P

peacelittleone

I would love to use a struct - but I have inherited this code and the
interfaces (Simulink) can only sustain arrays.
Thanks for your insight.

Heather.
 
K

Kobu

peacelittleone said:
I would love to use a struct - but I have inherited this code and the
interfaces (Simulink) can only sustain arrays.
Thanks for your insight.

Heather.

Do you "have to" or "want to" use enums to access array subscripts?
More info will be needed before anyone can help you.
 
P

peacelittleone

I "have to" use enums.
I have acutally found a solution. I apologize for the formatting ahead
of time... google doesn't perserve my spacing.

int m, n, foo = 0;

for (m = 0; m < 12; m==) {
for (n = 0; n < 9; n++) {
myValues[m][n] = myFileArray[foo];
foo += 1;
}
}


A = myValues[myOffset][A];
B = myValues[myOffset];
C = myValues[myOffset][C];
D = myValues[myOffset][D];

where I am able to modify myOffset at a higher level in the code.
Thank you to everyone!
 
M

Mike Wahler

peacelittleone said:
I "have to" use enums.
I have acutally found a solution. I apologize for the formatting ahead
of time... google doesn't perserve my spacing.

int m, n, foo = 0;

for (m = 0; m < 12; m==) {
for (n = 0; n < 9; n++) {
myValues[m][n] = myFileArray[foo];
foo += 1;
}
}


A = myValues[myOffset][A];
B = myValues[myOffset];
C = myValues[myOffset][C];
D = myValues[myOffset][D];

where I am able to modify myOffset at a higher level in the code.
Thank you to everyone!


It sure would have helped us had you said up-front
that your array was 2D, not 1D as your first example
showed. :)

I'm glad you got thinks working.

-Mike
 
B

Big K

peacelittleone said:
I "have to" use enums.
I have acutally found a solution. I apologize for the formatting ahead
of time... google doesn't perserve my spacing.

int m, n, foo = 0;

for (m = 0; m < 12; m==) {
for (n = 0; n < 9; n++) {
myValues[m][n] = myFileArray[foo];
foo += 1;
}
}


A = myValues[myOffset][A];
B = myValues[myOffset];
C = myValues[myOffset][C];
D = myValues[myOffset][D];

where I am able to modify myOffset at a higher level in the code.
Thank you to everyone!


What does this have to do with your original question? Your original
question was to find a way to loop through enum values (so you can use
the enum values in your array subscripting). The solution you have
above, still uses arithmetic values for your loop variables, not enum.
 
C

CBFalconer

peacelittleone said:
I "have to" use enums.
I have acutally found a solution. I apologize for the formatting
ahead of time... google doesn't perserve my spacing.

So don't use google, get a newsreader. Or at least follow the
advice in my sig below.
 
K

Keith Thompson

CBFalconer said:
So don't use google, get a newsreader. Or at least follow the
advice in my sig below.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Unfortunately, using the hidden "Reply" link avoids the quoting and
attribution bug, but not the indentation bug. The only way to get
proper indentation is to prefix each line with some marker like '.'
(or use a different news client).
 
R

Richard Bos

Keith Thompson said:
Unfortunately, using the hidden "Reply" link avoids the quoting and
attribution bug, but not the indentation bug. The only way to get
proper indentation is to prefix each line with some marker like '.'
(or use a different news client).

ITYM s/different//.

Richard
 

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,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top