How to create a temp in a 2D Array sort
Mar 30, 2013 at 9:19pm UTC
Hello, I am sorry to bother you guys again, but I'm unsure of how to properly finish up this program. I am unsure of how one is supposed to make a temp for sorting a 2D array.
Once again, this is C++!
Here is the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort(char [][11]); //initializing functrion
main()
{
char state[3][11]; //initializing array
char str1[]="Florida" ; //initializing needed strings
char str2[]="Oregon" ;
char str3[]="California" ;
char str4[]="Georgia" ;
strcpy(state[0], str1); //inserting value of strings into array
strcpy(state[1], str2);
strcpy(state[2], str3);
strcpy(state[3], str4);
//sort(state); //calls upon sort function
printf("%s\n" , state[0]); //prints sorted strings
printf("%s\n" , state[1]);
printf("%s\n" , state[2]);
printf("%s\n" , state[3]);
system("PAUSE" );
return 0;
}
void sort (char Str[][11])
{
int i;
char temp; //for switching out values
int flag=1; //true false flag
int d=4; //shell sort distance
while (flag =1 || d>1) //shell sort function
{
flag=0;
d=(d-1)/2;
for (i=0; i<4-d; i++)
{
if ( strcmp(Str[i+d],Str[i])>0)
strcpy(temp[0],Str[i+d]);
strcpy(Str[i+d],Str[i]);
strcpy(Str[i],temp);
flag=1;
}
}
return ;
}
Mar 30, 2013 at 10:54pm UTC
I managed to solve the problem on my own! Here is the solution for any who need it. It should be mentioned however that the sort still does'nt work!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort(char [][11]);
main()
{
char state[3][11];
char str1[]="Florida" ;
char str2[]="Oregon" ;
char str3[]="California" ;
char str4[]="Georgia" ;
strcpy(state[0], str1);
strcpy(state[1], str2);
strcpy(state[2], str3);
strcpy(state[3], str4);
//sort(state);
printf("%s\n" , state[0]);
printf("%s\n" , state[1]);
printf("%s\n" , state[2]);
printf("%s\n" , state[3]);
system("PAUSE" );
return 0;
}
void sort (char Str[][11])
{
int i;
char temp[3][10];
int flag=1;
int d=4;
while (flag =1 || d>1)
{
flag=0;
d=(d-1)/2;
for (i=0; i<4-d; i++)
{
if ( strcmp(Str[i+d],Str[i])>0)
{
strcpy(temp[0],Str[i+d]);
strcpy(Str[i+d],Str[i]);
strcpy(Str[i],temp[0]);
flag=1;
}
}
}
return ;
}
Topic archived. No new replies allowed.