![]() |
|
#1
|
|||
|
|||
ArraysHi there,
I need to compare each element of a 2D array with the one above it, below it as well as the elements to the left and right in order to find out the value which is maximum. I would appreciate any ideas on how to go about this. Thanks!! |
|
#2
|
|||
|
|||
|
It's quite straightforward to use a 2D array. To declare it like so
CPP / C++ / C Code:
To print the value at the i-th row & j-th column, you have: CPP / C++ / C Code:
Remember, C starts counting from 0, not 1. Your question is ambiguous, what is the point of comparing above & below? What do you wish to achieve? GF |
|
#3
|
||||
|
||||
|
Can I add a question to this question? Please.
Ok, If I have a 2D array with missing values, is it possible to define it. For example: x = {1, 2, not valid, 4, not valid, not valid, 7}; (I know this is not 2D, it is just an example) Is there a special character or symbol I need to use to let C++ know I am not using the 3rd, 5th, and 6th element. I am trying to tinker with something that have many numbers assigned to variable names, and there are certain names that do not have the number. Thanks a bunch. |
|
#4
|
||||
|
||||
|
Hi Mak90thug.
when you define an array, a block of memory is going to be set aside for this array. The value of these items will be arbitrary until they are defined, but they will still exist. A couple of possible options: In C, NULL is defined by 0. For appearance reasons, this would be the best thing to use if possible, ie CPP / C++ / C Code:
If 0 is not one of your possible numbers, but you have another that is, you could use: CPP / C++ / C Code:
If you have to have every number be an option, I think your best bet would be a structure in this case CPP / C++ / C Code:
Hopefully, that helps. |
|
#5
|
||||
|
||||
|
Thanks,
What if I can't have zero as a number in the invalid space? For example, if the class have 4 students; Sam, Cindy, Trish, and Mike. While everyone took the test and got a numerical grade from 0 to 10, Mike had a no-show and requested a make-up test. Therefore, he can't be given a NULL, which is defined to be 0, right? With int x[] = {1,2,NA,4,NA,NA,7} , how would I define NA to be a not applicable number or case. I mean, I want the students' grade report to be something that indicates the right grade for each student. In this case, Mike has a no-grade instead of a 0. Am I confusing you? This is just an example, the program I am trying to write is much more complicated. I am trying to shorten the variable definition to a one liner instead of doing Same = 8; Cindy = 10; Trish = 7; type of declaration. If they're a one line and can all be declared using an array, I can put all of the array into a prototype file called grade.h and #include grade.h for a much more complicate situation. A structure is great for a small program, but it may be too much work to do for a super large program. I think korn shell use symbols such as $ and such to indicate unique things. I was thinking that may be C++ can be written like: x = {1, 2, 3, somesymbol, 5, 6, somesymbol}; I really appreciate your help. Thanks, Kimber |
|
#6
|
||||
|
||||
|
If -1 is not a number that is used then the second example should work just fine. You could simply use the number -1, but for clarity I would use the #define declaritive. It also makes it easier to change if the need arises. You could even say
CPP / C++ / C Code:
Then when you are parsing or what not you could use: CPP / C++ / C Code:
I am not sure I understand your objective completely yet. One thing to keep in mind with a #include directive, is that the file is inserted into the program at compile time. If you change your #include file, you will need to recompile your program. This is not the way to include a data file. There is another thread in here that talks about that. As far as C++ being able to do x = {...}, that is something that I am unfamiliar with. I have never seen anything like that work. I did a quick test in g++, because there are alot of things that I don't know and it would not compile. Maybe I am missing your question. |
|
#7
|
||||
|
||||
|
This is what I have:
#include <iostream.h> #include <string.h> int main() { int x[6] = {1, 2, 3, NULL , NULL , 6}; int i; for (i = 0; i < 6; i++) { cout << x[i] << " "; } cout << endl; return 0; } Instead of NULL, I would like a invalid option to go into the array. |
|
#8
|
||||
|
||||
|
That looks good. BTW - this forum has one of the coolest features that I have ever seen to post code. If you put a C enclosed by [] before your code and and a /C enclosed by [] after your code it will highlight it and make it much more readable.
Okay see if this works for what you want to do. Put this at the top: CPP / C++ / C Code:
Then in your for loop, put in the following: CPP / C++ / C Code:
Does this work for what you want to do? |
|
#9
|
||||
|
||||
|
With the code above, I'll get the output of:
1 2 3 0 0 6 I want it to be 1 2 3 6. I know you will probably think "why the heck she doesn't just use x[4] array instead of x[6] array and only wants 4 elements. The reason is that this array is dependent another variable. The 2D array I was refering to much earlier will be something like: x[6][2] = { {1,3} , {2,6} , {3,5} , {4,5} , {5,1} , {6,5} } This is a 2D array. Now, instead of having something and neat like that, what if I want to make the row elements to be variables. The C++ will look something like: #include <iostream.h> #include <string.h> int main() { int x[6][2] = { {1,3} , {2,6} , {3,5} , {4,5} , {5,1} , {6,5} }; int i; for (i = 0; i < 6; i++) { cout << x[i][1] << endl; } cout << endl; return 0; } Now, imagine if the int i is the variable representing each student. The second column will be their grade. If student number 4, aka i = 3 (c++ start with 0), didn't take a test and receive a no grade. I still have to keep his information in the record, but I can't print 0 for him. I just don't want to have anything to do with him right now; therefore, I want to make his grade non number. So it should be something like: int x[6][2] = { {1,3} , {2,6} , {3,5} , {4,5} , I want nothing here , {6,5} }; Sorry for asking such a strange question. I know there is a symbol or logic out there. I just can't find it in any books I have. For example, I can just be: int x[6][2] = { {1,3} , {2,6} , {3,5} , {4,5} , " " , {6,5} }; The above line is wrong, I just used it as an example. Thank everyone. You're a genius. Thanks, Kimber |
|
#10
|
||||
|
||||
|
Slow girly typer here, I didn't get a chance to kick that last post out before you had a chance to answer.
Thank dsmith, Kimber |
Recent GIDBlog
Halfway done! by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| pointers and arrays | jack | C Programming Language | 4 | 15-Jan-2004 12:27 |
| Extra null element in an array | samtediou | MySQL / PHP Forum | 2 | 11-Dec-2003 11:52 |
| arrays in c | wolfgangaz | C Programming Language | 1 | 26-Oct-2003 04:52 |
| Arrays | Andrew | MySQL / PHP Forum | 2 | 26-Jan-2003 02:50 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The