Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Sorting a C-String Array (C++)
New Posts  All Forums:Forum Nav:

Sorting a C-String Array (C++)

post #1 of 15
Thread Starter 
Hey guys. I'm a first-year CompSci student and would love some help. The last lecture we had was on Structures however we have a guy who sounds like Mario and I didn't understand any of it.

We have an assignment that defines a structure called 'subscriber. In main.cpp, he's then written 'subscriber collection[100]'. We're supposed to have a collection of 100 records.

Now, when calling functions in subrec.cpp (a separate file), from main.cpp, what do I need to include in the function calls and prototypes to allow me to pass the records to that function?

Do the prototypes just have to be void function (struct), or something else?
As for the function calls, would you pass the collection of records like 'collection', or 'subscriber'? Hopefully you understand what I mean.

Pretty basic question probably but yeah, it'd be a huge help.
Edited by jNSK - 5/18/11 at 10:35pm
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
post #2 of 15
Code:
// main.cpp
#include "subrec.h"

int main()
{
    subscriber collection[100];

    functionToPassCollectionTo( collection, 100 );

    return 1;
}


// subrec.h
struct subscriber
{
   // Info about subscriber here
};

void functionToPassCollectionTo( subscriber collection[], int length );



// subrec.cpp
#include "subrec.h"

void functionToPassCollectionTo( subscriber collection[], int length )
{
    for (int n=0; n<length; n++)
    {
          // Do stuff
     }
}

}

Edited by lordikon - 5/17/11 at 10:20pm
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #3 of 15
Thread Starter 
Thank you good sir! Rep+.

I have another question. Nearly finished the assignment but this one is bugging me.

When printing a list of expired subscribers (from the char array collection[100]), I need to print them in alphabetical order.

I get that I have to use strcmp to achieve this, but I'm not sure how to go about it. I could sort the entire collection[] array into alphabetical order, however this would mean it would change the entire array's order.

Someone suggested making a number array and storing the indexes of each surname, making the indexes correspond to the surname's position in the array, then print using that. For example:

A, C, B in the number array would be: 1 3 2 (for alphabetical order).

I've tried implementing the code but it never works, I'm a newbie to this alphabetical-order-char thing. I know how to sort an array of chars however not a collection of chars (i.e. using structures). I'm sure it's the same implementation method however I'm having a hard time spotting what to do.

Anyone that could help me out would be a godsend, really.
Edited by jNSK - 5/18/11 at 10:36pm
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
post #4 of 15
Are you required to use an array, or are you allowed to use whatever type of container you'd like?

If you must use an array, and efficiency is not a concern, I would loop through the array and find the first name in alphabetical order, and then enter it into a new array[100]. Then find the next one in order and put it next in the new array. After you've done that for all 100 entries, copy your new array over the old one, and you're done.

If you can use another contain, like a vector, there are algorithms that will do this for you, however, because this for a class, I'm assuming you guys are using arrays so that you're forced to learn the details of things like how to write logic to determine alphabetical order.
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #5 of 15
Quote:
Originally Posted by jNSK View Post
Thank you good sir! Rep+.

I have another question. Nearly finished the assignment but this one is bugging me.

When printing a list of expired subscribers (from the char array collection[100]), I need to print them in alphabetical order.

I get that I have to use strcmp to achieve this, but I'm not sure how to go about it. I could sort the entire collection[] array into alphabetical order, however this would mean it would change the entire array's order.

Someone suggested making a number array and storing the indexes of each surname, making the indexes correspond to the surname's position in the array, then print using that. For example:

A, C, B in the number array would be: 1 3 2 (for alphabetical order).

I've tried implementing the code but it never works, I'm a newbie to this alphabetical-order-char thing. I know how to sort an array of chars however not a collection of chars (i.e. using structures). I'm sure it's the same implementation method however I'm having a hard time spotting what to do.

Anyone that could help me out would be a godsend, really.
Create a new array...char sorted[# of subsribers here];

Inside a nested for loop...compare 1 c-string at a time to the rest...if that c-string is greater...update a counter variable. At the end of each loop you can insert that c-string into the new array based upon the value of the counter variable...and remember to reset the counter variable back to zero before the next loop begins. You would have to consider the special case where there are identical c-strings.

Of course...efficiency wise this is not good if you have thousands upon thousands of subscribers...but this is only an assignment.

Also, if this is a c++ class you guys should be implementing these projects with classes instead of structs...
Edited by surfbumb - 5/18/11 at 11:08pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #6 of 15
Thread Starter 
Quote:
Originally Posted by surfbumb View Post
Create a new array...char sorted[# of subsribers here];

Inside a nested for loop...compare 1 c-string at a time to the rest...if that c-string is greater...update a counter variable. At the end of each loop you can insert that c-string into the new array based upon the value of the counter variable...and remember to reset the counter variable back to zero before the next loop begins. You would have to consider the special case where there are identical c-strings.

Of course...efficiency wise this is not good if you have thousands upon thousands of subscribers...but this is only an assignment.

Also, if this is a c++ class you guys should be implementing these projects with classes instead of structs...
Yeah, I guess that could work. We haven't learned about classes yet - it's my first semester of C++. The structure was already put in place at the start of the assignment (by the lecturer) and we have to work using it.

We have to use that character array inside a structure (i.e. 'subscriber collection[100]', where subscriber is the name of the struct).

So, what you're saying is, loop through with each value using strcmp() to find which of the strings are greater than it, and increase the count each time strcmp() returns a value greater than 0? Yeah, I guess that would work. Surnames are unique, there can't be more than one of one surname so that's not an issue - like I said, just the basics.

You're right, lordikon, that's exactly what they're doing. Making us learn the hard way so we can appreciate the easier ways.

Quote:
Originally Posted by lordikon
If you must use an array, and efficiency is not a concern, I would loop through the array and find the first name in alphabetical order, and then enter it into a new array[100]. Then find the next one in order and put it next in the new array. After you've done that for all 100 entries, copy your new array over the old one, and you're done.
Can you elaborate on how you'd go about looping through the array finding the first name in alphabetical order? The only way I could think of doing that at this stage is search for the character A, strcmp() the first character of each string and if it returns 0 then strcmp() the second character to see which of those two A's is before which alphabetically. Is there a more efficient way of doing this?

We've only learned the basics of string manipulation so far, such as strcmp, strlen, etc. You've been a big help so far, both of you.
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
post #7 of 15
like lordikon said...inside the struct you have your subscriber array of 100...create a new array inside a function with whatever approach you want...you can use my approach by using a counter variable...then simply copy that array to the subscriber array.

Code:
int count = 0;
char newarray[100];

for(int i = 0; i < 100; ++i){

       for(int p = 0; p < 100; ++p){                           //this loop does all 100 comparisons...per element.
             if(strcmp(subarray[i], subarray[p]) > 0)
                    ++count;
        }

        newarray[count] = subarray[i];       //place element into correct position
        count = 0;                                      //reset count      


}


for(int i = 0; i < 100; ++i)
         subarray[i] = newarray[i];          //copies all of newarrays elements to subarrays

strcpy(subarray, newarray);                //or you can use strcpy...but your teacher might want to see the other way...not sure

Edited by surfbumb - 5/19/11 at 11:17am
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #8 of 15
Alright, new strategy.

Check two elements in the array, if those two are not in the right order, swap their positions and then move forward one index and repeat. If you make it through the entire array without swapping then you're done, otherwise when you get to the end, start over at the beginning and make another pass.

So we have this array (I'll use numbers instead of words)

4 - 2 - 1 - 3

We compare the first two elements, 4, and 2. They're out of order so we swap them, now we have.

2 - 4 - 1 - 3

Now we move forward one index, and compare 4 and 1. Swap those:

2 - 1 - 4 - 3

Move forward one index, compare 4 and 3. Swap those:

2 - 1 - 3 - 4

We've reached the end, make another pass, starting back at 2 and 1, swap those:

1 - 2 - 3 - 4

And we're done.

If you'd like to read about efficient sorting algorithms, check these out:
http://en.wikipedia.org/wiki/Quicksort
http://en.wikipedia.org/wiki/Heapsort

If you want something similar to what I described above:
http://en.wikipedia.org/wiki/Bubble_sort
Edited by lordikon - 5/19/11 at 1:59pm
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #9 of 15
Thread Starter 
Quote:
Originally Posted by surfbumb View Post
like lordikon said...inside the struct you have your subscriber array of 100...create a new array inside a function with whatever approach you want...you can use my approach by using a counter variable...then simply copy that array to the subscriber array.

Code:
int count = 0;
char newarray[100];

for(int i = 0; i < 100; ++i){

       for(int p = 0; p < 100; ++p){                           //this loop does all 100 comparisons...per element.
             if(strcmp(subarray[i], subarray[p]) > 0)
                    ++count;
        }

        newarray[count] = subarray[i];       //place element into correct position
        count = 0;                                      //reset count      


}


for(int i = 0; i < 100; ++i)
         subarray[i] = newarray[i];          //copies all of newarrays elements to subarrays

strcpy(subarray, newarray);                //or you can use strcpy...but your teacher might want to see the other way...not sure
This one seems like it could work.

I wouldn't even have to use the last strcpy - I don't have to copy the arrays since I only have to print them out. I'll give this a try when I get into Uni later today.

I tried doing what you suggested, lordikon, however it wasn't working. I probably had an error in a loop that I failed to spot. I suppose these things come in time.

I think I'll try to implement both of your methods (if anything, for some practice for myself) and choose whichever uses the least lines.

Thanks again for all your help, I'll let you know how I go!
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
post #10 of 15
Quote:
Originally Posted by jNSK View Post
This one seems like it could work.

I wouldn't even have to use the last strcpy - I don't have to copy the arrays since I only have to print them out. I'll give this a try when I get into Uni later today.

I tried doing what you suggested, lordikon, however it wasn't working. I probably had an error in a loop that I failed to spot. I suppose these things come in time.

I think I'll try to implement both of your methods (if anything, for some practice for myself) and choose whichever uses the least lines.

Thanks again for all your help, I'll let you know how I go!
if that is the case then it would simply be this:
Code:
int count = 0;
char newarray[100];

for(int i = 0; i < 100; ++i){

       for(int p = 0; p < 100; ++p){      //this loop does all 100 comparisons...per element.
             if(strcmp(subarray[i], subarray[p]) > 0)
                    ++count;
        }
        newarray[count] = subarray[i];       //place element into correct position
        count = 0;                                      //reset count      
}

for(int i = 0; i < 100; ++i)
         cout<< newarray[i] << endl;            //print out the ordered list of names

let me know how things go...if you need this done i could use my ide/compiler to get it working for ya.
Edited by surfbumb - 5/19/11 at 3:16pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Sorting a C-String Array (C++)