Bubble sorting (Index exceeds the number of array elements)
Mostra commenti meno recenti
I'm supposed to write a program that bubblesorts surnames. Porg file contains surnames while Klucze file contains keys assigned to surnames.
global Staff
global keys
fid = fopen ('Porg.csv')
fid2 = fopen ('Klucze.csv')
i=2
while ~feof(fid); ~feof(fid2)
sd=fgetl(fid);
zd=fgetl(fid2)
tet(1:3)=strsplit(sd,';');
zet(1:3)=strsplit(zd,' ');
Staff(i).surname=tet(1);
Staff(i).name=tet(2);
keys(i).key=zet(1);
i=i+1;
end
for x=2:(i-1)
v(1)=Staff(x).surname;
v(2)=Staff(x).name;
v(3)=keys(x).key;
disp(v);
end
This code displays surnames together with names and assigned keys. Tried to bubble sort it by changing the last loop, and I keep getting an error "Index exceeds the number of array elements (1)". Does anyone know how to fix it? Probably it's a rookie mistake (I'm a newbie when it comes to programming)
g = length('Porg.csv')
for j=2:(g-1);
if j(g)>j(g+1);
temp=Staff(j).surname;
Staff(j).surname = Staff(j+1).surname;
Staff(j+1).surname = temp
end
v(2)=Staff(h).name;
v(3)=keys(h).key;
disp(v);
end
3 Commenti
madhan ravi
il 9 Nov 2018
Modificato: madhan ravi
il 9 Nov 2018
Upload porg.csv
Stephen23
il 9 Nov 2018
What do you expect this to do?:
g = length('Porg.csv')
Please upload some sample data files by clicking the paperclip button.
Karou
il 9 Nov 2018
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Shifting and Sorting Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!