473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Creative input on indexing from 0 to 99 or 1 to 100

I've got a project that has 100 settings. In C I've defined these
settings in an array:

#define SETTING_MAX 100
int Setting[SETTING_MAX];

The user refers to them as setting[1] through setting[100].
Internally I refer to them as setting[0] through setting[99].

Initially I was hidiing the conversion from the user; that is, taking
the user input and subtracting one from it. But this is driving me
nutty.
There are a dozen places where I apply this through out the project not
just one array.

For size constraints I can not use an array from 0 to 100.

Has anyone come up with a good solution to this issue.

Thanks in advance
George

Nov 15 '05 #1
6 1153
GMM50 wrote:
I've got a project that has 100 settings. In C I've defined these
settings in an array:

#define SETTING_MAX 100
int Setting[SETTING_MAX];

The user refers to them as setting[1] through setting[100].
Internally I refer to them as setting[0] through setting[99].

Initially I was hidiing the conversion from the user; that is, taking
the user input and subtracting one from it. But this is driving me
nutty.
There are a dozen places where I apply this through out the project not
just one array.

For size constraints I can not use an array from 0 to 100.

Has anyone come up with a good solution to this issue.

Thanks in advance
George


You could use function macros (or inline functions if your system
supports). Or real functions if the overhead is not excessive,
though one int making a differences suggests your resources are really
tight. One virtue of macros here is that you say it applies to
multiple arrays and that works even if the arrays are of different
types...

Example to get one based array value

#define GET_OB_ARRAY_VALUE(array,index) ((array)[(index)-1])

You could even do (to enforce lower boundry)

#define GET_OB_ARRAY_VALUE(array,index)
(assert((index)>0),((array)[(index)-1)]))

-David

Nov 15 '05 #2
SSM
If you prefer the solution below which overcomes complexity at the loss of 1
byte per array,
then you can do the following:

#define SETTING_MAX 101
Then you can use array index as input by the user which is 1 to 100.
But this comes with a loss of 1 byte per array which is Setting[0].

"GMM50" <ge***********@att.net> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I've got a project that has 100 settings. In C I've defined these
settings in an array:

#define SETTING_MAX 100
int Setting[SETTING_MAX];

The user refers to them as setting[1] through setting[100].
Internally I refer to them as setting[0] through setting[99].

Initially I was hidiing the conversion from the user; that is, taking
the user input and subtracting one from it. But this is driving me
nutty.
There are a dozen places where I apply this through out the project not
just one array.

For size constraints I can not use an array from 0 to 100.

Has anyone come up with a good solution to this issue.

Thanks in advance
George

Nov 15 '05 #3


GMM50 wrote:
I've got a project that has 100 settings. In C I've defined these
settings in an array:

#define SETTING_MAX 100
int Setting[SETTING_MAX];

The user refers to them as setting[1] through setting[100].
Internally I refer to them as setting[0] through setting[99].

Initially I was hidiing the conversion from the user; that is, taking
the user input and subtracting one from it. But this is driving me
nutty.
There are a dozen places where I apply this through out the project not
just one array.

For size constraints I can not use an array from 0 to 100.

Has anyone come up with a good solution to this issue.

Thanks in advance
George


Using macros is a good way.But I think you can apply 101 items instead
of 100 items.

#define SETTING_MAX 101
int Setting[SETTING_MAX];

And discard the first item.It is really hard to determine whether
arrays begin with 1 or 0.In my own opinion,0 is better. :P

Nov 15 '05 #4
You're correct 101 should not break the bank.
gm

Nov 15 '05 #5
In article <11**********************@g49g2000cwa.googlegroups .com>,
GMM50 <ge***********@att.net> wrote:
You're correct 101 should not break the bank.
gm


That seemed to be one of the conditions of contest, though.
I.e., he stated that making the array length 101 was not an option.

Which of course makes it sound like homework.

Nov 15 '05 #6
*** Rude topposting corrected ***
SSM wrote:
"GMM50" <ge***********@att.net> wrote in message
I've got a project that has 100 settings. In C I've defined
these settings in an array:

#define SETTING_MAX 100
int Setting[SETTING_MAX];

The user refers to them as setting[1] through setting[100].
Internally I refer to them as setting[0] through setting[99].

Initially I was hidiing the conversion from the user; that is,
taking the user input and subtracting one from it. But this is
driving me nutty. There are a dozen places where I apply this
through out the project not just one array.

For size constraints I can not use an array from 0 to 100.

Has anyone come up with a good solution to this issue.


If you prefer the solution below which overcomes complexity at the
loss of 1 byte per array, then you can do the following:

#define SETTING_MAX 101
Then you can use array index as input by the user which is 1 to 100.
But this comes with a loss of 1 byte per array which is Setting[0].


If you had refrained from top-posting you would have seen that the
OP specifically excluded this solution.

For user interaction, the OP could have a single pair of routines:

int getindex(FILE *f) { }
int putindex(int ix, FILE *f) { }

which can encapsulate all of text/binary conversion, range
checking, and add/subtract 1. Internal to get/putindex the value 0
could mean "not entered", while outside that would be represented
by -1. If you prevent getindex from ever returning out of range,
much will be simplified.

Another possible version of getindex could supply an optional
prompt, and assume the use of stdin/stdout pair. This may be
easier for input error reporting:

int getindex(char *prompt) { }
int putindex(int ix) {} /* Assumed to stdout */

Now the program internals can universally assume C usage. Another
possibility is to switch to Pascal or Ada, where such nuisances do
not arise.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving the ith item of every sublist? I know about for i...
15
by: Jay | last post by:
Hello all. I've recently transferred our web sites from a Windows2000 server to a Windows2003 server. The transfer went almost flawless until I noticed that our search function isn't working...
4
by: multimatum2 | last post by:
Hello, I need to enable/disable input text forms... But... I need to have the same style (color...) in both modes.. Could you help me ? Thanx a lot A small sample... ...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
7
by: Ryan | last post by:
I have a bit of a problem with regards an indexing strategy. Well, basically there is no indexing strategy on a set of data I have at work. Now, I didn't create the design as I would have allowed...
3
by: Chung Leong | last post by:
Here's the rest of the tutorial I started earlier: Aside from text within a document, Indexing Service let you search on meta information stored in the files. For example, MusicArtist and...
4
by: Emin | last post by:
Dear Experts, How much slower is dict indexing vs. list indexing (or indexing into a numpy array)? I realize that looking up a value in a dict should be constant time, but does anyone have a...
4
by: pratimapaudel | last post by:
I have tables in my database, it's sql server 2005. I heard some of them have indexing and some of them doesnot have indexing. If i want to check whether it has indexing or not how do i do? ...
2
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a server 2008 IIS 7.0 with indexing service installed. I have created the catalog and have a test page using these posts:...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.