GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Nov-2007, 10:46
raziel raziel is offline
New Member
 
Join Date: Nov 2007
Posts: 8
raziel is on a distinguished road

Array-filling programming problem


Hi everybody, novice here, I need some help with this problem that crashes only with certain inputs.

Problem

Write a program that outputs 0 or 1 using arrays according whether the value
of (i + j) is odd or even. i and j are the numbers of the element in the two dimensional array. Example: A[2][5], i=2 j=5.
The input is a number that indicates the dimension of the array.

example provided:

input:5
output:

1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1

I can only use for statements to fill and output the array.

Here is my code:

CPP / C++ / C Code:
#include<iostream>
#include<string>

main()
{
 int a, i, j;

 cout<<"Enter a number: "; cin>>a;
 int A[a][a];

 for(i=1 ; i<=a ; ++i)
    {for(j=1 ; j<=a ; ++j)
        {if((i+j)%2==0)
           {A[i][j]=1;}
         else{A[i][j]=0;}
        }
    }

 for(i=1 ; i<=a ; ++i)
    {for(j=1 ; j<=a ; ++j){cout<<A[i][j]<<" ";}
     cout<<endl;
    }

 system ("pause");
 return 0;
}

This program works fine with inputs from 1 to 5, then with 6 it crashes, it works again with 7 and after that it won't work with any number.
My best guess is that it is because of the program I'm using for compiling (Dev-C++).

Please help me out, i don't know what to do.
Thanks
  #2  
Old 08-Nov-2007, 12:03
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 482
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Array-filling programming problem


You cannot create an array like that. If you don't have a known/constant array size, you would need to create it dynamically with the keyword "new". Also, when you create an array of size n, you would access it with indices from 0 to n-1, not 1 to n.
  #3  
Old 08-Nov-2007, 12:11
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 482
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Array-filling programming problem


When I say dynamically creating the array there are two methods that come to mind:

Method 1:
CPP / C++ / C Code:
int **A;
cout<<"Enter a number: "; cin>>a;
A = new int*[a];
for(i=0;i<a;i++) A[i] = new int[a];
// Now you can access the array like A[i][j], where i and j go from 0 to a-1

// When finished you must have a "delete" for every "new"
for(i=0;i<a;i++) delete[] A[i];
delete[] A;
Or, the other method uses less memory but is less straightforward:
CPP / C++ / C Code:
int *A;
cout<<"Enter a number: "; cin>>a;
A = new int*[a*a];
// Now you can access the array like A[i*a+j], where i and j go from 0 to a-1

// When finished you must have a "delete" for every "new"
delete[] A;
  #4  
Old 08-Nov-2007, 12:13
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 482
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Array-filling programming problem


Correction, in the second example from above, you must declare the array like:
CPP / C++ / C Code:
//A = new int*[a*a]; --not correct
A = new int[a*a]; // correct
  #5  
Old 09-Nov-2007, 08:18
raziel raziel is offline
New Member
 
Join Date: Nov 2007
Posts: 8
raziel is on a distinguished road

Re: Array-filling programming problem


Thanks for the help, it solved the problem. I only needed to use the right assignments for the indices like you said. Tha program works fine now, I checked out what you said about using a constant to initialized an array, but I haven't learned that yet.
Anyway, I'm still concerned about the funny way of crashing of the program.

Thanks for the help and fast reply, you really helped me out.
see you around
  #6  
Old 09-Nov-2007, 08:54
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 482
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Array-filling programming problem


You're welcome. I'm sure that the reason that your program is crashing weird has to do with the way you're attempting to create it. C++ should not even allow that kind of statement to be made.

Explanation:
When creating an array, it can be done in 2 different ways.
1) In the code, you know the size so you can create it without the keyword "new" like so:
CPP / C++ / C Code:
int X[10];

This will create an array of integers of size 10.

2) If you are depending on a variable (such as user input) for the size of the array, you must do it like so:
CPP / C++ / C Code:
int size;
cin >> size;
int* X = new int[size];
// you must also delete it because you used "new"
delete[] X;

You can never create an array like this:
CPP / C++ / C Code:
int size;
cin >> size;
int X[size];
  #7  
Old 09-Nov-2007, 10:49
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Array-filling programming problem


Quote:
Originally Posted by fakepoo
I'm sure that the reason that your program is crashing weird has to do with the way you're attempting to create it. C++ should not even allow that kind of statement to be made....You can never create an array like this:
CPP / C++ / C Code:
int size;
cin >> size;
int X[size];

The Original Poster's system, Dev-C++, uses a version of the GNU C++ compiler.

Although the C++ Standard language specification does not support variable-length arrays, current GNU C++ compilers do. (They support the C99 standard Variable Length Arrays, and they let C++ programmers use them also.)

If people do decide to use this non-standard language extension of the GNU C++ compiler, they should be aware that their code may not be portable to other C++ compilers. The "right" way to do it is by using the new operator, the way that you demonstrated. Since there is a standard way of allocating arrays dynamically, I think that new programmers should stick to the standard rather than using non-standard implementation-dependent methods. (But that's just me---I'm funny that way.)

However...

Since the compiler does support it, that is not the cause of the problem reported by the Original Poster. Really.

The cause of the problem is what you said in your initial response:
Quote:
Originally Posted by fakepoo
Also, when you create an array of size n, you would access it with indices from 0 to n-1, not 1 to n.

Since the original program accesses memory outside the legal range, the behavior is undefined.

That is to say:
It may appear to work as expected.
It may cause erratic behavior like sometimes writing over the values of other variables.
It may crash.

Furthermore...
The behavior may be different for different values of n.
The behavior may be different if that piece of code is put into another program compiled on the same system.
The behavior may be different for different phases of the moon.

Etc.


Trying to analyze when (and/or why) the undefined behavior exhibits certain symptoms might be interesting to some people, but any conclusions drawn from such experiments are completely irrelevant.

Undefined is undefined. Period. Full stop.

Regards,

Dave
Last edited by davekw7x : 09-Nov-2007 at 11:26.
  #8  
Old 09-Nov-2007, 10:57
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 482
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Array-filling programming problem


I did not know that. I have never used such a compiler so, needless to say, I was surprised it even got compiled. Thanks much Dave!
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort in C++ alphabetically wilen C++ Forum 5 20-Apr-2007 14:43
problem with filling a char array in a windows GUI app. ryver C++ Forum 1 13-Aug-2006 08:22
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31
Object Oriented Programming? jake_jeckel C++ Forum 8 30-Oct-2005 12:25
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:10.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.