473,830 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array Problems

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[i][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 multidimensiona l 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 '05 #1
3 3797
ko******@gmail. com 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[i][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 multidimensiona l 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.

Oct 29 '05 #2
ko******@gmail. com 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[i][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 multidimensiona l 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-lit...html#faq-16.16

john
Oct 29 '05 #3
ko******@gmail. com 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-lit....html#faq-10.6
http://www.parashift.com/c++-faq-lit...html#faq-13.15

and browse the whole faq while you're there.

cout << " " << A[i][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 multidimensiona l 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

Oct 29 '05 #4

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

Similar topics

12
2250
by: shailashri_sk | last post by:
Hi, int *p; p++; here p now increments itself with the size of integer. similarly, I wanted to know, how to declare an pointer to an array ( say array of integers) where in it we do a p++ it increments itself with the size of the array each time it is incremented with 1. like
8
1701
by: engaref | last post by:
Hello Every body, I am new with C programming.I have received the Problems from my advisor on Array but I did not find any Proper answer yet. If Possible,please make a solution for the Problems. Thanks The Problems are as follows: 1-Declare an array of length of 10 and read integers into the elements
204
13148
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
16
14148
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the byteArray and afterwards see the changes in C#. (if you wanna know more details: the goal is to write the content of an existing char-array in c++ precisely into the passed byteArray, so that after the function has proceded the content of the...
3
2705
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to return an array as a property because it will return a copy of the array instead a reference to it. How can I force the property to return a reference to the array? Is it only a feature of arrays? I hope normal class objects (including collections)...
5
19604
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
3
3742
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a lot different from what the pros around here use. I really need help with debugging some program errors more than anything, even though my coding style might not be perfect. Anyway here is my code. About the only things that work right are...
104
17030
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from that array Could you show me a little example how to do this? Thanks.
152
9941
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { { 3.14 }, { 42.6 } }; f((double *)array, sizeof array / sizeof **array); return 0;
5
2682
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return that structure, is this valid? As shown in the code below I am allocating the structure in the function and then returning the structure. I know if the structure contained only simple types (int, float) this will work without problems as you...
0
9781
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9641
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10769
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10197
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7740
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5615
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3956
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3072
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.