Hello , this is my first time attempting to sort an array of strings and I am having some difficulty. My code is to read in first names and last names into two 2D arrays , sort them into a 3D array and then print them in rows and columns , the program runs but the results are not expected. The problem is that I get alternate strings being printed. I being a character I have come to realize must be indicating an empty element and the other the first string to be sorted into the array.
Code:#include<stdio.h>#include<stdlib.h> #include<string.h> #define CLASS_SIZE 6 #define ROW 2 #define COLUMN 3 #define MAX 15 void getdata(char name1[][MAX] , char name2[][MAX] , int size); void bubbleSort(char name1[][MAX] , char name2[][MAX] , int size); void seatStudents(char seat[][COLUMN][MAX * 2], char name1[][MAX] , char name2[][MAX] , int row , int column); void displayChart(char seat1[][COLUMN][MAX *2] , int row , int column); int main() { char firstName[CLASS_SIZE][MAX]; char lastName[CLASS_SIZE][MAX]; char Seat[ROW][COLUMN][MAX * 2]; getdata(firstName , lastName , CLASS_SIZE); bubbleSort(firstName , lastName , CLASS_SIZE); seatStudents(Seat , firstName , lastName , ROW , COLUMN); displayChart(Seat , ROW , COLUMN); system("PAUSE"); return 0; } void getdata(char name1[][MAX] , char name2[][MAX] , int size) { int i; for(i = 0; i < size; i++) { printf("Enter a student's first name\n"); scanf("%s" , name1[i]); printf("Enter the student's last name\n"); scanf("%s" , name2[i]); } } void bubbleSort(char name1[][MAX] , char name2[][MAX] , int size) { int i; int pass; char temp1[MAX]; char temp2[MAX]; for(pass = 0; pass < size; pass++) { for(i = 0; i < size; i++) { if(strcmp(name2[i] , name2[i + 1]) > 0) { strcpy(temp1 , name1[i]); strcpy(name1[i] , name1[i + 1]); strcpy(name1[i + 1] , temp1); strcpy(temp2 , name2[i]); strcpy(name2[i] , name2[i + 1]); strcpy(name2[i + 1] , name2[i]); } } } } void seatStudents(char seat[][COLUMN][MAX * 2] , char name1[][MAX] , char name2[][MAX] , int row , int column) { int i; int j; int k = 0; for(i = 0; i < row; i++) { for(j = 0; j < column; j++) { strcpy(seat[i][j] , name1[k]); strncat(seat[i][j] , name2[k] ,(strlen(name2[k]) + strlen(name1[k]))); } } } void displayChart(char seat[][COLUMN][MAX] , int row , int column) { int i; int j; for(i = 0; i < row; i++) { for(j = 0; j < column; j++) { printf("%4s" , seat[row][column]); } printf("\n"); } }



LinkBack URL
About LinkBacks


