I have been working on this assignment, and it says i have to sort a 2d record array based on the type of record so if its an id its an int and if a name its a string, my problem is when i begin using my bubble sort, it seems to not save the previous Array so it does not sort or anything. The Search(RecordArray,temp,n,i) is a function that finds the comma and gets all values before it to compare later on.
This is just my bubble sort. Thanks in advance.

for (int i=1;i<length;i++)
				{	
					char temp1[100]="";
					char temp2[100]="";
					Search(RecordArray,temp1,n,i);
					Search(RecordArray,temp2,n,i+1);
					int x,z;
					x=atoi(temp1);
					z=atoi(temp2);
					if(x>z)
					{
						char *y=&RecordArray[i][256];
						RecordArray[i][256]=RecordArray[i+1][256];
						RecordArray[i+1][256]=*y;	
					}	
					
				}

i changed it a little, but its still not swapping:(

for (int i=2;i<length;i++)
				{	
					for(int j=i+1;j<length-2;j++)
					{
					char temp1[100]="";
					char temp2[100]="";
					Search(RecordArray,temp1,n,i);
					Search(RecordArray,temp2,n,j);
					int x,z;
					x=atoi(temp1);
					z=atoi(temp2);
					if(z<x)
					{
						char *y="";
						y=&RecordArray[i-1][256];
						RecordArray[i][256]=RecordArray[j][256];
						RecordArray[j][256]=*y;
					}	
					else if(RecordArray[i][256]==NULL)
							break;

					}

try this.

#include<cstring>

char*  y = '\0';
char temp1[100];
char temp2[100];

for (int i=0; i<length-1; i++)
{
     Search(RecordArray,temp1,n,i);
     Search(RecordArray,temp2,n,i+1);  
     
     if(strcmp(temp1, temp2)> 0)
     {
          y = &RecordArray[i][254];
          RecordArray[i][254] = RecordArray[i+1];
          RecordArray[i+1][254] = *y;
     }
}

strcmp() will only give ye' the results you want if both arguments are of equal case (i.e. all upper or all lower case).

also, I am assuming you made RecordArray[ ][256], which means you lose one element for being zero based, and another element for null termination.

Its still not working, i actually have no idea why...

Please post the definition of your RecordArray variable. So far I can't tell why if the string found in one place (or its integer conversion) is greater than another, why you're swapping the last items between the two places.

Let's start with:
What data are you storing?
What does it mean to sort that data?

Record Array can store both string, and int type,
ex.
studentid,name
104, john
103, adam
102, bob

so if they tell us to sort by student id it would be int, if name it would be by string.

ah about time i got it!!! it had to do with me setting

char *y="";
y=&RecordArray[i-1][256];
RecordArray[i][256]=RecordArray[j][256];
RecordArray[j][256]=*y;

should have been

char y[256]="";
	strcpy(y,&RecordArray[i][0]);
        strcpy(&RecordArray[i][0],&RecordArray[j][0]);
	strcpy(&RecordArray[j][0],y);

so happy!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.