Sorting an array of integers
Dec 9, 2010 at 2:55pm UTC
Hello.
I am trying to write a program that will sort an array of integers descendingly to their numbers of divisors and I have some problems.
Here's what I've done so far:
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
#include <iostream>
using namespace std;
// funkcija za izpis elementov polja
void izpisElementov (int stevila[], int vel_polja)
{
for (int i = 0; i < vel_polja; i++) { //
cout << stevila[i] << " " << endl; //
}
}
// funkcija zamenja vrednosti v spremenljivkah prvi in drugi
void Zamenjaj (int & prvi, int & drugi)
{
int zacasni = prvi;
prvi = drugi;
drugi = zacasni;
}
void urediStevce(int stevila[], int vel_polja)
{
int stevec[vel_polja];
for (int i = 0; i < vel_polja; i++) {
for (int j = 0; j < vel_polja; j++) {
for (int k = 1; k <= stevila[j]; k++) {
if (stevec[j] % k == 0) {
stevec[j]++;
}
if (stevec[j] < stevec[j - 1]) {
Zamenjaj(stevila[i], stevila[i - 1]); }
}
}
}
}
int main ()
{
int vel_polja;
cout << "Vpisi velikost polja:" << endl;
cin >> vel_polja;
int stevila[vel_polja];
cout << "Vpisi stevila:" << endl;
for (int i = 0; i < vel_polja; i++) {
cin >> stevila[i];
}
cout << "Izpis stevil:" << endl;
urediStevce(stevila, vel_polja);
izpisElementov(stevila, vel_polja);
return 0;
}
I just don't know how to correctly write a function that will sort the integers descendingly..
Dec 9, 2010 at 3:51pm UTC
I assume you have your comments in Slovenian, is that right? It looks like you have a very good start. Does this code work? I think you have a classical bubble sort.
Dec 9, 2010 at 4:08pm UTC
Yes, my comments are Slovenian.
The code works, but the result isn't correct.
1 2 3 4 5 6 7 8 9 10
Vpisi velikost polja:
5
Vpisi stevila:
1 2 3 4 5
Izpis stevil:
2
2293408
3
5
4
Topic archived. No new replies allowed.