Array Problems

Discussion in 'C++' started by kohvirus@gmail.com, Oct 29, 2005.

  1. Guest

    Well after embaressing myself and posting in the wrong fourm, I found
    my way to the right one and I'm hoping to seek some help. This is the
    program without any modifications that I am making recently (because
    I'll most likely make it blow up moreso).

    I'm currently trying to write a program with an array but for the life
    of me I can't seem to get the right syntax for the array. Here is the
    program as follows:

    #include <iostream>
    #include <stdio.h>

    void display(int A[] [], int m, int n)

    {
    int i;
    int j;

    std::cout << "Enter your value for m ";
    std::cin >> m;
    std::cout << "\n";

    for (i =0; i< m; i++)

    { for (j=0; j < n; j++)

    cout << " " << A[j];

    cout << "\n";
    }

    }

    Sorry if the way I write makes you cringe, still learning to to make
    code 'neat'. Anyway, here are the error outputs I am getting and I am
    unable to resolve any further:

    fail.cpp:9: declaration of `A' as multidimensional array
    fail.cpp:9: must have bounds for all dimensions except the first
    fail.cpp: In function `void display(int, int)':
    fail.cpp:21: `A' undeclared (first use this function)
    fail.cpp:21: (Each undeclared identifier is reported only once
    fail.cpp:21: for each function it appears in.)

    Any help at narrowing the problem would be appreciated. I'm using this
    to test for a CGI program I will be creating here soon.

    Thanks!
    , Oct 29, 2005
    #1
    1. Advertising

  2. red floyd Guest

    wrote:
    > Well after embaressing myself and posting in the wrong fourm, I found
    > my way to the right one and I'm hoping to seek some help. This is the
    > program without any modifications that I am making recently (because
    > I'll most likely make it blow up moreso).
    >
    > I'm currently trying to write a program with an array but for the life
    > of me I can't seem to get the right syntax for the array. Here is the
    > program as follows:
    >
    > #include <iostream>
    > #include <stdio.h>
    >
    > void display(int A[] [], int m, int n)
    >
    > {
    > int i;
    > int j;
    >
    > std::cout << "Enter your value for m ";
    > std::cin >> m;
    > std::cout << "\n";
    >
    > for (i =0; i< m; i++)
    >
    > { for (j=0; j < n; j++)
    >
    > cout << " " << A[j];
    >
    > cout << "\n";
    > }
    >
    > }
    >
    > Sorry if the way I write makes you cringe, still learning to to make
    > code 'neat'. Anyway, here are the error outputs I am getting and I am
    > unable to resolve any further:
    >
    > fail.cpp:9: declaration of `A' as multidimensional array
    > fail.cpp:9: must have bounds for all dimensions except the first
    > fail.cpp: In function `void display(int, int)':
    > fail.cpp:21: `A' undeclared (first use this function)
    > fail.cpp:21: (Each undeclared identifier is reported only once
    > fail.cpp:21: for each function it appears in.)
    >
    > Any help at narrowing the problem would be appreciated. I'm using this
    > to test for a CGI program I will be creating here soon.
    >
    > Thanks!
    >


    You need to specify the second dimension for A, e.g. int
    A[][SOME_CONSTANT];


    The two error messages for line 9 are quite clear on this.
    red floyd, Oct 29, 2005
    #2
    1. Advertising

  3. wrote:
    > Well after embaressing myself and posting in the wrong fourm, I found
    > my way to the right one and I'm hoping to seek some help. This is the
    > program without any modifications that I am making recently (because
    > I'll most likely make it blow up moreso).
    >
    > I'm currently trying to write a program with an array but for the life
    > of me I can't seem to get the right syntax for the array. Here is the
    > program as follows:
    >
    > #include <iostream>
    > #include <stdio.h>
    >
    > void display(int A[] [], int m, int n)
    >
    > {
    > int i;
    > int j;
    >
    > std::cout << "Enter your value for m ";
    > std::cin >> m;
    > std::cout << "\n";
    >
    > for (i =0; i< m; i++)
    >
    > { for (j=0; j < n; j++)
    >
    > cout << " " << A[j];
    >
    > cout << "\n";
    > }
    >
    > }
    >
    > Sorry if the way I write makes you cringe, still learning to to make
    > code 'neat'. Anyway, here are the error outputs I am getting and I am
    > unable to resolve any further:
    >
    > fail.cpp:9: declaration of `A' as multidimensional array
    > fail.cpp:9: must have bounds for all dimensions except the first
    > fail.cpp: In function `void display(int, int)':
    > fail.cpp:21: `A' undeclared (first use this function)
    > fail.cpp:21: (Each undeclared identifier is reported only once
    > fail.cpp:21: for each function it appears in.)
    >
    > Any help at narrowing the problem would be appreciated. I'm using this
    > to test for a CGI program I will be creating here soon.
    >
    > Thanks!
    >


    You cannot pass an array to a function in C or C++. You use pointers
    instead.

    This syntax

    void f(int a[])

    is just another way of writing

    void f(int* a)

    In other words the first form is just a way of confusing newbies into
    thinking that you can pass an array to a function in C++. Usually they
    find out the truth when they try a two dimensinal array.

    There are several things you could do at this point

    1) You could accept that the second dimension must be a constant, e.g.

    void f(int a[][10])

    which of course is just another way of writing

    void f(int (*a)[10])

    Pointers again! Remember no way of passing an array to a function in C++.

    2) You could learn about how to represent 2d arrays using pointers. In
    this case your function would look like this

    void f(int** a)

    Trouble with this option is that your calling code would have to change
    as well.

    3) You could learn about vectors, you can pas a 2d vector to a function

    void f(const std::vector< std::vector<int> >& a)

    My advice would be option 3, stay away from pointers, they are difficult.

    You might also be interrested in the FAQ

    http://www.parashift.com/c -faq-lite/freestore-mgmt.html#faq-16.16

    john
    John Harrison, Oct 29, 2005
    #3
  4. wrote:
    > Well after embaressing myself and posting in the wrong fourm, I found
    > my way to the right one and I'm hoping to seek some help. This is the
    > program without any modifications that I am making recently (because
    > I'll most likely make it blow up moreso).
    >
    > I'm currently trying to write a program with an array but for the life
    > of me I can't seem to get the right syntax for the array. Here is the
    > program as follows:
    >
    > #include <iostream>
    > #include <stdio.h>


    <stdio.h> should be <cstdio> in C++. What's more, you don't need it
    here.

    > void display(int A[] [], int m, int n)


    Well that's illegal. Take a statement like

    A[1][2] = 0;

    and imagine A is a 3x3 array. That means you are accessing the third
    column of the second row, which is equivalent to

    A[1*row_length + 2] = 0;

    Now, the compiler, in display() has no way to know the value of
    "row_length". To do that, you must tell it:

    void display(int A[][3], int m, int n)

    You could also set the first dimension, but that's not necessary. The
    compiler does not need to know how many rows there are.

    "But that's a pain". Yes it is. Either use a plain pointer

    void display(int *A, int m, int n)

    and do the math yourself, as I did earlier, using "m" and "n" (these
    are the dimensions, right?) or use a std::vector.

    >
    > {
    > int i;
    > int j;


    No. Always define variables as near its for use as possible.

    > std::cout << "Enter your value for m ";
    > std::cin >> m;
    > std::cout << "\n";


    > for (i =0; i< m; i++)


    Here, you should define i:

    for (int i=0; i<m; ++i)

    > { for (j=0; j < n; j++)


    for (int j=0; j<n; ++j)

    See

    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.6
    http://www.parashift.com/c -faq-lite/operator-overloading.html#faq-13.15

    and browse the whole faq while you're there.

    >
    > cout << " " << A[j];
    >
    > cout << "\n";
    > }
    >
    > }
    >
    > Sorry if the way I write makes you cringe, still learning to to make
    > code 'neat'. Anyway, here are the error outputs I am getting and I am
    > unable to resolve any further:
    >
    > fail.cpp:9: declaration of `A' as multidimensional array
    > fail.cpp:9: must have bounds for all dimensions except the first
    > fail.cpp: In function `void display(int, int)':
    > fail.cpp:21: `A' undeclared (first use this function)
    > fail.cpp:21: (Each undeclared identifier is reported only once
    > fail.cpp:21: for each function it appears in.)


    That should be solved. However, I fail to see the point of this
    program, so I cannot give you more advices.


    Jonathan
    Jonathan Mcdougall, Oct 29, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Susan Baker
    Replies:
    2
    Views:
    809
    kelvSYC
    Jun 26, 2005
  2. Shelly

    Problems, problems for newbie

    Shelly, Sep 2, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    2,182
    Shelly
    Sep 3, 2007
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    490
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    209
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    473
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page