Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 05-02-2003, 03:22 PM   #1
Member (6 bit)
 
Newbuilder's Avatar
 
Join Date: Mar 2003
Posts: 34
Angry Fire C++ Problem

I am working on a problem, and hoped I might find some help here.

I am trying to cull data from an array. The array is made up of a number (n) of structures:

struct Competitor
{
int age;
float score;
charname[6];
};

I am using a pointer to identify the array, skater_pointer.

skater_pointer = new Competitor[n];

What I would like to be able to do is to use a function to ask the user the lowest score they want to see. Then from that take all the scores >= and create a new array with the new size (n), and be able to use the newly sized array in the other functions that already exsist ( Getdata, Showdata, Sortdata ).


Any thoughts?

spinning with pointers,
Jeff
Newbuilder is offline   Reply With Quote
Old 05-02-2003, 06:23 PM   #2
Member (10 bit)
 
Join Date: Jul 2002
Location: University of California, Santa Barbara
Posts: 800
Looking at what you're doing, I would probably use a class rather than a struct, and integrate all such functions inside of it. That way you can have a variable that specifies the length of an array, because, correct me if I'm wrong, if you try to access array[100] when the array doesn't have that many elements, it will return an error. If you have the variable specifying the length, you would use that variable in any function you write for it, and it wouldn't go over the limit. Plus class's are simply a more elegant way to write code IMO.

That said, you'll want to create another array, and go through the array one element at a time, checking it against the minimum value. If it is >= the min value, put a copy of that entry in the new array.
mattg2k4 is offline   Reply With Quote
Old 05-02-2003, 07:34 PM   #3
Member (6 bit)
 
Newbuilder's Avatar
 
Join Date: Mar 2003
Posts: 34
The problem is is that these are specific class instructions per the instructor, so I am bound to using the struct. Aside from that I am going to pass n (# of elements in the array defined by user) to the culling function by reference ( int &n). This will allow the function to change n in main as per the new number of elements. I will have the function go through and compare skater_pointer[i].score to the thresh hold assigned by the user. Everytime it gets one it will put it in a temp array [j]. Incrementing both as the loop processes ( j only when there is a hit) I can then just assign j to n and have it change in main as it is by reference. My problem now is that the temp array created in the function is trapped? The other functions will still use skater_pointer, while I want that array to be replaced by the temp array. Can I delete skater_pointer, get a new skater_pointer of size j and then re transfer the data back to skater_pointer? Is there an easier way? Is there a way to have skater_pointer stop pointing at its array and point to the new temp array? Sorry if I am making no sense, but this is still all new to me and Im learning.

If anyone has any advice I would definitely appreciate it and if what I have written is not clear then please tell me so that I can clarify.

Thanks again,
Jeff
Newbuilder is offline   Reply With Quote
Old 05-07-2003, 11:47 PM   #4
Member (7 bit)
 
LoveJones's Avatar
 
Join Date: Sep 2002
Posts: 93
Assuming the culling function is called from main, what you want to do is pass it the int reference from main. This way, any changes made to the array are actually changed in memory. You can also do this with a pointer by doing the traditional array declaration, and passing the name as the parameter as the name itself is simply a pointer to the first element in the array.

int array[50];

cull( array ); //passes a pointer into the cull function



The logic for what you are trying to do is very simple. Once you pass the empty array/reference into the culling function, you just fill it with all values that are >= the user specified value. It sounds like you have that part figured out, you just have an issue with scoping.

So basically all you need to do is pass the pointer or reference of an empty array that was instantiated in main, to the culling function and fill it up. That way you can manipulate it in other functions.

Btw, your professor seems like he is teaching you guys C, but for some reason making you use C++.
LoveJones is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:58 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2