•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,557 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,680 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 5630 | Replies: 11
![]() |
•
•
Join Date: Jun 2006
Posts: 29
Reputation:
Rep Power: 3
Solved Threads: 0
I'm trying to write a program where the user gives a size of an array. myArray(5) (below) fills and prints a 5x5 array with numbers 1-5 and prints out 5 numbers on a line.
The output would look something like this...
The code of the program is as follows...
The compiler keeps giving me errors saying that it cant make an array with a size of 0, which makes sense; however, the array should not be initialized till a user enters a value.
I am sure this is some simple fix, but I am just not experienced enough to figure this one out. Can someone help me please?
The output would look something like this...
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
The code of the program is as follows...
#include <iostream>
using namespace std;
int main()
{
int size=1;
cout << "enter an array size: ";
cin >> size; cout << '\n';
myArray(size);
// disregard next two lines: stops program from flashing closed
int c=9;
cin>>c;
}
void myArray(int sizex)
{
// fill array
int a[sizex][sizex];
for(int i=1; i<sizex; i++)
{
for(int j=1; j<sizex; j++)
{
if(j%1==0)
a[i][j] = i;
else
a[i][j] = j;
}
}
// print array
int n=1;
for(int i=1; i<6; i++)
{
for(int j=1; j<6; j++)
{
cout << a[i][j];
if((n % 5) == 0 )
cout << "\n";
else
cout << ' ';
n++;
}
}
}The compiler keeps giving me errors saying that it cant make an array with a size of 0, which makes sense; however, the array should not be initialized till a user enters a value.
I am sure this is some simple fix, but I am just not experienced enough to figure this one out. Can someone help me please?
•
•
Join Date: Jan 2007
Location: Maryland, USA
Posts: 1,036
Reputation:
Rep Power: 4
Solved Threads: 23
•
•
Join Date: Jan 2007
Location: Maryland, USA
Posts: 1,036
Reputation:
Rep Power: 4
Solved Threads: 23
c++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; void printarray(int); int main() { int num; cout<<"enter a number\n"; cin>>num; printarray(num); return 0; } void printarray(int n) { int array[n][n]; for(int x=0, i=1; x<n; x++, i++) { for(int y=0; y<n; y++) { array[x][y]=i; } } for(int y=0; y<n; y++) { for(int x=0; x<n; x++) { cout<<array[x][y]; } cout<<"\n"; } }
Last edited by Sturm : Jun 1st, 2007 at 5:20 pm.
"Hey ass, don't hijack my thread. This is serious." -JoshSCH
> int a[sizex][sizex];
The first problem is standard C++ doesn't allow variable sized arrays.
Though some compilers offer it as an extension.
> for(int i=1; i<sizex; i++)
The second problem is that array subscripts start at 0, not 1.
The first problem is standard C++ doesn't allow variable sized arrays.
Though some compilers offer it as an extension.
> for(int i=1; i<sizex; i++)
The second problem is that array subscripts start at 0, not 1.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
•
•
Join Date: Jan 2007
Location: Maryland, USA
Posts: 1,036
Reputation:
Rep Power: 4
Solved Threads: 23
•
•
Join Date: Jun 2006
Posts: 29
Reputation:
Rep Power: 3
Solved Threads: 0
I'm using VC++ express edition and I get the following errors:
1>------ Build started: Project: TestProj, Configuration: Debug Win32 ------ 1>Compiling... 1>source.cpp 1>d:\documents and settings\owner\my documents\visual studio 2005\projects\testprojexe\testproj\source.cpp(14) : error C2057: expected constant expression 1>d:\documents and settings\owner\my documents\visual studio 2005\projects\testprojexe\testproj\source.cpp(14) : error C2466: cannot allocate an array of constant size 0 1>d:\documents and settings\owner\my documents\visual studio 2005\projects\testprojexe\testproj\source.cpp(14) : error C2057: expected constant expression 1>d:\documents and settings\owner\my documents\visual studio 2005\projects\testprojexe\testproj\source.cpp(14) : error C2466: cannot allocate an array of constant size 0 1>d:\documents and settings\owner\my documents\visual studio 2005\projects\testprojexe\testproj\source.cpp(14) : error C2087: 'array' : missing subscript 1>d:\documents and settings\owner\my documents\visual studio 2005\projects\testprojexe\testproj\source.cpp(14) : error C2133: 'array' : unknown size
Well, you can use a variable for array size, but it has to be a const value, I believe...
On the other hand, you can do dynamically sized arrays like this:
Hope that helps!
On the other hand, you can do dynamically sized arrays like this:
cplusplus Syntax (Toggle Plain Text)
main(){ int **myArray; // a run-time sized 2d has trickier initialization syntax int c, r; // number of columns and rows for our array cout << "Simple Two Dimensional Array Demo" << endl; cout << "Enter number of columns:"; cin >> c; cout << "Enter number of rows:"; cin >> r; cout << endl; // initialize a run-time sized two dimensional array myArray = new int*[r]; // create first dimension (of pointers to 2nd dimension) for (int i=0; i<r; i++){ myArray[i] = new int[c]; // create second dimension(s) } // assign test values for(int i=0; i<r; i++) for(int j=0; j<c; j++) myArray[i][j] = i*j; print2dArray(myArray, c, r); }
Last edited by ft3ssgeek : Jun 1st, 2007 at 10:24 pm. Reason: Forgot code tags...
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
- Previous Thread: C++, issues with converting a decimal to fraction
- Next Thread: I'm so confused, ISO/ ANSI C++



Linear Mode