Array Problems

K

kohvirus

Hello everyone. 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!
 
K

Keith Thompson

Hello everyone. 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";
}

}


First of all, your code is C++, not C. If you have questions about
C++, comp.lang.c++ is down the hall on the left.

But as it happens, the array issues are (I'm fairly sure) common to C
and C++.

What looks like an array parameter declaration really declares a
pointer parameter. For example,
void func(int param[]);
really means
void func(int *param);

For that matter, so does this:
void func(int param[10]);
The 10 is simply ignored.
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.)

As the compiler is telling you, you need to specify the bounds of
all dimensions except the first. So this:
void func(int a[][10]);
is legal, and really means
void func(int (*a)[10]);
but this
void func(int a[][]);
is is illegal.

There really isn't any simple way to declare a two-dimensional array
(equivalently, an array of arrays) with both bounds allowed to vary.
Array indexing is defined in terms of pointer arithmetic, which
requires a fixed element size, so you have to decide in advance how
long each row of your array needs to be.

There are several approaches that give you more flexibility. One of
the most common is to use a pointer-to-pointer:
void func(int **a);
The argument can be a pointer to an array of pointers, where element
in turn points to an array of int. You have to explicitly allocate
(probably using malloc()) the array of pointers *and* each row of
ints, which means you have to explicitly deallocate everything when
you're done.

If you want to take this approach, section 6 of the C FAQ (google it
if you don't know where it is) has a lot of good information -- as do
all the other sections.

<OT>
On the other hand, if you're really using C++, there's probably
something in the STL that will do most of the work for you -- but we
can't discuss it here.
</OT>
 
K

kohvirus

I apoligize for posting in the wrong area. I'll repost there. Thank you
for the info you did post, it did help point me in the right direction.
I'll see how much farther I can take it with a little more insight.
 
M

Martin Ambuhl

Hello everyone. 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>

For some obscure reason you posted C++ code in < C++
is a different language and has a different newsgroup
< On the off-chance that you thought you were
writing C, here is a version of your code that conforms to the current C
standard and works:

#include <stdio.h>

void display(size_t m, size_t n, int a[m][n])
{
size_t i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++)
printf("%2d ", a[j]);
putchar('\n');
}
}

/* mha: driver added so you can see that this works */
#define ROWS 32
#define COLS 4
#define UPPERBOUND 100.
#include <stdlib.h>
#include <time.h>

int main(void)
{
int foo[ROWS][COLS];
size_t i, j;
srand(time(0));
for (i = 0; i < ROWS; i++)
for (j = 0; j < COLS; j++)
foo[j] = UPPERBOUND * (rand() / (1. + RAND_MAX));
display(ROWS, COLS, foo);
return 0;
}



#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!
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top