2 Replies - 28893 Views - Last Post: 26 January 2008 - 04:29 AM Rate Topic: -----

#1 hesse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-January 08

bubble sort a two dimensional array

Posted 25 January 2008 - 03:19 AM

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 10
#define MIN 5

/*function prototype*/
 void initializeArray(int[][MAX]);
void printArray(int [][MAX]);
void populateRandomValues (int [][MAX],int, int);
void bubblesort(int [][MAX], int,int);

void main(void)
{
	int array[MIN][MAX], temp, max, key;
	int lower, upper= MAX*10;
	initializeArray (array);
	printArray (array);
	populateRandomValues (array, lower, upper);
	printArray (array);
	bubblesort(array, temp,max);
	printArray (array);
}

void printArray(int array[][MAX])
{
   int r, c;
   for(r=0;r<MIN;r++)
	  {
	   for(c=0;c<MAX;c++)
		 {
		 printf(" %d",array[r][c]);
		  }
	  printf("\n");
	 }
printf("\n");
}
		 
	  
void populateRandomValues (int array[][MAX],int lower, int upper)
{
int r,c;
 srand(time(NULL));
   for(r=0;r<MIN;r++)
	{
	 for(c=0;c<MAX;c++)
	  {
		array[r][c]=1+(rand()%100);
	  }
	}
}void bubblesort(int array[][MAX], int temp,int max)
{
int r,k,c,z; 
	for(r=0;r<MAX;r++)
	 {   
	  for(c=0;c<MAX-1;c++)
		{
		 if(array[z][c] > array[z][c+1])
		   {
		   temp= array[z][c];
		   array[z][c]= array[z][c+1];
		   array[z][c+1]= temp;
		   }
		 if(c % (MAX-2) == 0)
		   {
			for(z=0;z<MAX;z++)
			 {
			  if(array[r][c]>array[r+1][z])
			   {
				temp = array[r][c];
				array[r][c] = array[r+1][z];
				array[r+1][z] = temp;
			   }
			 }
		   }
		 }
	   }
}


* mod edit - added code tags

Is This A Good Question/Topic? 0
  • +

Replies To: bubble sort a two dimensional array

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: bubble sort a two dimensional array

Posted 25 January 2008 - 03:40 AM

Hi hesse, welcome, maybe you want to post this as a snippet?
If so make sure the code meets the requirements for submitting a snippet and post it here:

http://www.dreamincode.net/code/
Was This Post Helpful? 0
  • +
  • -

#3 rockey  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 02-January 08

Re: bubble sort a two dimensional array

Posted 26 January 2008 - 04:29 AM

What error is occur??
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1