Bubble sort 2D int array with c

Click For Summary
SUMMARY

The discussion centers on implementing a bubble sort algorithm for a 2D integer array in C, specifically sorting an array defined with 15 rows and 2 columns. The user encounters compilation errors due to incorrect function argument types when calling the bubblesort function. The solution involves passing the entire 2D array to the function instead of a single row, correcting the function call to bubblesort(deck). Additionally, the code contains logical errors in the sorting loops that need to be addressed for proper functionality.

PREREQUISITES
  • Understanding of C programming language syntax
  • Familiarity with 2D arrays in C
  • Knowledge of sorting algorithms, specifically bubble sort
  • Experience with function prototypes and parameter passing in C
NEXT STEPS
  • Review C programming documentation on 2D arrays and pointers
  • Study the implementation of bubble sort in C with examples
  • Learn about debugging compilation errors in C, focusing on type mismatches
  • Explore alternative sorting algorithms for 2D arrays, such as quicksort or mergesort
USEFUL FOR

C programmers, computer science students, and anyone looking to improve their understanding of sorting algorithms and 2D array manipulation in C.

clope023
Messages
990
Reaction score
130
hi, I'm not sure if there is a homework forum for programming but I thought I'd post my problem here; I'm supposed to bubble sort a 2d integer array with elements 15 and 2; this is what my code looks like so far:

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

#define cards 15
#define sides 2

void bubblesort(int items[cards][sides]);

int main()
{
int i, stime;
long ltime; ltime = time(NULL);
stime = (unsigned)ltime/2;
srand(stime);
int deck[cards][sides];

for(i=1;i<=15;i++)
{ deck[0]=i;
deck[1]=rand();
printf("%d ", deck[0]);
printf("%d\n", deck[1]);
bubblesort(deck[0]);
}

return 0;
}void bubblesort(int items[cards][sides])
{
int a,b,c,d,t;

for(a=1;a<=cards;a++)
for(b=0;b<sides;b++)
{
for(c=cards-1; c>=cards;c--)
{
for(d=sides-1; c>=sides;d--)
if(items[c-1][d-1] > items[c][d])
t=items[c-1][d-1];
items[c-1][d-1] = items[c][d];
items[c][d] = t;
}
}
}

it keeps on returning the same 2 errors

initializing argument 1 of void bubblesort(int (*)[2])
and invalid conversion from int to int(*)[2]

I'm not sure what kind of 'conversion' is going on; I've tried changing the variable array for bubblesort to pointer and to memory address but that gave extra errors; any help on what I might be doing wrong is appreciated
 
Physics news on Phys.org
On this line of your code
Code:
bubblesort(deck[i][0]);
You are sending an int to your bubblesort function, when your prototype says you are expecting a pointer to a pointer (int **). Try changing it to
Code:
bubblesort(deck)
But from looking at the way you have used memory, you are bound to have more problems.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K