#help
262 messages · Page 1 of 1 (latest)
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.
#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
?
the instructions are hard to read.
regardless having a 32768 * unsigned array on the stack isn't smart
witch part are you talking about?
unsigned int ordered_input[size];
okay should i just remove unsigned?
no. you should utilize the heap instead
how?
using an allocator of a sort. libc has one (in form of *alloc family of functions), there are more out there
#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
i don't see how those are similar sorry.
as a side note you shouldn't srand more than once
@ruby lintel are u here?
okay but i have never used this ever
there's always a first time to anything 🙂
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
🤷♂️
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
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?
since i couldn't read the instructions - i have no idea.
regardless they:
- don't call
srandmore than once - use proper heap allocation
these are the full instructions
right. you can structure you function however you like, however (for the 3rd time by now) - you should:
allocate such big arrays on the heap (even though modern linux provide 8MB of stack for its main thread).
call srand once.
fix those 2 and see if it matches the instructions
yeah
i have the same code and no issues
mingw on vscode
okay but i still dont know how to do that
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?
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>
kinda sorta. the cast is redundant
unsigned int *almost_ordered_input = malloc(size * sizeof(unsigned int));```
thats better
{
printf("Memory allocation failed. Exiting...\n");
return 1;
}```
from what ouput i'm getting the sorting algorithm is working very well
is this the ouput you want?
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
you can't open the link?
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
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
a. good to see you're enabling flags.
b. read through them (most of them) are self explanatory
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
@fallen oar is the output the one you want?
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
i dont know this is what the example output is
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
and what do you suppose this is doing:
unsigned int *ordered_input = malloc(size * sizeof(unsigned int));
...
int ordered_input[size];
```?
#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;
}```
better. you just forgot to free it at the end #1233369228275486743 message
and to include stdlib ofc
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
and? did you read any of them at all?
yes i did
ok, and what didn't you understand?
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?
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)
okay but it does not say unsigned anywhere in this function?
again - you function expects a signed type, you supplied it an unsigned type - hence the warning
indeed you function doesn't expect an unsigned
again- i dont understand what that means
int *reverse_ordered_input = malloc(size * sizeof(unsigned int));```
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
in the input_gen.c file, change the int arr[] to unsigned int arr[]
in the different input_gen functions
void generate_ordered_input(unsigned int arr[], int n)like this?
suppling answers without explaining the reason behind it - won't help much. especially on such a fundemental thing
do i keep them like this in main or do i still have the unsigned?
up to you and/or your instructions
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
ok i'll stop np
please don't ever include .c files
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```
that's good
i dont really understand what they want the output to be?
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?
Ohh is it like fopen and stuff?
can you do that?
Hmm maybe
ok try
#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?
first , you don't need the printArray function any more
read them and fix them
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```
you do not need the print array function anymore
why cant we just keep the print array function?
i have removed it
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?
from sorting.c?
yes
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?
because we don't need it
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;
}```
did you try it to see if it works?
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
you're not using the op variable
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
why do you have bubbleSort function in you main file
becuse you told me to add it instead of the printarray function?
here?
should i remove it?
if i remove it runs but nothing happens
i meant call it not define it
okay but do i not do that here #include "sorting.h"
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
i got my code to do this
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
you don't need it to i think
i think it does but my output is not quite right
you need the operation count, that's all
no
but i don't think you need to
oh okay how do i do that?
its okay! hmm i am not too sure
dont we already have an operation count in bubblesort from task 3,2?
you just need one counter
oh okay
int op = 0;
ohhh
and then you need a pointer
int op = 0;
yes and then another variable
which will be a pointer to the op variable
show me
how you would do it
right
then you will do bubbleSort(ordered_input, size, op_p);
you will do the same for each input type
reverse, randomized, and almost ordered
do i put both int op the pointer and the bublesort under each of them?
or just the bubblesort function?
{
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;
}```
hmm no
it is in the input_gen.h as well
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
its okay haha
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
oh okay
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
{
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;
}```
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,...);```
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
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
yes
Ordered input: 32640 operations like this?
is it becuse i have new line here? fprintf(output_file, "\n");
yes, in these types of lines, fprintf(output_file, "Almost ordered input (Run %d): \n", run + 1); take out the "\n"
this is making a new line
fprintf(output_file, "Almost ordered input (Run %d): ", run + 1);
that should do the trick
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
you didn't take out the right new line
oh sorry wait
take out the one after you print ordered input and such
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
maybe add another fptinf("\n") just after you print the number of operations for extra readability
fprintf(output_file, "Reverse ordered input: ");``` after these?
it says there are too few arguments in function call
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```
this looks good
so solved i guess?
yes thank ou sooooo much
!solved
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
no problem!
Finally