How to sort an array? (You can answer in Polish)

0

Hi guys.
I am a beginner and I have a problem with my assignment.
20201110_184825.jpg

I tried to write a code on my own. I was able to create an array on length n (with random numbers), but when I tried to sort it, it does not work and I do not know why. Can somebody help me, please?

I understand the Polish language (maybe 95%), but I cannot write without mistakes, so I decided to ask in English. So you can answer in Polish. Thanks a lot!


#include <iostream>
#include <cstdlib> 
#include <ctime>									// define "time" in 32th row
#include <cassert>
#include <algorithm>

using namespace std;

int main() {

	//   CREATE AN ARRAY OF LENGTH "n" AND FILL IT WITH RANDOM NUMBERS


	int n = 0;										// size of an array at the beginning
	int	i = 0;										// for cycle
	int	j = 0;
	int	random_number = 0;							// random number at the beginning
	int* array = nullptr;

	cout << "Please enter a number: ";				// write the message for an user
	cin >> n;										// this allow to change n (in command line)
	array = new int[n];								// change n and save it to "array"
	
	srand((unsigned)time(0));

	for (i = 0; i < n; i++) {						// for cycle from i=0 to i=(n-1)
		random_number = (rand() % n);				/* generate a random number from 0 to n (I want to avoid the situation, 
													   when somebody write n too hight and I set up too small random n (there would be a lot of the same numbers)*/	
		array[i] = random_number;					// it fills array with random numbers
		cout << array[i] << ' ';					
		if ((i + 1) % 10 == 0) {					// write 10 numbers in one row (only for clarity)
			cout << '\n';
		}
	} 

	return 0;

	// SORTING
	sort(array, array + n);
	for (j = 0; j < n; j++) {
	
		//cout << "Sorted array is:" << endl;
		cout << array[j] << " ";

	}
} 
2

Przesuń swojego returna na koniec. Zamiast

    return 0;

    // SORTING
    sort(array, array + n);
    for (j = 0; j < n; j++) {

        //cout << "Sorted array is:" << endl;
        cout << array[j] << " ";

    }

Zrób tak:

    // SORTING
    sort(array, array + n);
    for (j = 0; j < n; j++) {

        //cout << "Sorted array is:" << endl;
        cout << array[j] << " ";

    }
   return 0;

Aczkolwiek wiedz, że sort ze standardu C++ implementuje algorytm quick sort, a w zadaniu masz wymóg użycia sortowania przez wstawianie.

2

do komentarza - najlepiej stąd. na poczatku jest wprowadzenie z wytlumaczeniem krok po kroku, pozniej przyklad kodu

0

@Zrorpaczona: czego używasz? Jakie IDE, kompilator, system?
Może na zasadzie "dać wędkę nie rybę", będziemy ci w stanie dostarczyć instrukcję jak używać debuggera?

0

Starałam się napisać kod wedłlug tego, co jest na stronie tutaj, ale coś jest nie tak .... według mnie problem bedzie z tym main() i void(). Albo może jest to w źlej kolejności. Jestem zupełnie początkująca, więc nie wiem, co źle napisałam ....


#include <iostream>
#include <cstdlib> 
#include <ctime>									// define "time" in 32th row
#include <cassert>
#include <algorithm>

using namespace std;

int main() {

	//   CREATE AN ARRAY OF LENGTH "n" AND FILL IT WITH RANDOM NUMBERS


	int n = 0;										// size of an array at the beginning
	int	i = 0;										// for cycle
	int	j = 0;
	int k = 0;
	int	random_number = 0;							// random number at the beginning
	int* array = nullptr;

	cout << "Please enter a number: ";				// write the message for an user
	cin >> n;										// this allow to change n (in command line)
	array = new int[n];								// change n and save it to "array"

	srand((unsigned)time(0));

	for (i = 0; i < n; i++) {						// for cycle from i=0 to i=(n-1)
		random_number = (rand() % n);				/* generate a random number from 0 to n (I want to avoid the situation,
													   when somebody write n too hight and I set up too small random n (there would be a lot of the same numbers)*/
		array[i] = random_number;					// it fills array with random numbers
		cout << array[i] << ' ';
		if ((i + 1) % 10 == 0) {					// write 10 numbers in one row (only for clarity)
			cout << '\n';
		}
	}



	// SORTING
	cout << "Sorted array is:" << endl;


	void insertionSort(int array, int n)
	{
		int j;
		int k;
		int moving;
		for (j = 1; j < n; j++) {

			moving = array[j];
			k = j - 1;							// moving elements of array[0 ... j-1]

			while (k >= 0 && array[k] > moving)
			{
				array[k + 1] = array[k];
					k = k - 1;
			}
			array[k + 1] = moving;
		}
	}

	void printArray(int array[], int n)
	{
		int j;
		for (j=0;j<n; j++)
			cout << array[j] << " ";
		cout << endl;
	}

	insertionSort(array, n);
	printArray(array, n);

	return 0;
} 
0

Udało mi sie napisać kod według zadania. I działa to, tak jak powinno działać. :) Ale gdyby się znalazł ktoś baaardzo cierpliwy, który wytłumaczyłby mi, dlaczego ten kod wyżej nie działa, to byłabym baaardzo szczęśliwa. :) Może też o parę tygodni. Ważne jest to, bym zrozumiała, co robie źle
Tutaj jest mój kod, który działa:


#include <iostream>
#include <cstdlib> 
#include <ctime>									// define "time" in 32th row
#include <cassert>
#include <algorithm>

using namespace std;

int main() {

	//   CREATE AN ARRAY OF LENGTH "n" AND FILL IT WITH RANDOM NUMBERS


	int n = 0;										// size of an array at the beginning
	int	i = 0;										// for cycle
	int	j = 0;
	int k = 0;
	int	random_number = 0;							// random number at the beginning
	int* array = nullptr;

	cout << "Please enter a number: ";				// write the message for an user
	cin >> n;										// this allow to change n (in command line)
	array = new int[n];								// change n and save it to "array"

	srand((unsigned)time(0));

	for (i = 0; i < n; i++) {						// for cycle from i=0 to i=(n-1)
		random_number = (rand() % n);				/* generate a random number from 0 to n (I want to avoid the situation,
													   when somebody write n too hight and I set up too small random n (there would be a lot of the same numbers)*/
		array[i] = random_number;					// it fills array with random numbers
		cout << array[i] << ' ';
		if ((i + 1) % 10 == 0) {					// write 10 numbers in one row (only for clarity)
			cout << '\n';
		}
	}



	// SORTING
	cout << "Sorted array is:" << endl;
	int v = 0;

	for (i = 1; i < n; i++)
	{
		if (array[i] < array[i - 1])
		{
			v = array[i];
			j = i;
			do
			{
				array[j] = array[j - 1];
				array[j - 1] = v;
				j--;
			} while (j > 0 && v < array[j - 1]);
		}
	}

		for (i = 0; i < n; i++)
		{
			cout << array[i] << ' ';
			if ((i + 1) % 10 == 0) cout << '\n';
		}
	

	return 0;
} 
0

Kod z poprzedniego postu powinien wygladac tak:


#include <iostream>
#include <cstdlib> 
#include <ctime>                                    // define "time" in 32th row
#include <cassert>
#include <algorithm>

using namespace std;

    void insertionSort(int array[], int n)
    {
        int j;
        int k;
        int moving;
        for (j = 1; j < n; j++) {

            moving = array[j];
            k = j - 1;                          // moving elements of array[0 ... j-1]

            while (k >= 0 && array[k] > moving)
            {
                array[k + 1] = array[k];
                    k = k - 1;
            }
            array[k + 1] = moving;
        }
    }

    void printArray(int array[], int n)
    {
        int j;
        for (j=0;j<n; j++)
            cout << array[j] << " ";
        cout << endl;
    }

int main() {

    //   CREATE AN ARRAY OF LENGTH "n" AND FILL IT WITH RANDOM NUMBERS

    int n = 0;                                      // size of an array at the beginning
    int i = 0;                                      // for cycle
    int j = 0;
    int k = 0;
    int random_number = 0;                          // random number at the beginning
    int* array = nullptr;

    cout << "Please enter a number: ";              // write the message for an user
    cin >> n;                                       // this allow to change n (in command line)
    array = new int[n];                             // change n and save it to "array"

    srand((unsigned)time(0));

    for (i = 0; i < n; i++) {                       // for cycle from i=0 to i=(n-1)
        random_number = (rand() % n);               /* generate a random number from 0 to n (I want to avoid the situation,
                                                       when somebody write n too hight and I set up too small random n (there would be a lot of the same numbers)*/
        array[i] = random_number;                   // it fills array with random numbers
        cout << array[i] << ' ';
        if ((i + 1) % 10 == 0) {                    // write 10 numbers in one row (only for clarity)
            cout << '\n';
        }
    }

    // SORTING
    cout << "Sorted array is:" << endl;

    insertionSort(array, n);
    printArray(array, n);

    return 0;
} 

Zwroc uwage, ze funkcje z sortowaniem (i wyswietleniem) powinny znajdowac sie poza funkcja main. W sortowaniu przez wstawianie powinnas miec jako pierwszy argument tablice intow, a nie int array, czyli int array[]. I tyle, to wszystkie poprawki. Poczytaj o funkcjach, np. TUTAJ, zeby ich poprawnie uzywac

0

@Zrorpaczona

  1. Nie możesz definiować funkcji w ciele innej funkcji, w Twoim przypadku definicja insertionArray oraz printArray wewnątrz funkcji main jest niedozwolona.
  2. Niewłaściwie przekazałaś tablicę do insertionArray

Twój kod powinien wyglądać tak:

#include <iostream>
#include <cstdlib> 
#include <ctime>                                    // define "time" in 32th row
#include <cassert>
#include <algorithm>

using namespace std;

void insertionSort(int array[], int n)
    {
        int j;
        int k;
        int moving;
        for (j = 1; j < n; j++) {

            moving = array[j];
            k = j - 1;                          // moving elements of array[0 ... j-1]

            while (k >= 0 && array[k] > moving)
            {
                array[k + 1] = array[k];
                    k = k - 1;
            }
            array[k + 1] = moving;
        }
    }

    void printArray(int array[], int n)
    {
        int j;
        for (j=0;j<n; j++)
            cout << array[j] << " ";
        cout << endl;
    }

int main() {

    //   CREATE AN ARRAY OF LENGTH "n" AND FILL IT WITH RANDOM NUMBERS

    int n = 0;                                      // size of an array at the beginning
    int i = 0;                                      // for cycle
    int random_number = 0;                          // random number at the beginning
    int* array = nullptr;

    cout << "Please enter a number: ";              // write the message for an user
    cin >> n;                                       // this allow to change n (in command line)
    array = new int[n];                             // change n and save it to "array"

    srand((unsigned)time(0));

    for (i = 0; i < n; i++) {                       // for cycle from i=0 to i=(n-1)
        random_number = (rand() % n);               /* generate a random number from 0 to n (I want to avoid the situation,
                                                       when somebody write n too hight and I set up too small random n (there would be a lot of the same numbers)*/
        array[i] = random_number;                   // it fills array with random numbers
        cout << array[i] << ' ';
        if ((i + 1) % 10 == 0) {                    // write 10 numbers in one row (only for clarity)
            cout << '\n';
        }
    }

    // SORTING
    cout << "Sorted array is:" << endl;

  
    insertionSort(array, n);
    printArray(array, n);

    return 0;
} 

1 użytkowników online, w tym zalogowanych: 0, gości: 1