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++) - Page 2

post #11 of 15
Thread Starter 
Quote:
Originally Posted by surfbumb View Post
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.
It works! Thank you!

I had to modify it a little:

Code:
for(int i = 0; i < numRecords; ++i){

for(int p = 0; p < numRecords; ++p){      //this loop does all 100 comparisons...per element.
I only want it to do it for the number of records (numRecords) because it was printing out a lot of garbage (when there were no records in the element it was checking). It's working perfectly.

I have another question, related to coding in general:

In loops, such as this, what difference does 'i++' and '++i' make? I know the difference between the two, however when I switch them in the above for loop's, the loop behaves exactly the same? I just need to understand the difference within a loop. Wouldn't '++i' increment 'i' before the loop is executed? Or am I getting confused here.

In fact, when I change i++ to ++i and p++ to ++p in my edited bit of code above, it gives me a Segmentation fault.
Edited by jNSK - 5/19/11 at 5:20pm
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 #12 of 15
Quote:
Originally Posted by jNSK View Post
It works! Thank you!

I had to modify it a little:

Code:
for(int i = 0; i < numRecords; ++i){

for(int p = 0; p < numRecords; ++p){      //this loop does all 100 comparisons...per element.
I only want it to do it for the number of records (numRecords) because it was printing out a lot of garbage (when there were no records in the element it was checking). It's working perfectly.

I have another question, related to coding in general:

In loops, such as this, what difference does 'i++' and '++i' make? I know the difference between the two, however when I switch them in the above for loop's, the loop behaves exactly the same? I just need to understand the difference within a loop. Wouldn't '++i' increment 'i' before the loop is executed? Or am I getting confused here.

In fact, when I change i++ to ++i and p++ to ++p in my edited bit of code above, it gives me a Segmentation fault.
Good job on getting it finished...i bet it felt somewhat satisfying. The difference between ++i and i++ is this:

i = 1;
j = ++i; //increments i...returns that value which is 2...assigns 2 to j

(j = 2, i = 2)

i = 1;
j = i++; //increments i...but returns the pre-incremented value...and assigns it to j

(j = 1, i = 2)

In for loops...i++ and ++i dont really matter as they are preferences since when you drop down after a loop the variable will be the same value regardless if you use prefix or postfix...it counts whenever you are assigning or reading...

ex...cout << i++....vs. cout << ++i
Edited by surfbumb - 5/19/11 at 11:09pm
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 #13 of 15
Quote:
Originally Posted by surfbumb View Post
Good job on getting it finished...i bet it felt somewhat satisfying. The difference between ++i and i++ is this:

i = 1;
j = ++i; //increments i...returns that value which is 2...assigns 2 to j

(j = 2, i = 2)

i = 1;
j = i++; //increments i...but returns the pre-incremented value...and assigns it to j

(j = 1, i = 2)

In for loops...i++ and ++i dont really matter as they are preferences since when you drop down after a loop the variable will be the same value regardless if you use prefix or postfix...it counts whenever you are assigning or reading...

ex...cout << i++....vs. cout << ++i
Depending on what you're incrementing, a prefix incrementation can be more efficient. With primitive types and stl containers it shouldn't matter either way. Generally I've found it good practice to always use the prefix increment whenever it's not in a statement where its value is evalutated, and in those cases I just pick whichever is most convenient.

Here's a good discussion about it, answer #6 hits the nail on the head: http://stackoverflow.com/questions/2...n-i-and-i-in-c
Custom types may have an overloaded post-increment operator which will often do a copy, which can be more expensive. His quote is basically what I was getting at:
Quote:
A better answer is that ++i will sometimes be faster but never slower.

Edited by lordikon - 5/19/11 at 11:22pm
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 #14 of 15
Thread Starter 
Ah, I see. So in the context of a 'for' loop, it doesn't matter either way then. At least that's what I can gather from your answer. I will try and use ++i whenever possible - maybe I should get some practice on it by writing some easy programs.

You guys still didn't answer my question, though - what's the segmentation fault all about when I change from i++ to ++i as the operation in the for loop?
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 #15 of 15
Quote:
Originally Posted by jNSK View Post
Ah, I see. So in the context of a 'for' loop, it doesn't matter either way then. At least that's what I can gather from your answer. I will try and use ++i whenever possible - maybe I should get some practice on it by writing some easy programs.

You guys still didn't answer my question, though - what's the segmentation fault all about when I change from i++ to ++i as the operation in the for loop?
The compile will literally output the same machine code for an int in a for loop with either i++ or ++i, so there should be no difference between the two.
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
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++)