Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,234 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,638 people online right now. Registration is fast and FREE... Join Now!




Blank Array Output. Why?

 
Reply to this topicStart new topic

Blank Array Output. Why?

mitrub
13 Mar, 2007 - 05:05 PM
Post #1

New D.I.C Head
*

Joined: 13 Mar, 2007
Posts: 1


My Contributions
Given a character array, I am writing a function that returns the 18 sub-clusters of a given length that come closest to having given numbers of each character type present in the original array (for example, 18 a's, 10 b's, 12 c's, and 5 d's). These 18 clusters must also satisfy three other properties that are irrelevant to the problem I'm having.

To do this, I iterated 18 for-loops (I am too lazy to define this function recursively until it works). For every set of 18 sub-cluster starting points, I assigned the 18 subclusters to a given array, evaluated the array, and if the array came closest to satisfying the four aforementioned conditions, I assigned its subclusters to an 18 position array called bestclustergroup.

When I print bestclustergroup outside of the iterated for-loop, the execution window remains blank. I can't figure out why, considering that bestcluster group is initialized for every set of 18 cluster starting positions.

If I can determine the bug in the reduced test-case below, I'm confident my program will work:
CODE

#include <iostream>
using namespace std;

main (){
int nofposinbestgroup = 18 * 2;
char bestclustergroup [nofposinbestgroup];
char allsingleclusters[] = "aaabacadaeafbabbbcbdbebfcacbcccdcecfdadbdcdddedfeaebecedeeeffafbfcfdfeff";
int positionlimit = 36;

for (long int first = 0; first < positionlimit - 17; ++first){
for (long int second = first + 1; second < positionlimit - 16; ++second){
for (long int third = second + 1; third < positionlimit - 15; ++third){
for (long int fourth = third + 1; fourth < positionlimit - 14; ++fourth){
for (long int fifth = fourth + 1; fifth < positionlimit - 13; ++fifth){
for (long int sixth = fifth + 1; sixth < positionlimit - 12; ++sixth){
for (long int seventh = sixth + 1; seventh < positionlimit - 11; ++seventh){
for (long int eighth = seventh + 1; eighth < positionlimit - 10; ++eighth){
for (long int ninth = eighth + 1; ninth < positionlimit - 9; ++ninth){
for (long int tenth = ninth + 1; tenth < positionlimit - 8; ++tenth){
for (long int eleventh = tenth + 1; eleventh < positionlimit - 7; ++eleventh){
for (long int twelfth = eleventh + 1; twelfth < positionlimit - 6; ++twelfth){
for (long int thirteenth = twelfth + 1; thirteenth < positionlimit - 5; ++thirteenth){
for (long int fourteenth = thirteenth + 1; fourteenth < positionlimit - 4; ++fourteenth){
for (long int fifteenth = fourteenth + 1; fifteenth < positionlimit - 3; ++fifteenth){
for (long int sixteenth = fifteenth + 1; sixteenth < positionlimit - 2; ++sixteenth){
for (long int seventeenth = sixteenth + 1; seventeenth < positionlimit - 1; ++seventeenth){
for (long int eighteenth = seventeenth + 1; eighteenth < positionlimit; ++eighteenth){
    
long int clusterpositions[18] = {first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth, eleventh, twelfth, thirteenth, fourteenth, fifteenth, sixteenth, seventeenth, eighteenth};
//long int clusterpositions[18] = {positionlimit - 18, positionlimit - 17, positionlimit - 16, positionlimit - 15, positionlimit - 14, positionlimit - 13, positionlimit - 12, positionlimit - 11, positionlimit - 10, positionlimit - 9, positionlimit - 8, positionlimit - 7, positionlimit - 6, positionlimit - 5, positionlimit - 4, positionlimit - 3, positionlimit - 2, positionlimit - 1};
for (int position = 0; position < 18; ++position){
for (int lettercounter = 0; lettercounter < 2; ++lettercounter){
int whereinallclusters = clusterpositions[position]*2 + lettercounter;
int whereinbest = position*2 + lettercounter;
char character = allsingleclusters[whereinallclusters];
bestclustergroup[whereinbest] = character;
}}
}}}}}}}}}}}}}}}}}}

for (int where = 0; where < nofposinbestgroup; ++where)
cout << bestclustergroup[where];
return bestclustergroup;

return 0;
}

If anybody could figure out why this program returns me a blank, I would greatly appreciate it. This programming bug has baffled me, a beginner, for days. Thanks.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Blank Array Output. Why?
15 Mar, 2007 - 03:32 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
well first off I wonder if you are thinking clearly here... Do you realize how many times the inside of your program will execute? As far as I can tell your program should come up with an output... eventually...


The outter loop executes 19 times, the next loop executes 19*19 times, the next loop 19*19*19, and there are 18 such loops each I belive loops though 19 times, so that is 19^18 = 104127350297911241532841, count in the inner two loops 18*2 imples 3748584610724804695182276 interations... basicly this is a VERY inefficient algorithm... so lets imagin that the innards of the loop executed in 1 clock tick... on my 2.0 Ghz processor, in a perfect world, (3.748585x10^23 instructions) /2x10^9 (instructions/sec) = (1.87429x10^15 sec) => (1.87429x10^15 sec) /(31536000 sec/year) = 5.94334x10^7 years... lets just say I think it will be a while before you find out if your program works or not...

Even if I analized your loops a little off, your program sill works on an exponential scale. Think up a better approch.

What exactly are you trying to do? In data mining we use a lot of types of clustering and there are MUCH better algorithems than this.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 04:02PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month