Looping function , implement own logic

when i choose for the first array which is [0][0]

the entire row and column cannot be choose and compare. something like this
1
2
3
4
5
11 12 18 40
14 15 13 22
11 17 19 23
17 14 20 28


so i have to compare each COLUMN and not ROWS
for the second column which only contain 15 , 17 , 14 so i choose 14 and add to another variable, after wards i compare 13 19 20 again and choose the smallest 13 too add into another variable and last i compare 22 23 28 and get the smallest to add up together which is 22 then my answer will be

int answer = 11 + 14 + 13 + 22 = 60 following is my coding but fail to run. i try to implement algorithm but fail. but should be correct mostly . can someone tell me where i going wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void function(){
bool *stars = new bool[4];
int totalMin = 0;
for( int y = 0 ; y < p ; y++ ){
    int min,iMin = -1;
        for( int x = 0 ; x < j ; x++ ){
        if( !stars[y] || (iMin < 0 || min > minimumCost[y][x]) ){
            min = minimumCost[y][x];
            cout << "minimum : " << min << endl;
            iMin = min;
            totalMin += min;
        }
        stars[iMin] = 1;
    }
}

cout << "Total : " << totalMin << endl;
}


Last edited on
The value of any element of stars is unspecified when the array is allocated. You make no effort to ensure they contain a meaningful value before using the array.

Also, you make no effort to exclude any column or row as you indicate you should in the OP.

If the values in the array minimumCost correspond to those you show in your post, then stars[iMin] will be out of the bounds of the stars array and cause undefined behavior.

There is no reason to have a variable named min and a variable named iMin. They differ in value in only one place in your code and that is only because you don't initialize min.
Last edited on
So you want to scan the entire first row, choose the smallest, then that first row plus the column where you found the smallest cannot be used again? Then you want to go through each of the remaining columns (3 x 1) and choose the smallest from these rows as well?
some senior teach me and i cant think what is the boolean for. that's why i having problem at here.

i can't think of it and @smac89 . not to choose the smallest value at entire row. at first i will choose the [0][0] value. first value in 2d dynamic . in this time not yet compare

so after i choose the first value/ the entire row and column of the values cannot be choose. will do like this table

1
2
3
4
5
11 12 18 40
14 15 13 22
11 17 19 23
17 14 20 28


and then only compare remaing columns ( 3x1) and choose the smallest value from EACH COLUMN . not rows ya. is column. so far i oni done until that. and no any idea afterwards...


@cire. do you have any idea for me? can help me make some changes?? too tough for basic for me.... im just diploma student
any idea?
Topic archived. No new replies allowed.