problems with arrays

This is my algorithm explained in a very simple way.
http://img693.imageshack.us/img693/7105/algorithm.png

I know that my problem is a homework but I think it's ok to ask for hints. So I have a graph that is group of people. Every connected node means friendship. I have to split group by coefficient 1:2 so that I would loose least friendships. The way I convert it to computer language is 2d matrix in witch number 0 means not friend and number 1 means friend. This is something I have made myself so far. The algorithm finds the the sum of every row and witch one is max moves all indexes connected to that row to temp[] array. After that the indexes are inserted either to g1[] (the 1st group) or to the g2[].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#include <fstream>
#include <iomanip>
using namespace std;

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm1::readarray(int A[CMax][CMax], int & n, int & m, const char mas[])
{
 ifstream fd(mas);
  fd >> n >> m;
   for(int i = 0; i < n; i++)
    for(int j = 0; j < m; j++)
     fd >> A[i][j];
 fd.close();
}
//------------------------------------------------------------------------
void TForm1::frarray(int A[CMax][CMax], int n, int m)
{
  ofstream fr("Output.txt", ios::app);
  fr << "   ";
 for(int i = 0; i < m; i++)
 fr << setw(4) << i+1;
 for(int i = 0; i < n; i++)
 fr <<"    ";
 fr << endl;
  for(int i = 0; i < n; i++)
 {
   fr << setw(2)<<i+1 << "| ";
   for(int j = 0; j < m; j++)
     fr << setw(3) << A[i][j] << " " ;
   fr << endl;
 }
 fr.close();
}

//---------------------------------------------------------------------------

int TForm1::find1(int A[CMax][CMax], int & n, int & m)
{
 y = 0;
  for (int i = 0; n > i; i++)
   for (int j = 0; m > j; j++){
    if(A[i][j] > 0)
     y = y + 1; }
   return y;
}
void TForm1::morethanzero(int A[CMax][CMax],int C[CMax], int & n, int & m)
{
 for(int l = 0; n > l; l++)
   {
   int kiek = 0;
   for (int j = 0; m > j; j++) {
   if(A[l][j] > 0) {
         kiek++;
        }
     }
     C[l]=kiek;
    }
 }


//------------------------------------------------------------------------
int TForm1::maxinrow(int C[CMax], int n)
{
  int maks = C[0];
  int i, rownumber;
   for (i = 0; i < n; i++){
        if (maks < C[i]){
     maks = C[i];
     rownumber = i;
                        }
                          }
   return maks, rownumber;
}
//------------------------------------------------------------
int TForm1::Temp(int A[CMax][CMax],int & n, int & rownumber)
{
 ofstream fr("Output.txt", ios::app);
  int l = rownumber;
  int sh=0;
  temp[0]= rownumber;
  i = 1;
   for (int j = 0; m > j; j++) {
        if(A[l][j] > 0) {
                temp[i]=j;
                i++;
                        }      }
   for(int k=0; k < i; k++){
        fr << temp[k]+1 << endl;
                           }
     return temp[0];
     fr.close();
}
//------------------------------------------------------------
void TForm1::split(int temp[CMax], int n, int g, int i){
 int sh=0;
 ofstream fr("Output.txt", ios::app);
  for(int k=0; k < i; k++){
    if((n*0.75)>g){
        g1[g]=temp[k]+1; g++;
                  }
    else{
        g2[sh]=temp[k]+1; sh++;
        }
                           }
   fr.close();
}
//------------------------------------------------------------
void TForm1::groups(int g1[CMax],int g2[CMax], int n){
ofstream fr("Output.txt", ios::app);
fr<<"g1= "<<endl;
    for(int gr=0; gr<n+1; gr++){
        if(g1[gr]>0){
                fr<<g1[gr]<<endl;
                    }
                               }
fr<<"g2= "<<endl;
    for(int gr2=0; gr2<n+1; gr2++){
        if(g2[gr2]>0){
                fr<<g2[gr2]<<endl;
                     }
                                  }
    fr.close();
}
//------------------------------------------------------------
void TForm1::clear(int A[CMax][CMax], int & n, int temp[CMax])
{
 ofstream fr("Output.txt", ios::app);
 int l,j;
    for(int k=1; k < i; k++){
        l = temp[0];
        for(int j = 0; n > j; j++) {
        A[j][l] = 0;
        A[l][j] = 0;
                            }}
 fr.close();
}
//------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 ofstream fr("Output.txt");
 readarray(A, n, m, "Data.txt");
 frarray(A, n, m, "Matrix A", "Output.txt");
 int q=0; int g = 0; int sh = 0;
  for(i=0; i < n; i++){
   if(find1(A, n, m)>0){
     morethanzero(A,C, n, m);
     maxinrow(C, n);
     Temp(A,n, eilnr);
     split(temp, n, g, i);
     g=g+i;
     clear(A, n, temp);
     //frarray(A, n, m, "Matrix A", "Output.txt");
     } q++;}
     groups(g1,g2, n);

  fr.close();
   Memo1->Lines->LoadFromFile(CRez);
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  Close();      
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormActivate(TObject *Sender)
{
    ofstream fr(CRez);
       fr.close();

}
//--------------------------------------------------------------------------- 


This code is only good from 1 to 2 cycles. After that it removes to much nodes from matrix. The other problem is that it prints out some nodes twice. Any ideas how to improve the algorithm would be appreciated. Or maybe someone could link me to the algorithm that splits one array into two with coefficient of 1:2.
Last edited on
Topic archived. No new replies allowed.