Do I need to use bubble sort on the array first?
This is a discussion on consecutive numbers within the C Programming forums, part of the General Programming Boards category; Do I need to use bubble sort on the array first?...
Do I need to use bubble sort on the array first?
Last edited by SDH; 01-21-2013 at 04:08 PM.
Yes, you should sort array first. But don't forget to sort both arrays!
If you assume that difference==4 means that user have straight it's wrong. Because 4h 4s 6d 6c 8c will give you diff==4 and it's not a straight. You need different approach.
I also noticed that you have many variables like i, a or c that you use with for loop. Only i would be enough. You should keep your program less complex and more understandable.
ok thank you very much, what other method to work out a straight would you use instead?
1 sort arrays
2 use int difference[4] instead int difference
Post your code when you finish this!
I have just added bubble sort to the code and that works perfect. Where in the code do I add the new array?
here is the code too
Code:#include<stdio.h>int main(void) { int c; int n; int count1[127] = {0}; int count2[127] = {0}; int i; int a; int temp; int j; char array[5]; char array1[5]; n = 5; for (c = 0; c < n; c++) scanf("%c%c ", &array1[c], &array[c]); getchar(); for (i=0;i<n; i++) { for (i=0; i<4; i++) { for (j=i+1; j<5; j++) { if(array1[i] > array1[j]) { temp = array1[i]; array1[i] = array1[j]; array1[j] = temp; } } } for(i=0;i<5;i++) printf("%c",array1[i]); printf("\n"); ///////////////////////////////////////////////////////////////////////////////////// /////////////////////BUBBLE SORT^///////////////////////////////////////////// } for (i = 0; i < n; i++) { count1[array[i]]++; //if array[i] is N, then count1[N]++// } for (i = 0; i < 127; i++) { if (count1[i] == 5) { printf("Flush.\n"); } } for (a = 0; a < n; a++) { count2[array1[a]]++; //if array[a] is N, then count1[N]++// } for (a = 0; a < 127; a++) { if (count2[a] == 5) { printf("Flush.\n"); }else if (count2[a] == 4) { printf("Four of a kind.\n"); }else if (count2[a] == 3) { printf("Three of a kind.\n"); }else if (count2[a] == 2) { printf("Pair.\n"); }else if (count2[a] && count2[a] && count2[a] == 2) { printf("Two Pair.\n"); } } return 0; }
You are using int i, j, a and c with for loop. You do not need them all. You only need i and j. So get rid of extra variables, leave just the ones you really need. Try to implement new array by yourself(easiest way to learn), then post code.
I have an idea how to intialize it but I am not sure on where to put it in the code and how to write it in, any clues?
here is what I have come up with so far. Am I getting any closer?7
Code:#include<stdio.h>int main(void) { int c; int n; int count1[127] = {0}; int count2[127] = {0}; int i; int a; int temp; int j; int difference= 0; char array[5]; char array1[5]; n = 5; for (c = 0; c < n; c++) scanf("%c%c ", &array1[c], &array[c]); getchar(); for (i=0;i<n; i++) { for (i=0; i<4; i++) { for (j=i+1; j<5; j++) { if(array1[i] > array1[j]) { temp = array1[i]; array1[i] = array1[j]; array1[j] = temp; } } } for(i=0;i<5;i++) printf("%c",array1[i]); printf("\n"); for(i=0;i<n-1;i++) { difference=(array[i+1]-array[i]); printf("%d",difference); } }
Initialize difference as int difference[4] and use it as difference[i] in for loop.
Then if all elements of array called difference are 1 it's a straight.
And again you failed to sort both arrays.
thanks for all the help you are giving me. For some reason it still wont work, where am going wrong on using bubble sort to sort both arrays?
thank you very muchCode:#include<stdio.h>int main(void) { int c; int n; int count1[127] = {0}; int count2[127] = {0}; int i; int a; int temp; int j; char array[5]; char array1[5]; n = 5; for (c = 0; c < n; c++) scanf("%c%c ", &array1[c], &array[c]); getchar(); for (i=0;i<n; i++) { for (i=0; i<4; i++) { for (j=i+1; j<5; j++) { if(array1[i] > array1[j]) { temp = array1[i]; array1[i] = array1[j]; array1[j] = temp; } } } for (i=0;i<n; i++) { for (i=0; i<4; i++) { for (j=i+1; j<5; j++) { if(array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } } for(i=0;i<5;i++) printf("%c",array1[i]); printf("\n"); printf("%c",array[i]); } for (i = 0; i < n; i++) { count1[array[i]]++; //if array[i] is N, then count1[N]++// } for (i = 0; i < 127; i++) { if (count1[i] == 5) { printf("Flush.\n"); } } for (a = 0; a < n; a++) { count2[array1[a]]++; //if array[a] is N, then count1[N]++// } for (a = 0; a < 127; a++) { if (count2[a] == 5) { printf("Flush.\n"); }else if (count2[a] == 4) { printf("Four of a kind.\n"); }else if (count2[a] == 3) { printf("Three of a kind.\n"); }else if (count2[a] == 2) { printf("Pair.\n"); }else if (count2[a] && count2[a] && count2[a] == 2) { printf("Two Pair.\n"); } } return 0; }
If you don't know how to do this things maybe this program is still to complex for you and you should learn with something easier than this. Also try with pen and pape. Write down your algorithm and then implement it step by step and use printf to see where are you getting.
Just thought I would kep you updated, I have managed to get the code to work properly using a couple more loops and counts. Thanks for your help.