Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New to programming, don't get 2D/3D arrays

Hey everyone, I'm basically new to programming. I've decided to try and get started with C (not C++ or C#) and so far I've been doing pretty well. I managed to get far as two-dimensional arrays before I started to falter. While I think I broadly understand 2D integer arrays, I certainly don't understand 3D string arrays.

I'm learning by taking the techniques and applying them in an actual program I've created, an exchange rate "calculator" that basically takes asks the user to select a base currency then prints its value in USD. There's no maths involved, I simply googled stuff like EUR/USD and set the values manually in the array which I discuss below.

But here's where I'm getting stuck. I figure the best way to learn multi-dimensional arrays is to practically apply the theory, so here's what I've typed so far (I've omitted the other functions of my program (including the code which calls this function) for brevity):

 char currencies[5][3][4] = {
    {'1','2','3','4','5'},
    {'GBP','EUR','JPY','CAD','AUD'},
    {'1.5','1.23','0.11','0.96','0.87'}
};

int point, symbol, value;

displayarraycontents()
{
    for(point=1;point<5;point++){
        for(symbol=1;symbol<5;symbol++){
            for(value=1;symbol<5;symbol++)
                printf("%s ", currencies[point][symbol][value]);
            printf("\n");
        }}

}

Because C doesn't feature a string data type, building string arrays completely messes with my head.

Why currencies[5][3][4]? Because I'm storing a total of 5 currencies, each marked by a 3-letter symbol (eg EUR, CAD), which have a value of up to 4 digits, including the decimal point.

I'm trying to display this list:

1 GBP 1.5
2 EUR 1.23
3 JPY 0.11
4 CAD 0.96
5 AUD 0.87

When I click build, the line where I specify the values in the array is highlighted with several instances of this warning:

warning: overflow in implicit constant conversion

...and the line where I print the contents of the array is highlighted with this warning:

warning: format '%s' expects type 'char *', but argument 2 has type 'int'

Upon running the code, the rest of the program works fine except this function, which produces a "segmentation error" or somesuch.

Could somebody give me a hand here? Any help would be greatly appreciated, as well as any links to simple C 2D/3D string array initialisation tutorials! (my two books, the K&R and Teach Yourself C only provide vague examples that aren't relevant)

Thanks in advance!
-Ryan

EDIT: updated code using struct:

struct currency {
    char symbol[4];
    float value[5];
};


void displayarraycontents(){

        int index;

        struct currency currencies[] {
            {"GBP", 1.50},
            {"EUR", 1.23},
            {"JPY", 0.11},
            {"CAD", 0.96},
            {"AUD", 0.87},};

}

I get the following errors: main.c:99: error: nested functions are disabled, use -fnested-functions to re-enable
main.c:99: error: expected '=', ',', ';', 'asm' or 'attribute' before '{' token
main.c:100: error: expected ';' before '}' token
main.c:100: error: expected expression before ',' token

In the actual code window itself, every symbol is flagged as an "unexpected token".

like image 321
Ryan Avatar asked Jun 27 '10 21:06

Ryan


1 Answers

In this case, you don't actually want a 3D array. In fact, since you have a table of values, all you need is a 1D array.

The tricky part is that each element of the array needs to store two things: the currency symbol, and the associated exchange rate. C has a way of building a type that stores two things - it's the struct mechanism. We can define a struct to hold a single currency:

struct currency {
    char symbol[4];
    char value[5];
};

(Note that this does not create a variable; it creates a type. struct currency is analagous to char, except that we defined the meaning of the former ourselves).

...and we can now create an array of 5 of these:

struct currency currencies[5] = { 
    {"GBP", "1.5" },
    {"EUR", "1.23" },
    {"JPY", "0.11" },
    {"CAD", "0.96" },
    {"AUD", "0.87" } };

To iterate over them and print them out, the code would look like:

void displayarraycontents(void)
{
    int point;

    for(point = 0; point < 5; point++)
    {
        printf("%d %s %s\n", point + 1, currencies[point].symbol, currencies[point].value);
    }
}
like image 69
caf Avatar answered Oct 21 '22 22:10

caf