#help

262 messages · Page 1 of 1 (latest)

fallen oar
#

i am writing a main program with these instructions and my main program does not work and i dont know why.

peak zodiacBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

fallen oar
#
#include "input_gen.c"
#include "sorting.c"

void printArray(unsigned int *arr, int size, int op)
{
    bubbleSort(arr, size, &op);
    for (int i = 0; i < size; i++)
    {

        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main()
{
    int sizes[] = {256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
    int num_runs = 30;
    int op = 0;
    for (int size_idx = 0; size_idx < sizeof(sizes) / sizeof(sizes[0]); size_idx++)
    {
        int size = sizes[size_idx];
        printf("Size: %d\n", size);

        unsigned int ordered_input[size];
        generate_ordered_input(ordered_input, size);
        printf("Ordered input: ");
        printArray(ordered_input, size, op);

        // Reverse ordered input
        unsigned int reverse_ordered_input[size];
        generate_reverse_ordered_input(reverse_ordered_input, size);
        printf("Reverse ordered input: ");
        printArray(reverse_ordered_input, size, op);

        for (int run = 0; run < num_runs; run++)
        {
            unsigned int random_input[size];
            generate_randomized_input(random_input, size);
            printf("Random input (Run %d): ", run + 1);
            printArray(random_input, size, op);

            unsigned int almost_ordered_input[size];
            generate_almost_ordered_input(almost_ordered_input, size);
            printf("Almost ordered input (Run %d): ", run + 1);
            printArray(almost_ordered_input, size, op);
        }

        printf("\n");
    }

    return 0;
}```
#

this is my main program

#

it works to compile normaly but not with flags like wall or pedantic

fallen oar
#

?

lean basalt
#

the instructions are hard to read.
regardless having a 32768 * unsigned array on the stack isn't smart

fallen oar
#

witch part are you talking about?

lean basalt
#

unsigned int ordered_input[size];

fallen oar
#

okay should i just remove unsigned?

lean basalt
#

no. you should utilize the heap instead

fallen oar
#

how?

lean basalt
#

using an allocator of a sort. libc has one (in form of *alloc family of functions), there are more out there

fallen oar
#
#include <stdlib.h>
#include <time.h>

void generate_ordered_input(int arr[], int n)
{
    for (int i = 0; i < n; i++)
    {
        arr[i] = i + 1;
    }
}

void generate_reverse_ordered_input(int arr[], int n)
{
    for (int i = 0; i < n; i++)
    {
        arr[i] = n - i;
    }
}

void generate_randomized_input(int arr[], int n)
{
    generate_ordered_input(arr, n);
    srand(time(NULL));
    for (int i = 0; i < n; i++)
    {
        int j = rand() % n;
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

void generate_almost_ordered_input(int arr[], int n)
{
    generate_ordered_input(arr, n);
    srand(time(NULL));
    int num_permutations = n * 0.04;
    for (int i = 0; i < num_permutations; i++)
    {
        int index1 = rand() % n;
        int index2 = rand() % n;
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }
}``` okay this is my input_gen.c where i used similar functions
lean basalt
#

i don't see how those are similar sorry.
as a side note you shouldn't srand more than once

fallen oar
#

@ruby lintel are u here?

fallen oar
lean basalt
#

there's always a first time to anything 🙂

fallen oar
#

yes but if they have not taught it to me in class i would feel more comfortable using somting else and they would prob understand that i did not write it then

#
#include "input_gen.c"
#include "sorting.c"```should i even include the c files?
#

i have incuded h files befor i think

lean basalt
#

🤷‍♂️
if they didn't teach you heap allocations in the past idk how many months you're here - they are doing a bad job.

the having 32768 * sizeof(unsigned) (which is about 3M bytes on x86-64 or around 3MB) (times 2. you have 2 of those arrays) on the stack isn't a smart thing is because the stack is small by comparison, and C has no mechanism to warn you about it

fallen oar
#

oh okay

#
#include <time.h>





unsigned int* Ordered(int n)
{
    unsigned int* array = malloc(sizeof(unsigned int) * n); //allocates memory for the array
    
    for (unsigned int i = 0; i < n; i++) //loops through the array n times and adds every integer from 1-n to the array
    {
        array[i] = i+1;
    }
    
    return array;
}

unsigned int* ReverseOrdered(int n)
{
    unsigned int* array = malloc(sizeof(unsigned int) * n); //allocates memory for the array

    for (unsigned int i = n; i > 0; i--) //fills the array in a descending order
    {
        array[n-i] = i;
    }
    
    return array;
}

// DO NOT FORGET to set a random seed in main
unsigned int* Randomized(int n)
{
    unsigned int* array = Ordered(n); // first takes an ordered array

    for (unsigned int i = n-1; i > 0; i--)
    {
        unsigned int j = rand() % (i + 1); //generates a random number between 0 and i (including i)

        //swaps places of index i and index j
        unsigned int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    return array;
}


// DO NOT FORGET to set a random seed in main
// AlmostOrdered is supposed to have about 4% of elements in the wrong place but it seems that this version gives 3,9% on average but that felt close enough
unsigned int* AlmostOrdered(int n)
{
    unsigned int* array = Ordered(n); // first takes an ordered array

    for (unsigned int i = 0; i < (n / 50); i++) 
    {
        // n / 50 represents the 4% estimate of elements being in the wrong place
        // n / 50 is 2% but as it swaps two elements the actual amount of elements in the wrong order is dubbled (not always as it is randomized)

        unsigned int a = rand() % n;
        unsigned int b = rand() % n;

        //swaps places of index a and b
        unsigned int temp = array[a];
        array[a] = array[b];
        array[b] = temp;
    }

    return array;
}```
#

this is my teammates input_gen.c do you think i should write my unctions sililar to his in my input_gen.c?

lean basalt
#

since i couldn't read the instructions - i have no idea.
regardless they:

  1. don't call srand more than once
  2. use proper heap allocation
fallen oar
lean basalt
ruby lintel
ruby lintel
#

mingw on vscode

fallen oar
#
        unsigned int *reverse_ordered_input = (unsigned int *)malloc(size * sizeof(unsigned int));
        unsigned int *random_input = (unsigned int *)malloc(size * sizeof(unsigned int));
        unsigned int *almost_ordered_input = (unsigned int *)malloc(size * sizeof(unsigned int));``` like this?
lean basalt
#

fair. you allocate things by calling malloc / calloc / realloc e.g.:

T *some_block = malloc(some_size * sizeof *some_block);```
you check the allocation succeeded:
```c
if (some_block == NULL) { 
  // handle the error
}```
you treat `some_block` as an array, and when you're done you `free` it. e.g.
`free(some_block);`

<https://en.cppreference.com/w/c/memory>
lean basalt
fallen oar
#
            unsigned int *almost_ordered_input = malloc(size * sizeof(unsigned int));```
lean basalt
#

thats better

fallen oar
#
            {
                printf("Memory allocation failed. Exiting...\n");
                return 1;
            }```
ruby lintel
#

from what ouput i'm getting the sorting algorithm is working very well

#

is this the ouput you want?

fallen oar
#
main.c:33:13: error: conflicting types for 'ordered_input'
         int ordered_input[size];
             ^~~~~~~~~~~~~
main.c:25:23: note: previous definition of 'ordered_input' was here
         unsigned int *ordered_input = malloc(size * sizeof(unsigned int));
                       ^~~~~~~~~~~~~
main.c:36:20: warning: pointer targets in passing argument 1 of 'printArray' differ in signedness [-Wpointer-sign]
         printArray(ordered_input, size, op);
                    ^~~~~~~~~~~~~
main.c:5:6: note: expected 'unsigned int *' but argument is of type 'int *'
 void printArray(unsigned int *arr, int size, int op)
      ^~~~~~~~~~
main.c:38:13: error: conflicting types for 'reverse_ordered_input'
         int reverse_ordered_input[size];
             ^~~~~~~~~~~~~~~~~~~~~
main.c:26:23: note: previous definition of 'reverse_ordered_input' was here
         unsigned int *reverse_ordered_input = malloc(size * sizeof(unsigned int));
                       ^~~~~~~~~~~~~~~~~~~~~
main.c:41:20: warning: pointer targets in passing argument 1 of 'printArray' differ in signedness [-Wpointer-sign]
         printArray(reverse_ordered_input, size, op);
                    ^~~~~~~~~~~~~~~~~~~~~
main.c:5:6: note: expected 'unsigned int *' but argument is of type 'int *'
 void printArray(unsigned int *arr, int size, int op)
      ^~~~~~~~~~
main.c:53:17: error: conflicting types for 'random_input'
             int random_input[size];
                 ^~~~~~~~~~~~
main.c:45:27: note: previous definition of 'random_input' was here
             unsigned int *random_input = malloc(size * sizeof(unsigned int));
                           ^~~~~~~~~~~~
main.c:56:24: warning: pointer targets in passing argument 1 of 'printArray' differ in signedness [-Wpointer-sign]
             printArray(random_input, size, op);
                        ^~~~~~~~~~~```
#

these are the faults i get, i dont know i cant open the link

ruby lintel
#

Ordered input: 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

fallen oar
#

no sorry my computer blocks links that are "unsafe"

#

oh okay

ruby lintel
#

Reverse ordered input: 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

#

Random input (Run 1): 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

#

Almost ordered input (Run 1): 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

lean basalt
fallen oar
#

it might work but my code still need to pass with the flags -wall and -pedantic witch it does not

#
         unsigned int *ordered_input = malloc(size * sizeof(unsigned int));``` this came after i added what you told me to
ruby lintel
#

@fallen oar is the output the one you want?

lean basalt
#

well yes. you can't have different identifiers with the same name.
i.e. if you decided to change ordered_input from an array to a heap allocated block - you need to delete the previous declaration

fallen oar
#

i dont know this is what the example output is

ruby lintel
#

but do we sort the input?

#

i don't understand

fallen oar
#

yes i think so

#
#include "input_gen.c"
#include "sorting.c"

void printArray(unsigned int *arr, int size, int op)
{
    bubbleSort(arr, size, &op);
    for (int i = 0; i < size; i++)
    {

        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main()
{
    int sizes[] = {256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
    int num_runs = 30;
    int op = 0;
    for (int size_idx = 0; size_idx < sizeof(sizes) / sizeof(sizes[0]); size_idx++)
    {
        int size = sizes[size_idx];
        printf("Size: %d\n", size);
        unsigned int *ordered_input = malloc(size * sizeof(unsigned int));
        unsigned int *reverse_ordered_input = malloc(size * sizeof(unsigned int));
        if (ordered_input == NULL || reverse_ordered_input == NULL)
        {
            printf("Memory allocation failed. Exiting...\n");
            return 1;
        }

        int ordered_input[size];
        generate_ordered_input(ordered_input, size);
        printf("Ordered input: ");
        printArray(ordered_input, size, op);

        int reverse_ordered_input[size];
        generate_reverse_ordered_input(reverse_ordered_input, size);
        printf("Reverse ordered input: ");
        printArray(reverse_ordered_input, size, op);

        for (int run = 0; run < num_runs; run++)
        {
            unsigned int *random_input = malloc(size * sizeof(unsigned int));
            unsigned int *almost_ordered_input = malloc(size * sizeof(unsigned int));

            if (random_input == NULL || almost_ordered_input == NULL)
            {
                printf("Memory allocation failed. Exiting...\n");
                return 1;
            }
            int random_input[size];
            generate_randomized_input(random_input, size);
            printf("Random input (Run %d): ", run + 1);
            printArray(random_input, size, op);```
#
            generate_almost_ordered_input(almost_ordered_input, size);
            printf("Almost ordered input (Run %d): ", run + 1);
            printArray(almost_ordered_input, size, op);
        }

        printf("\n");
    }

    return 0;
}```
#

this is what is have now

lean basalt
#

and what do you suppose this is doing:

        unsigned int *ordered_input = malloc(size * sizeof(unsigned int));
        ...

        int ordered_input[size];
```?
fallen oar
#
#include "input_gen.c"
#include "sorting.c"

void printArray(unsigned int *arr, int size, int op)
{
    bubbleSort(arr, size, &op);
    for (int i = 0; i < size; i++)
    {

        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main()
{
    int sizes[] = {256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
    int num_runs = 30;
    int op = 0;
    for (int size_idx = 0; size_idx < sizeof(sizes) / sizeof(sizes[0]); size_idx++)
    {
        int size = sizes[size_idx];
        printf("Size: %d\n", size);
        unsigned int *ordered_input = malloc(size * sizeof(unsigned int));
        unsigned int *reverse_ordered_input = malloc(size * sizeof(unsigned int));
        if (ordered_input == NULL || reverse_ordered_input == NULL)
        {
            printf("Memory allocation failed. Exiting...\n");
            return 1;
        }

        generate_ordered_input(ordered_input, size);
        printf("Ordered input: ");
        printArray(ordered_input, size, op);

        generate_reverse_ordered_input(reverse_ordered_input, size);
        printf("Reverse ordered input: ");
        printArray(reverse_ordered_input, size, op);

        for (int run = 0; run < num_runs; run++)
        {
            unsigned int *random_input = malloc(size * sizeof(unsigned int));
            unsigned int *almost_ordered_input = malloc(size * sizeof(unsigned int));

            if (random_input == NULL || almost_ordered_input == NULL)
            {
                printf("Memory allocation failed. Exiting...\n");
                return 1;
            }
            generate_randomized_input(random_input, size);
            printf("Random input (Run %d): ", run + 1);
            printArray(random_input, size, op);

            generate_almost_ordered_input(almost_ordered_input, size);
            printf("Almost ordered input (Run %d): ", run + 1);
            printArray(almost_ordered_input, size, op);
        }

        printf("\n");
    }

    return 0;
}```
lean basalt
#

and to include stdlib ofc

fallen oar
#

oh right

#
main.c:34:32: warning: pointer targets in passing argument 1 of 'generate_ordered_input' differ in signedness [-Wpointer-sign]
         generate_ordered_input(ordered_input, size);
                                ^~~~~~~~~~~~~
In file included from main.c:3:0:
input_gen.c:5:6: note: expected 'int *' but argument is of type 'unsigned int *'
 void generate_ordered_input(int arr[], int n)
      ^~~~~~~~~~~~~~~~~~~~~~
main.c:38:40: warning: pointer targets in passing argument 1 of 'generate_reverse_ordered_input' differ in signedness [-Wpointer-sign]      
         generate_reverse_ordered_input(reverse_ordered_input, size);
                                        ^~~~~~~~~~~~~~~~~~~~~
In file included from main.c:3:0:
input_gen.c:13:6: note: expected 'int *' but argument is of type 'unsigned int *'
 void generate_reverse_ordered_input(int arr[], int n)
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:52:39: warning: pointer targets in passing argument 1 of 'generate_randomized_input' differ in signedness [-Wpointer-sign]
             generate_randomized_input(random_input, size);
                                       ^~~~~~~~~~~~
In file included from main.c:3:0:
input_gen.c:21:6: note: expected 'int *' but argument is of type 'unsigned int *'
 void generate_randomized_input(int arr[], int n)
      ^~~~~~~~~~~~~~~~~~~~~~~~~
main.c:56:43: warning: pointer targets in passing argument 1 of 'generate_almost_ordered_input' differ in signedness [-Wpointer-sign]       
             generate_almost_ordered_input(almost_ordered_input, size);
                                           ^~~~~~~~~~~~~~~~~~~~
In file included from main.c:3:0:
input_gen.c:34:6: note: expected 'int *' but argument is of type 'unsigned int *'
 void generate_almost_ordered_input(int arr[], int n)
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~```
#

now i get these errors stil

lean basalt
#

and? did you read any of them at all?

fallen oar
#

yes i did

lean basalt
#

ok, and what didn't you understand?

fallen oar
#

any if it void generate_ordered_input(int arr[], int n) { for (int i = 0; i < n; i++) { arr[i] = i + 1; } }

#
input_gen.c:5:6: note: expected 'int *' but argument is of type 'unsigned int *'
 void generate_ordered_input(int arr[], int n)``` this error says there is somthing wrong with this function
#

it says its of tyo unsigned int but it is not?

lean basalt
#

this warning (and the one precedes it) tells you that you aren't suppling the correct type to the function. the function expects a signed type (int *) while you supply it with a different type unsigned int * (or unsgined * if you will. both are equivalent)

fallen oar
lean basalt
#

again - you function expects a signed type, you supplied it an unsigned type - hence the warning

lean basalt
fallen oar
#

again- i dont understand what that means

#
        int *reverse_ordered_input = malloc(size * sizeof(unsigned int));```
lean basalt
#

int is a signed type. it represent both negative and positive values. unsigned int on the other hand represent only positives (kinda sorta. lets treat it that way).

your function expects a pointer to a signed int.
void generate_ordered_input(int arr[], int n) is actually void generate_ordered_input(int *arr /*a pointer to a signed int*/, int n)

yet you pass it unsigned int *ordered_input = malloc(size * sizeof(unsigned int)); which is unsigned hence the warning

ruby lintel
#

in the input_gen.c file, change the int arr[] to unsigned int arr[]

#

in the different input_gen functions

fallen oar
#

void generate_ordered_input(unsigned int arr[], int n)like this?

lean basalt
fallen oar
lean basalt
#

up to you and/or your instructions

fallen oar
#

C:\Users\ALMAWA~1\AppData\Local\Temp\cctmD3iD.o:sorting.c:(.text+0x0): multiple definition of `bubbleSort' C:\Users\ALMAWA~1\AppData\Local\Temp\ccc80sRo.o:main.c:(.text+0x222): first defined here C:\Users\ALMAWA~1\AppData\Local\Temp\ccSCaExW.o:input_gen.c:(.text+0x0): multiple definition of `generate_ordered_input' C:\Users\ALMAWA~1\AppData\Local\Temp\ccc80sRo.o:main.c:(.text+0x0): first defined here C:\Users\ALMAWA~1\AppData\Local\Temp\ccSCaExW.o:input_gen.c:(.text+0x47): multiple definition of `generate_reverse_ordered_input' C:\Users\ALMAWA~1\AppData\Local\Temp\ccc80sRo.o:main.c:(.text+0x47): first defined here C:\Users\ALMAWA~1\AppData\Local\Temp\ccSCaExW.o:input_gen.c:(.text+0x90): multiple definition of `generate_randomized_input' C:\Users\ALMAWA~1\AppData\Local\Temp\ccc80sRo.o:main.c:(.text+0x90): first defined here C:\Users\ALMAWA~1\AppData\Local\Temp\ccSCaExW.o:input_gen.c:(.text+0x145): multiple definition of `generate_almost_ordered_input' C:\Users\ALMAWA~1\AppData\Local\Temp\ccc80sRo.o:main.c:(.text+0x145): first defined here collect2.exe: error: ld returned 1 exit status now these is fewer errors

lean basalt
fallen oar
#

hmm it says muliple definitions of bubblesort in sorting.c

#

oh okay

#

okay it runs now but it keep going forever

#
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242```
ruby lintel
#

that's good

fallen oar
#

i dont really understand what they want the output to be?

ruby lintel
#

me neither

#

do they want us to sort the input or not?

fallen oar
#

i am pretty sure they do i remember them mentioning that it is easier that way

#

in your main program, results must be written to a text file, with the following format
for each run. like wht does this mean?

ruby lintel
#

i think i know

#

they don't want the sorted array, just the number of operations

fallen oar
#

Ohh is it like fopen and stuff?

ruby lintel
#

can you do that?

fallen oar
#

Hmm maybe

ruby lintel
#

ok try

fallen oar
#
#include <stdlib.h> 
#include "input_gen.h"
#include "sorting.h"

void printArray(unsigned int *arr, int size, int op)
{
    bubbleSort(arr, size, &op);
    for (int i = 0; i < size; i++)
    {

        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main()
{
    FILE *output_file = fopen("output.txt", "w"); // Open file for writing

    if (output_file == NULL)
    {
        printf("Error opening file for writing.\n");
        return 1;
    }

    int sizes[] = {256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
    int num_runs = 30;
    int op = 0;
    for (int size_idx = 0; size_idx < sizeof(sizes) / sizeof(sizes[0]); size_idx++)
    {
        int size = sizes[size_idx];
        printf("Size: %d\n", size);
        unsigned int *ordered_input = malloc(size * sizeof(unsigned int));
        unsigned int *reverse_ordered_input = malloc(size * sizeof(unsigned int));
        if (ordered_input == NULL || reverse_ordered_input == NULL)
        {
            printf("Memory allocation failed. Exiting...\n");
            fclose(output_file);
            return 1;
        }

        generate_ordered_input(ordered_input, size);
       fprintf(output_file, "Ordered input: \n");
        printArray(ordered_input, size, op);

        generate_reverse_ordered_input(reverse_ordered_input, size);
        fprintf(output_file, "Reverse ordered input: \n");
        printArray(reverse_ordered_input, size, op);

        for (int run = 0; run < num_runs; run++)
        {
            unsigned int *random_input = malloc(size * sizeof(unsigned int));
            unsigned int *almost_ordered_input = malloc(size * sizeof(unsigned int));

            if (random_input == NULL || almost_ordered_input == NULL)
            {
                printf("Memory allocation failed. Exiting...\n");
                fclose(output_file);
                return 1;
            }```
#
            fprintf(output_file, "Random input (Run %d): \n", run + 1);
            printArray(random_input, size, op);

            generate_almost_ordered_input(almost_ordered_input, size);
            fprintf(output_file, "Almost ordered input (Run %d): \n", run + 1);
            printArray(almost_ordered_input, size, op);

            free(random_input); 
            free(almost_ordered_input);
        }
        free(ordered_input); 
        free(reverse_ordered_input);

        fprintf(output_file, "\n");
    }
    fclose(output_file);
    return 0;
}```
#

is it not like this kind of?

ruby lintel
#

first , you don't need the printArray function any more

fallen oar
#

Oh okay

#

But then I get a bunch of errors again

ruby lintel
#

read them and fix them

fallen oar
#
main.c:36:9: warning: implicit declaration of function 'printArray'; did you mean 'printf_s'? [-Wimplicit-function-declaration]
         printArray(ordered_input, size, op);
         ^~~~~~~~~~
         printf_s
C:\Users\ALMAWA~1\AppData\Local\Temp\ccKISr4j.o:main.c:(.text+0x150): undefined reference to `printArray'
C:\Users\ALMAWA~1\AppData\Local\Temp\ccKISr4j.o:main.c:(.text+0x192): undefined reference to `printArray'
C:\Users\ALMAWA~1\AppData\Local\Temp\ccKISr4j.o:main.c:(.text+0x238): undefined reference to `printArray'
C:\Users\ALMAWA~1\AppData\Local\Temp\ccKISr4j.o:main.c:(.text+0x278): undefined reference to `printArray'
collect2.exe: error: ld returned 1 exit status```
ruby lintel
#

you do not need the print array function anymore

fallen oar
#

why cant we just keep the print array function?

ruby lintel
#

so don't use

#

it

fallen oar
#

i have removed it

ruby lintel
#

yes but you're stil calling it

#

look at your code

fallen oar
#
       fprintf(output_file, "Ordered input: \n");
        printArray(ordered_input, size, op);```
#

should i just remove the whole printarray(ordered... thing?

#

why do we need to remove the printarray function?

ruby lintel
#

yes

#

replace it with bubbleSort function

fallen oar
#

from sorting.c?

ruby lintel
#

yes

fallen oar
#
main.c:36:9: warning: unused variable 'op' [-Wunused-variable]
     int op = 0;
         ^~
C:\Users\ALMAWA~1\AppData\Local\Temp\cc6Z2Spk.o:sorting.c:(.text+0x0): multiple definition of `bubbleSort'
C:\Users\ALMAWA~1\AppData\Local\Temp\ccChDS5j.o:main.c:(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status```
#

why cant we keep the printarray function?

ruby lintel
#

because we don't need it

fallen oar
#

okay but now bubblesort is defined muliple times

#
#include <stdlib.h> 
#include "input_gen.h"
#include "sorting.h"

void bubbleSort(unsigned int *arr, int n, int *op)
{
    *op = 0;
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < n - 1 - i; j++)
        {
            (*op)++;
            if (arr[j + 1] < arr[j])
            {
                int swap = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = swap;
            }
        }
    }
}

int main()
{
    FILE *output_file = fopen("output.txt", "w"); // Open file for writing

    if (output_file == NULL)
    {
        printf("Error opening file for writing.\n");
        return 1;
    }

    int sizes[] = {256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
    int num_runs = 30;
    int op = 0;
    for (int size_idx = 0; size_idx < sizeof(sizes) / sizeof(sizes[0]); size_idx++)
    {
        int size = sizes[size_idx];
        fprintf(output_file, "Size: %d\n", size);
        unsigned int *ordered_input = malloc(size * sizeof(unsigned int));
        unsigned int *reverse_ordered_input = malloc(size * sizeof(unsigned int));
        if (ordered_input == NULL || reverse_ordered_input == NULL)
        {
            printf("Memory allocation failed. Exiting...\n");
            fclose(output_file);
            return 1;
        }

        generate_ordered_input(ordered_input, size);
       fprintf(output_file, "Ordered input: \n");
        

        generate_reverse_ordered_input(reverse_ordered_input, size);
        fprintf(output_file, "Reverse ordered input: \n");
        

        for (int run = 0; run < num_runs; run++)
        {
           unsigned int *random_input = malloc(size * sizeof(unsigned int));
          unsigned int *almost_ordered_input = malloc(size * sizeof(unsigned int));

            if (random_input == NULL || almost_ordered_input == NULL)
            {
                printf("Memory allocation failed. Exiting...\n");
                fclose(output_file);
                return 1;```
#
            fprintf(output_file, "Random input (Run %d): \n", run + 1);
            

            generate_almost_ordered_input(almost_ordered_input, size);
            fprintf(output_file, "Almost ordered input (Run %d): \n", run + 1);
            

            free(random_input); 
            free(almost_ordered_input);
        }
        free(ordered_input); 
        free(reverse_ordered_input);

        fprintf(output_file, "\n");
    }
    fclose(output_file);
    return 0;
}```
ruby lintel
#

did you try it to see if it works?

fallen oar
#

yes it does not this comes up main.c: In function 'main': main.c:36:9: warning: unused variable 'op' [-Wunused-variable] int op = 0; ^~ C:\Users\ALMAWA~1\AppData\Local\Temp\cczfFvuE.o:sorting.c:(.text+0x0): multiple definition of `bubbleSort' C:\Users\ALMAWA~1\AppData\Local\Temp\ccsOqaMF.o:main.c:(.text+0x0): first defined here collect2.exe: error: ld returned 1 exit status

ruby lintel
#

you're not using the op variable

fallen oar
#

okay should i remove it?

#

C:\Users\ALMAWA~1\AppData\Local\Temp\ccEFoRX0.o:sorting.c:(.text+0x0): multiple definition of `bubbleSort' C:\Users\ALMAWA~1\AppData\Local\Temp\ccKkO0Hi.o:main.c:(.text+0x0): first defined here collect2.exe: error: ld returned 1 exit statusthen there is still multiple definitions of bubble sort

ruby lintel
#

why do you have bubbleSort function in you main file

fallen oar
#

becuse you told me to add it instead of the printarray function?

fallen oar
#

should i remove it?

#

if i remove it runs but nothing happens

ruby lintel
fallen oar
#

okay but do i not do that here #include "sorting.h"

ruby lintel
#

call it means c bubbleSort(reverse_ordered_input, size, op);

#

something like this

#

Size: 256
Ordered input: 32640 operations

Reverse ordered input: 32640 operations

Random input (Run 1): 32640 operations

Almost ordered input (Run 1): 32640 operations

Random input (Run 2): 32640 operations

Almost ordered input (Run 2): 32640 operations

Random input (Run 3): 32640 operations

i think this is the kind of output you want

fallen oar
#

okay but nothing happeds still

#

yes

ruby lintel
#

i got my code to do this

fallen oar
#

okay but does it open a file and putput that?

#

becuse nothing happens for me

#

OH WAIT

#

i found it

#

this is correct right?

#

no wait is does not say the numbers now

ruby lintel
fallen oar
#

i think it does but my output is not quite right

ruby lintel
#

you need the operation count, that's all

ruby lintel
#

but i don't think you need to

fallen oar
#

oh okay how do i do that?

ruby lintel
#

do you have an idea?

#

also sorry for not responding, i've been busy

#

@fallen oar

fallen oar
#

its okay! hmm i am not too sure

#

dont we already have an operation count in bubblesort from task 3,2?

ruby lintel
#

yes we do

#

so we need another one

#

in the main

fallen oar
#
            int op_count_almost_ordered = 0;```
#

something like this?

ruby lintel
#

you just need one counter

fallen oar
#

oh okay

ruby lintel
#

int op = 0;

fallen oar
#

ohhh

ruby lintel
#

and then you need a pointer

fallen oar
#

int op = 0;

ruby lintel
#

yes and then another variable

#

which will be a pointer to the op variable

#

show me

#

how you would do it

fallen oar
#
    int *op_p = &op;```like this?
#

how do we use this now?

ruby lintel
#

then you will do bubbleSort(ordered_input, size, op_p);

#

you will do the same for each input type

#

reverse, randomized, and almost ordered

fallen oar
#

do i put both int op the pointer and the bublesort under each of them?

#

or just the bubblesort function?

ruby lintel
#

wait your main function is weird

#

can you show it to me

fallen oar
#
{
    int op = 0;
    int *op_p = &op;
    FILE *output_file = fopen("output.txt", "w");

    if (output_file == NULL)
    {
        printf("Error opening file for writing.\n");
        return 1;
    }

    int sizes[] = {256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
    int num_runs = 30;

    for (int size_idx = 0; size_idx < sizeof(sizes) / sizeof(sizes[0]); size_idx++)
    {
        int size = sizes[size_idx];
        fprintf(output_file, "Size: %d\n", size);
        unsigned int *ordered_input = malloc(size * sizeof(unsigned int));
        unsigned int *reverse_ordered_input = malloc(size * sizeof(unsigned int));
        if (ordered_input == NULL || reverse_ordered_input == NULL)
        {
            printf("Memory allocation failed. Exiting...\n");
            fclose(output_file);
            return 1;
        }

        generate_ordered_input(ordered_input, size);
        fprintf(output_file, "Ordered input: \n");

        bubbleSort(ordered_input, size, op_p);

        generate_reverse_ordered_input(reverse_ordered_input, size);
        fprintf(output_file, "Reverse ordered input: \n");

        bubbleSort(reverse_ordered_input, size, op_p);

        for (int run = 0; run < num_runs; run++)
        {
            unsigned int *random_input = malloc(size * sizeof(unsigned int));
            unsigned int *almost_ordered_input = malloc(size * sizeof(unsigned int));

            if (random_input == NULL || almost_ordered_input == NULL)
            {
                printf("Memory allocation failed. Exiting...\n");
                fclose(output_file);
                return 1;
            }
            generate_randomized_input(random_input, size);
            fprintf(output_file, "Random input (Run %d): \n", run + 1);

            bubbleSort(randomized_input, size, op_p);

            generate_almost_ordered_input(almost_ordered_input, size);
            fprintf(output_file, "Almost ordered input (Run %d): \n", run + 1);```
#

            free(random_input);
            free(almost_ordered_input);
        }
        free(ordered_input);
        free(reverse_ordered_input);

        fprintf(output_file, "\n");
    }
    fclose(output_file);
    return 0;
}```
ruby lintel
#

wait a second

#

where does the generate_almost_ordered_input function come from

fallen oar
#

i am not sure haha

#

i just had it from the beginning i think

ruby lintel
#

hmm no

fallen oar
#

it is in the input_gen.h as well

ruby lintel
#

you don't need it, just sort the array and print the op_p variable in your output file

#

or wait

#

no i messed up

#

i'm sorry i got confused

fallen oar
#

its okay haha

ruby lintel
#

so now you just need to print the op_p variable, you're almost done

#

fprintf("%d operations \n ", *op_p);

like this

#

just below each bubleSort line

#

you put that line

fallen oar
#

oh okay

ruby lintel
#

something like this

    printf("Ordered input: ");
    bubbleSort(ordered_input, size, op_p);
    printf("%d operations \n ", *op_p);
    printf("\n");
#

don't forget to replace printf with fprintf

#

i wasn't printing the ouput to a file

fallen oar
#
{
    int op = 0;
    int *op_p = &op;
    FILE *output_file = fopen("output.txt", "w");

    if (output_file == NULL)
    {
        printf("Error opening file for writing.\n");
        return 1;
    }

    int sizes[] = {256, 512, 1024, 2048, 4096, 8192, 16384, 32768};
    int num_runs = 30;

    for (int size_idx = 0; size_idx < sizeof(sizes) / sizeof(sizes[0]); size_idx++)
    {
        int size = sizes[size_idx];
        fprintf(output_file, "Size: %d\n", size);
        unsigned int *ordered_input = malloc(size * sizeof(unsigned int));
        unsigned int *reverse_ordered_input = malloc(size * sizeof(unsigned int));
        if (ordered_input == NULL || reverse_ordered_input == NULL)
        {
            printf("Memory allocation failed. Exiting...\n");
            fclose(output_file);
            return 1;
        }

        generate_ordered_input(ordered_input, size);
        fprintf(output_file, "Ordered input: \n");

        bubbleSort(ordered_input, size, op_p);
        fprintf("%d operations \n ", *op_p);

        generate_reverse_ordered_input(reverse_ordered_input, size);
        fprintf(output_file, "Reverse ordered input: \n");

        bubbleSort(reverse_ordered_input, size, op_p);
        fprintf("%d operations \n ", *op_p);

        for (int run = 0; run < num_runs; run++)
        {
            unsigned int *random_input = malloc(size * sizeof(unsigned int));
            unsigned int *almost_ordered_input = malloc(size * sizeof(unsigned int));

            if (random_input == NULL || almost_ordered_input == NULL)
            {
                printf("Memory allocation failed. Exiting...\n");
                fclose(output_file);
                return 1;
            }
            generate_randomized_input(random_input, size);
            fprintf(output_file, "Random input (Run %d): \n", run + 1);

            bubbleSort(random_input, size, op_p);
            fprintf("%d operations \n ", *op_p);

         ```
#
            fprintf(output_file, "Almost ordered input (Run %d): \n", run + 1);

            bubbleSort(almost_ordered_input, size, op_p);
            fprintf("%d operations \n ", *op_p);

            free(random_input);
            free(almost_ordered_input);
        }
        free(ordered_input);
        free(reverse_ordered_input);

        fprintf(output_file, "\n");
    }
    fclose(output_file);
    return 0;
}```
ruby lintel
#

yes perfect

#

now run

#

see if it works

fallen oar
#

wait what

#

happend

#

hold on

#
             fprintf("%d operations \n ", *op_p);
                                          ^
In file included from main.c:1:0:
C:/Program Files/Haskell Platform/8.6.5/mingw/x86_64-w64-mingw32/include/stdio.h:387:15: note: expected 'const char * restrict' but argument is of type 'int'
   int __cdecl fprintf(FILE * __restrict__ _File,const char * __restrict__ _Format,...);```
ruby lintel
#

hmmm wait i forgot about this

#

lemme check somethink

#

you need to add output_file in that line

#

fprintf(output_file , "%d operations \n ", *op_p);

#

like this

fallen oar
#

okay it works now Size: 256 Ordered input: 32640 operations Reverse ordered input: 32640 operations Random input (Run 1): 32640 operations Almost ordered input (Run 1): 32640 operations Random input (Run 2): 32640 operations Almost ordered input (Run 2): 32640 operations Random input (Run 3): 32640 operations Almost ordered input (Run 3): 32640 operations Random input (Run 4): 32640 operations Almost ordered input (Run 4): 32640 operations Random input (Run 5): 32640 operations Almost ordered input (Run 5): 32640 operations Random input (Run 6): 32640 operations Almost ordered input (Run 6): 32640 operations Random input (Run 7): 32640 operations Almost ordered input (Run 7): 32640 operations Random input (Run 8):

#

but is there a way to make the number be beside the other text

ruby lintel
#

yes

fallen oar
#

Ordered input: 32640 operations like this?

#

is it becuse i have new line here? fprintf(output_file, "\n");

ruby lintel
#

this is making a new line

#

fprintf(output_file, "Almost ordered input (Run %d): ", run + 1);

#

that should do the trick

fallen oar
#
Ordered input: 
32640 operations Reverse ordered input: 
32640 operations Random input (Run 1): 
32640 operations Almost ordered input (Run 1): 
32640 operations Random input (Run 2): 
32640 operations Almost ordered input (Run 2): 
32640 operations Random input (Run 3): 
32640 operations Almost ordered input (Run 3): 
32640 operations Random input (Run 4): 
32640 operations Almost ordered input (Run 4): 
32640 operations Random input (Run 5): 
32640 operations Almost ordered input (Run 5): 
32640 operations Random input (Run 6): 
32640 operations Almost ordered input (Run 6): 
32640 operations Random input (Run 7): 
32640 operations Almost ordered input (Run 7):``` hmm maybe it was another new line
ruby lintel
#

you didn't take out the right new line

fallen oar
#

oh sorry wait

ruby lintel
#

take out the one after you print ordered input and such

fallen oar
#

yes i see

#
Ordered input: 32640 operations 
 Reverse ordered input: 32640 operations 
 Random input (Run 1): 32640 operations 
 Almost ordered input (Run 1): 32640 operations 
 Random input (Run 2): 32640 operations 
 Almost ordered input (Run 2): 32640 operations 
 Random input (Run 3): 32640 operations 
 Almost ordered input (Run 3): 32640 operations 
 Random input (Run 4): 32640 operations 
 Almost ordered input (Run 4): 32640 operations 
 Random input (Run 5): 32640 operations 
 Almost ordered input (Run 5): 32640 operations 
 Random input (Run 6): 32640 operations 
 Almost ordered input (Run 6): 32640 operations 
 Random input (Run 7): 32640 operations 
 Almost ordered input (Run 7): 32640 operations 
 Random input (Run 8): 32640 operations 
 Almost ordered input (Run 8): 32640 operations 
 Random input (Run 9): 32640 operations 
 Almost ordered input (Run 9): 32640 operations 
 Random input (Run 10): 32640 operations 
 Almost ordered input (Run 10): 32640 operations ```now
ruby lintel
#

maybe add another fptinf("\n") just after you print the number of operations for extra readability

fallen oar
#
        fprintf(output_file, "Reverse ordered input: ");``` after these?
#

it says there are too few arguments in function call

ruby lintel
#

add ouput_file

#

fprintf(output_file, "\n");

fallen oar
#
Ordered input: 32640 operations 
 
Reverse ordered input: 32640 operations 
 
Random input (Run 1): 32640 operations 
 
Almost ordered input (Run 1): 32640 operations 
 
Random input (Run 2): 32640 operations 
 
Almost ordered input (Run 2): 32640 operations 
 
Random input (Run 3): 32640 operations 
 
Almost ordered input (Run 3): 32640 operations 
 
Random input (Run 4): 32640 operations 
 
Almost ordered input (Run 4): 32640 operations 
 
Random input (Run 5): 32640 operations```
ruby lintel
#

this looks good

fallen oar
#

yes

#

finally haha

ruby lintel
#

so solved i guess?

fallen oar
#

yes thank ou sooooo much

fallen oar
#

!solved

peak zodiacBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

ruby lintel
vagrant haven
#

Finally