lundi 17 octobre 2022

Member variable are assigned seemingly randomly [duplicate]

In the SortingAlgorythems class i have two variables called xWin and yWin to save the Windows size of the renderer. The Constructur is supposed to set those values. In the visualize function these are used to draw a rectangle. (It looks a bit yanky cause im using it to check said values) What i expect is that a rectangle is drawn with size 1000x1000. What happens is that each time you run the code, the rectangle has the correct height but a different width each time. So something weird is happening with the xWin and yWin variables.

#include <ctime>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <vector>

#define WINDOW_WIDTH 1000
#define WINDOW_HEIGHT 1000

class SortingAlgorythems {
    public:
    int x;
    int array[50];
    
    int xWin;
    int yWin;
    
    SortingAlgorythems(int yV, int xV) {
        xWin = xV;
        yWin = yV;
    }
    
    void randomize (int numbers[]){
        int randomIndex;
        int temp;
            for(int i =0;  i < 51; i++){
            randomIndex = rand() % 50;
                temp = numbers[i];
                numbers[i] = numbers[randomIndex];
                numbers[randomIndex] = temp;
        }
    }

    void visualize(int visualizedArray[50], SDL_Renderer* renderer) {
        int width = 10;
        int arrLen = 50;
    
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
    SDL_RenderClear(renderer);
    SDL_SetRenderDrawColor(renderer, 0, 255, 0, 0);

        for (int i = 0; i <= arrLen; i++){
            SDL_Rect rect;
            rect.x = 0;//2000 / arrLen * i;
            rect.y = 0;
            rect.w = xWin;//xWin / arrLen;
            rect.h = yWin;//arrLen * visualizedArray[i];
            SDL_RenderFillRect(renderer, &rect);
            SDL_RenderDrawRect(renderer, &rect);
        }
        SDL_RenderPresent(renderer);
    }
    
    void setArray (int arr[50]){
        for (int i = 0; i < 51; i++){
        array[i] = arr[i];
        }
    }
    
    void bubbleSort (int l, int virtualArray[l],SDL_Renderer *renderer){
            for (int i = 0; i < 50; i++){
                for (int j = 0; j < 50 - i; j++){
                    if (virtualArray[j] > virtualArray[j+1]){
                        int temp = virtualArray[j];
                virtualArray[j] = virtualArray[j+1];
                visualize (virtualArray, renderer);
                }
            }
        }
    }
    
    void quickSort(int *array, int low, int high, SDL_Renderer *renderer)
{
    int i = low;
    int j = high;
    int pivot = array[(i + j) / 2];
    int temp;

    while (i <= j)
    {
        while (array[i] < pivot){
            i++;}
        while (array[j] > pivot){
            j--;}
        if (i <= j)
        {
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
            i++;
            j--;
            visualize(array, renderer);
            SDL_Delay(60);
        }
    }
    
    if (j > low){
        quickSort(array, low, j, renderer);}
    if (i < high){
        quickSort(array, i, high, renderer);}
    }
};


int main() {
    SortingAlgorythems sort(WINDOW_WIDTH, WINDOW_HEIGHT);
    
    int array[50];
    for (int i = 0; i < 51; i++){
        array[i] = i;
    }
    
    SDL_Event event;
    SDL_Renderer *renderer;
    SDL_Window *window;
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer);    
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
    SDL_RenderClear(renderer);
    
    srand((int) time(0));
    sort.randomize (array);
    sort.setArray (array);
    sort.visualize(array, renderer);
    sort.quickSort(array,0,50,renderer);
    
    
    while (1) {
        if (SDL_PollEvent(&event) && event.type == SDL_QUIT) {
            break;
        }
    }
    
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return EXIT_SUCCESS;
} ```




Aucun commentaire:

Enregistrer un commentaire