Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › populating a 2 dimensional array
New Posts  All Forums:Forum Nav:

# populating a 2 dimensional array

OK so i used this formula to figure out the total number of groups possible with the given number of people which i have an array of names that is n
so n.length is the total number of people pepNum is the group size and num will be the maximum possible groups with no duplicate groups order doesn't matter.

for(int i=((n.length-pepNum)+1);i>0;i--){
num+=i;
}

using this array for the groups i need to fill it with the possible groups
groups = new String[pepNum][num];

this is basically how the groups should be populated with an example of groups size of three and group size of five

//String[] = {Bob Henry Joe Sally Jane Jimmy Jess Sissy}
// 0 1 2 3 4 5 6 7
//all possible groups with no duplicate groups order doesn't
//mater 0 1 2 = 2 0 1
// with group size of 3
//0 1 2
//0 1 3
//0 1 4
//0 1 5
//0 1 6
//0 1 7
//1 2 3
//1 2 4
//1 2 5
//1 2 6
//1 2 7
//2 3 4
//2 3 5
//2 3 6
//2 3 7
//3 4 5
//3 4 6
//3 4 7
//4 5 6
//4 5 7
//5 6 7
//with group size of 5
//0 1 2 3 4
//0 1 2 3 5
//0 1 2 3 6
//0 1 2 3 7
//1 2 3 4 5
//1 2 3 4 6
//1 2 3 4 7
//2 3 4 5 6
//2 3 4 5 7
//3 4 5 6 7

What i need to know is how to write a loop if possible to take the names out of n array and put them into my groups array in the proper order I've been messing with it for awhile and its probably something stupid but its starting to give me a head ache

I greatly appreciate any help thank you
Hey. Welcome to OCN! If you use code tags around your code it would make things easier to read. Example:
Code:
``````for(i = 0; i < 5; i++){
do some stuff;
}``````
I'm also a bit confused as to what you're trying to do exactly.

Edit: After looking a little more, I think I understand what you're trying to do, and it seems to me you'll need nested for loops. Example:

Code:
``````for(i = 0; i < size; i++){
for(j = 0; j < size2; j++){
for(k = 0; k < size3; k++){
cout << string[i] << string[j] << string[k];
}
}
}``````
This is c++ since I haven't learned java and I think that's what you're using. Hope that gives you an idea.
Edited by superhead91 - 4/22/11 at 11:07am
 Sulaco (14 items) Nostromo (Retired) (12 items) MacBook Pro (4 items)
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W
Case
Cooler Master HAF 932
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit
 Sulaco (14 items) Nostromo (Retired) (12 items) MacBook Pro (4 items)
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W
Case
Cooler Master HAF 932
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit
he needs to pull out names and put them in each possible order. I am too far out of C to help without being at home next to my book shelf.

but it would start by pulling the first name and pull names that are not equal to the first
then incriment to the next name and repeat.
 My System (16 items)
CPUMotherboardGraphicsGraphics
2600K @ 4.7GHz Asus P8P67 B3 GTX 580 EVGA Hydro Copper 2 GTX 580 EVGA
RAMHard DriveOptical DriveOS
4x4GB G.Skill Ripjaws bunch of 'em Blu-Ray For movies Windows 8.1
MonitorMonitorKeyboardPower
24.4" Hans G HH251 X2 Yamakasi DS270  Blah 1000watt Super Flower
Built into Desk Microsoft SideWinder X8 Comfy one... Creative Extreme Gamer
 My System (16 items)
CPUMotherboardGraphicsGraphics
2600K @ 4.7GHz Asus P8P67 B3 GTX 580 EVGA Hydro Copper 2 GTX 580 EVGA
RAMHard DriveOptical DriveOS
4x4GB G.Skill Ripjaws bunch of 'em Blu-Ray For movies Windows 8.1
MonitorMonitorKeyboardPower
24.4" Hans G HH251 X2 Yamakasi DS270  Blah 1000watt Super Flower
Built into Desk Microsoft SideWinder X8 Comfy one... Creative Extreme Gamer
Hmmm... I googled finding all permutations of string and it seems like there was a lot of talk of using recursive functions. I confess I haven't done much with recursive functions so I can't give you a function off the top of my head, but maybe you could try and think of how to do it recursively.
 Sulaco (14 items) Nostromo (Retired) (12 items) MacBook Pro (4 items)
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W
Case
Cooler Master HAF 932
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit
 Sulaco (14 items) Nostromo (Retired) (12 items) MacBook Pro (4 items)
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W
Case
Cooler Master HAF 932
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit
Quote:
 Originally Posted by superhead91 Hey. Welcome to OCN! If you use code tags around your code it would make things easier to read. Example: Code: ``````for(i = 0; i < 5; i++){ do some stuff; }`````` I'm also a bit confused as to what you're trying to do exactly. Edit: After looking a little more, I think I understand what you're trying to do, and it seems to me you'll need nested for loops. Example: Code: ``````for(i = 0; i < size; i++){ for(j = 0; j < size2; j++){ for(k = 0; k < size3; k++){ cout << string[i] << string[j] << string[k]; } } }`````` This is c++ since I haven't learned java and I think that's what you're using. Hope that gives you an idea.
well pretty much what the program dose is get a number from the user of how many people they are entering then it allows them to enter each persons name one at a time then ask them for a group size.

so say they entered 10 people and a group size of 3 it takes those ten people takes three people from that and makes a group

so say {bob joe henry jan sally jack jimmy jeff judd bruce} are the ten people group size is three

so one possible group would be { bob joe henry } or {bob joe jan }

or if they entered a group size of 4 { bob joe henry jan } is the group

now the order dosent mater so { bob joe henry } is the same as { henry bob joe} they are the same group

now i need it to populate an array with all the possible groups as i tried to show above i figured out all the math and stuff and figured i would need nested for loops its just im trying to figure out the exact syntax of the for loops to properly put the names in.

this is the code i used to figure out the maximum number of groups possible with the give number of people (n.length) and the group size (pepNum)
Code:
``````for(int i=((n.length-pepNum)+1);i>0;i--){
num+=i;
}``````
i do that so i can make my group array with group size by max number of groups

and yes its java but what ever u know will be fine the syntax will still be pretty much the same

i dont know if this helped or just made it more confusing let me know:-P
Is there a limit on group size or string size? Using nested for loops for groups of three wouldn't be too bad, but if you're wanting groups of 5 or 6 or more nested for loops could get pretty nasty.
 Sulaco (14 items) Nostromo (Retired) (12 items) MacBook Pro (4 items)
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W
Case
Cooler Master HAF 932
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit
 Sulaco (14 items) Nostromo (Retired) (12 items) MacBook Pro (4 items)
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W
Case
Cooler Master HAF 932
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit
Quote:
 Originally Posted by superhead91 Is there a limit on group size or string size? Using nested for loops for groups of three wouldn't be too bad, but if you're wanting groups of 5 or 6 or more nested for loops could get pretty nasty.
no no limit that's the problem and the number of people in the groups are going to be entered by the user so for loops aren't going to work the group size is going to change every time
Yeah. I think you're gonna have to figure out how to do it recursively.
 Sulaco (14 items) Nostromo (Retired) (12 items) MacBook Pro (4 items)
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W
Case
Cooler Master HAF 932
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit
 Sulaco (14 items) Nostromo (Retired) (12 items) MacBook Pro (4 items)
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W
Case
Cooler Master HAF 932
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit
New Posts  All Forums:Forum Nav:
Return Home
Back to Forum: Coding and Programming
• populating a 2 dimensional array
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › populating a 2 dimensional array