# 3 days failing to get bubblesort working .. devastated :(

i am a beginner, i got linear search working quite fast.. but then total stuck.

i tried to write function test, that gives me some random numbers and sort them.

I watched all the videos and thought i understood how points work , but apparently not.

I dont even get the array pointer working right and fill it with the random numbers. (I get a core dumb )

Here is my code:

``````#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
int n = GetInt();
int* array[n];

int i;
for (i=0; i<n;i++)
{
*array[i]= rand();
printf("Eingabe: %d : %d\n",i,*array[i]);
}
}

void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}

void sort(int* array[], int x)
{
int i;
int counter;

do
{
counter = 0;
for (i=0;i<x; i++)
{
if (array[i]>array[i+1])
{
swap(array[i], array[i+1]);
counter = 1;
}
}
}
while (counter==1);
}
``````

I hope someboby has mercy and gives me some advice or tip or hint... i sooooo wanna learn programming. so i would hate to have to give up so early :(

why do you use int* array[n]; and not int array[n];?

you aren't calling your sort() function.

• Hello, i was going off the example given in the lecture. I thought i have to create that *int array to later acess it via that pointer. Does that mean my sort function should also just take in the array and not the pointer ? Also it takes in x, which should be the number of elements in the array i guess.. but nowhere do i find how i get x beforehand :( Sorry but i just dont understand some things and dont find any explanation for it – Stephan Ketterer Feb 5 '15 at 15:47
• x is the number of elements in the array. you ask for that number at the beginning of your code (n). then you create the array but putting in n random numbers. Next you need to call your sort function. You don't need to define the array this way, you can always pass the pointer to the function. Here's a related question on stackoverflow, that might help you: [link]stackoverflow.com/questions/1670821/… – gitterb Feb 5 '15 at 16:01
• and perhaps it's easier if you would try to implement the sort without using a function. Same with swap. if you really want to use function, you will also need to declare prototypes before your main() – gitterb Feb 5 '15 at 16:03
• I really appreciate your help i just cannot understand it :( – Stephan Ketterer Feb 5 '15 at 16:06
• I really appreciate your help i just cannot understand it :( 1. If i start if the swap function it takes in 2 pointers, so i can only feed it 2 points and not some array values as far as i can understand. 2. Ok so i have to write the 2 functions with semicolons before the main function as a prototype so the program knows , something is coming. 3. I tried to adress my problems in 1 by creating 2 pointer variables that i feed into swap.. will post my code.. but i just errors – Stephan Ketterer Feb 5 '15 at 16:10

As far as I know, pset3 "find less comfortable" uses selection sort, while the more difficult version does not use bubble sort. Anyway, here is the pseudo code for it:

``````    begin BubbleSort(list)

for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for

return list

end BubbleSort
``````