473,398 Members | 2,404 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,398 software developers and data experts.

Dynamic Array

///////////// start code /////////////
#include<iostream>
#include<iomanip>

using namespace std;

int ReadSquareSize()
{
int x;
cout<<"Plhktrologhse to mege8os tou pinaka, akeraios 8etikos perittos apo 3 kai panw: ";
while(1)
{
cin>>x;
if(x>=3 && x%2==1)
{
return x;
}
else
{
cout<<"Plhktrologhse to mege8os tou pinaka, akeraios 8etikos perittos apo 3 kai panw: ";
}
}
cout<<endl;
}

int main()
{
int n = ReadSquareSize();
int square[n][n]; //----- error here -----
}

///////////////// end code /////////////////

This is a part from my code. What i cannot understand is why i get 6 errors in the error line. I have declared n from the previous line. Malloc is better to use? If so, can u post the corrected code lines?
Nov 21 '06 #1
3 2426
Colloid Snake
144 100+
You use 'int main' and you don't use any return statement.

Try putting return 0; below your array declaration.

Also, the error messages might help.
Nov 21 '06 #2
Hi there,

You can't daclare something like:

int x;
cin >> x;
int a[x];

It won't compile because x is unknown in the time of compilation. Values in brackets while declaring arrays have to be daclered before compilation.

U can try to declare 2-dimension array using new. Problem is that in C++ u can't declare 2-dimensional array directly.
Example u can remake for your code:

int **a = new int*[x];
for (int i = 0; i < x; ++i)
a[i] = new int[y];

I encourage you to learn about very nice funcion from STL library -> vectors. U can find a use for them in your example.
Nov 21 '06 #3
sircool
12
///////////// start code /////////////
#include<iostream>
#include<iomanip>

using namespace std;

int ReadSquareSize()
{
int x;
cout<<"Plhktrologhse to mege8os tou pinaka, akeraios 8etikos perittos apo 3 kai panw: ";
while(1)
{
cin>>x;
if(x>=3 && x%2==1)
{
return x;
}
else
{
cout<<"Plhktrologhse to mege8os tou pinaka, akeraios 8etikos perittos apo 3 kai panw: ";
}
}
cout<<endl;
}

int main()
{
int n = ReadSquareSize();
int square[n][n]; //----- error here -----
}

///////////////// end code /////////////////

This is a part from my code. What i cannot understand is why i get 6 errors in the error line. I have declared n from the previous line. Malloc is better to use? If so, can u post the corrected code lines?

if you use like that definitation
int square[n][n] that means you made a array static.So n must be const that does not matter you return a n value from any function.
i will make a dynamic aray declaration you can use

int **arary,row,colomn; // two asterix means you make two dimentional aray
than you will request memory (in C malloc, in C++ you can use new function)

array=new int*[row];
for(int a=0;a<row;a++) //row=n in your code
array[a]=new int[colomn]; //request a colomn*sizeof(int) memory for each row

merory management is like that and dynamic arrays can be like that

keep in touch
Nov 21 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
1
by: lemonade | last post by:
Hello! Can someone explain to me the difference between dynamic array of pointers vs dynamic array of objects by giving a real life example. Following is the code that I am using for dynamic...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
0
by: pjr | last post by:
Using VS2005, I dynamically create an event delegate. Code follows question. My method gets the event's parameters and passes them onto a common event handler. My delegate gets called when expected...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
13
by: kwikius | last post by:
Does anyone know what a C99 dynamic array is, and if it will be useable in C++? regards Andy Little
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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.