![]() |
|
#1
|
|||
|
|||
Array-filling programming problemHi 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:
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
|
|||
|
|||
Re: Array-filling programming problemYou 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
|
|||
|
|||
Re: Array-filling programming problemWhen I say dynamically creating the array there are two methods that come to mind:
Method 1: CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
Re: Array-filling programming problemCorrection, in the second example from above, you must declare the array like:
CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
Re: Array-filling programming problemThanks 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
|
|||
|
|||
Re: Array-filling programming problemYou'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:
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:
You can never create an array like this: CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
Re: Array-filling programming problemQuote:
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:
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
|
|||
|
|||
Re: Array-filling programming problemI 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 GIDBlog
Observations of Iraq by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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