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 22-Jan-2004, 03:26
Chazza Chazza is offline
New Member
 
Join Date: Jan 2004
Posts: 1
Chazza is on a distinguished road

Arrays


Hi 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  
Old 22-Jan-2004, 07:33
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
It's quite straightforward to use a 2D array. To declare it like so
CPP / C++ / C Code:
#define COL_SIZE 20    
#define ROW_SIZE 30

int array[ROW_SIZE][COL_SIZE];

To print the value at the i-th row & j-th column, you have:
CPP / C++ / C Code:
printf("i-th row & j-th column = %d", array[i-1][j-1]);

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  
Old 23-Jan-2004, 17:36
mak90thug's Avatar
mak90thug mak90thug is offline
New Member
 
Join Date: Jan 2004
Posts: 17
mak90thug is on a distinguished road
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  
Old 23-Jan-2004, 19:09
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
int x[] = {1,2,NULL,4,NULL,NULL,7};

If 0 is not one of your possible numbers, but you have another that is, you could use:
CPP / C++ / C Code:
#define NA -1
int x[] = {1,2,NA,4,NA,NA,7};

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:
struct set{
    int x;
    char valid;
}numbers[SIZE];

numbers[0].valid =1;
numbers[0].x = 1;

numbers[1].valid = 1;
numbers[1].x = 2;

numbers[2].valid = 0;

Hopefully, that helps.
  #5  
Old 23-Jan-2004, 19:44
mak90thug's Avatar
mak90thug mak90thug is offline
New Member
 
Join Date: Jan 2004
Posts: 17
mak90thug is on a distinguished road
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  
Old 23-Jan-2004, 20:10
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
#define NO_GRADE -1

Then when you are parsing or what not you could use:
CPP / C++ / C Code:
if(grade[index] == NO_GRADE)
    //Do something
else
    //Do something else

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  
Old 23-Jan-2004, 20:50
mak90thug's Avatar
mak90thug mak90thug is offline
New Member
 
Join Date: Jan 2004
Posts: 17
mak90thug is on a distinguished road
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  
Old 23-Jan-2004, 21:12
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
#define NO_SCORE -1

Then in your for loop, put in the following:

CPP / C++ / C Code:
if (x[i] == NO_SCORE)
    cout << "N/A ";
else
   cout << x[i] << " ";

Does this work for what you want to do?
  #9  
Old 23-Jan-2004, 21:14
mak90thug's Avatar
mak90thug mak90thug is offline
New Member
 
Join Date: Jan 2004
Posts: 17
mak90thug is on a distinguished road
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  
Old 23-Jan-2004, 21:16
mak90thug's Avatar
mak90thug mak90thug is offline
New Member
 
Join Date: Jan 2004
Posts: 17
mak90thug is on a distinguished road
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 GIDBlogHalfway done! 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
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

All times are GMT -6. The time now is 16:12.


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