sort char and int

Write your question here.
i'm trying to sort the value of hasil ,it already works but what i want is in the same time char cobac also sort when hasil sorted.

when hasil sorted the others,cobac not sorted as hasil.how i can put it works?

best regards
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
  Put the code you need help with here.
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <ctime>
using namespace std;

class data{

public :

	char *cobac[100],*t;
	int x, y,max,xy;
	int kuadrata,kuadratb,kuadratc,kuadratd,kuadrate,kuadratf,kuadratg,kuadrath,hasil[],tr[40];
	string trainData;
	int a,b,c,d,e,f,g,h,i,coba,z,line,temp;
	int j,k,l,m,n,o,p,q,r;
	int datatest();
	int splitdata();
	void knn();
	int sort(),maxl();
};
int data::datatest(){
j=3;
k=1;
l=1;
m=1;
n=1;
o=1;
p=1;
q=1;
}
int data::splitdata(){
int start_s=clock();
datatest();
	//cout << j*5 << endl;
ifstream fin("kddcup.txt");	
r=0;line=0;
	while(getline(fin,trainData))
{	
     // do whatever to the string "line"
	//cout << line << "  "<< trainData << endl;
	 
	char *pcg=strdup(trainData.c_str());
	//pcg=(char *)malloc(42);
	trainData = strtok(pcg,",");
i=1;
  while (pcg != NULL)
  {
    int tint=atoi(pcg);
	int tmp[43];
	
//    		printf ("%s",pcg);

		tmp[i]=tint;
		
    		pcg = strtok (NULL, ",");
//		printf("nilai = %d \n",tmp[i]);

if   (i==5) {
	a=tmp[i];
	//kuadrata=a*j;	
	kuadrata=pow((a-j),2);
	//printf("nilai a= %d \n",kuadrata);

	}
else if(i==6) {
	b=tmp[i];	
	kuadratb=pow((b-k),2);	
	//printf("nilai b= %d \n",kuadratb);	
//printf("nilai b = %d \n",b);
}
else if(i==23){
	c=tmp[i];	
	kuadratc=pow((c-l),2);
	//printf("nilai c= %d \n",c[1]);
	//
}
else if(i==24){
	d=tmp[i];
	kuadratd=pow((d-m),2);	
	//printf("nilai = %d \n",d[1]);
	}
else if(i==32){
	e=tmp[i];
	kuadrate=pow((e-n),2);	
	//printf("nilai = %d \n",e[1]);
	}
else if(i==33){
	f=tmp[i];
	kuadratf=pow((f-o),2);	
	//printf("nilai = %d \n",f[1]);
	}
else if(i==36){
	g=tmp[i];
	kuadratg=pow((g-p),2);	
	//printf("nilai = %d \n",g[1]);
	}
else if(i==37){
	h=tmp[i];	
	kuadrath=pow((h-q),2);
	//printf("nilai = %d \n",h[1]);
	}
else if(i==41){
	t=pcg;
}

	i++;


  }//while pcg

hasil[line]=sqrt(kuadrata+kuadratb+kuadratc+kuadratd+kuadrate+kuadratf+kuadratg+kuadrath);
cobac[line]=t;
line++;

}//while line
max=line;	

sort();
//printf("%s",cobac[x]);printf("%d \n",hasil[x]);
printf("total baris %d \n",max);
int stop_s=clock();
cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000000000 << endl;
}//fungsi split

int data::sort(){


for(z=0;z<max;z++){
	for(y=0;y<max-1;y++){
		if(hasil[y]>hasil[y+1]){
	temp=hasil[y+1];
	hasil[y+1]=hasil[y];
	hasil[y]=temp;

		}//printf("%d %d \n",y,temp);
	}//printf("%d %d \n",y,temp);
}

//printf("%d %s %d \n",line,cobac[y],hasil[0]);
for(x=0;x<max;x++){printf("%s %d \n",cobac[x],hasil[x]);
}
}

int main(){

	data access;
	access.splitdata();
	//access.splitdata(2,1,1,1,1,11,1,1);
	
	//printf("%d",z.a);
}
Last edited on
Topic archived. No new replies allowed.