Jump to content

Coding challenge


snakey

Recommended Posts

  • Replies 237
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

is this the code that you want?

only runtime errors and not syntax errors

#include <iostream>
#include <ctime>
using namespace std;

char get_Char();
int get_Int();
template <class Type>
bool compare(Type,Type);

int main(){
    int myNum, randNum;
    char myChar, randChar;
    char again;
    
    cout<<"  Welcome to the Awesome Game  n";
        
    do{
        cout<<"*******************************n";
        myChar = get_Char();
        myNum = get_Int();
        
        randChar = rand()%4;
        randNum = rand()%3;
        
        if(compare<int>(myNum, randNum) && compare<char>(myChar, randChar))
            cout<<"You  Winn";
        else
            cout<<"You Loosen";

        cout << "do you want to play again(y/n)? ";
        cin >> again;

    }while(again = 'y');  

    cout<<"*******************************n"
        <<"Thanks for Playingn";
    return 0;
}

char get_Char(){
    char temp;
    cout<<"enter a character(a-d): ";
    cin>>temp;
    return temp;
}
int get_Int(){
    int temp;
    cout<<"enter an integer(1-3): ";
    cin>>temp;
    return temp;
}
template <class Type>
bool compare(Type a,Type b){
    if(a=b)
        return true;
    else
        return false;
}

edited after sablefoxx's comment #55  and my comment #56

Link to comment
Share on other sites

Compiler catches some of it:

./guess.cpp: In function ‘int main()’:

./guess.cpp:33: warning: suggest parentheses around assignment used as truth value

./guess.cpp: In function ‘bool compare(Type, Type) [with Type = int]’:

./guess.cpp:25:  instantiated from here

./guess.cpp:54: warning: suggest parentheses around assignment used as truth value

./guess.cpp: In function ‘bool compare(Type, Type) [with Type = char]’:

./guess.cpp:25:  instantiated from here

./guess.cpp:54: warning: suggest parentheses around assignment used as truth value

But it does compile, and the program does not work (just the way its supposed to?)

Link to comment
Share on other sites

sablefoxx what compiler are you using?

I'm using Microsoft Visual Studio 2005

and I don't see any warnings or errors

this is not one of the errors but should fix some of the strict warnings your compiler is giving you

try  replacing  line 25:  if(compare(myNum, randNum) && compare(myChar, randChar))

with:  if(compare<int>(myNum, randNum) && compare<char>(myChar, randChar)) 

Link to comment
Share on other sites

i am using g++, im in Ubuntu 7.10  :)

Link to comment
Share on other sites

here are the solutions:

1 and 2: line #33 & 53 }while(again = 'y');    if(a=b)

there is a logical error.

the = will alaways return  true.

this is because the compiler sees it as an assignment.

the = should be == to make it a comparison.

3: line #22 randChar = rand()%4;

the range of comparible characters is not a,b,c

instead it has a range of: null, ?, ?, ?

to fix this add 'a' or 97 to get it in the range of a-d

4: line #2  #include <ctime>

This is not the error but a hint.

You can write a line that initializes the random number

based on the system time.

srand(time(0));

This allows you to be able to run the app without having it

choose the same random number every time.

Congrats to: sablefoxx and TheWarden for finding errors 1, 2, and 3

Here is the Next Challenge:

Wrte a Program that

1. an integer is inputed

2. the number is converted to binary

3. output the binary number

Bonus: make it also do Octal and/or Hexadecimal

Link to comment
Share on other sites

  • 2 weeks later...

FIRST POST AT HAK. 5!

Code for the challenge IOCCC style. . .

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;assert.h&gt;

void print_str(unsigned char *str) {
    printf("%c", *str);
     while (*str != str[-1]) {
        str++;
        printf("%c", *str - (-1)[str]);
    }
}

int main(int argc, char *argv[]) {
    char str1[] = { 0x2a, 0x54, 0x7e, 0xa8, 0xd2, 0xfc, 0x26, 0x50, 0x7a,
                    0xa4, 0xce, 0xf8, 0x22, 0x4c, 0x76, 0xa0, 0xca, 0xf4,
                    0x1e, 0x48, 0x72, 0x9c, 0xc6, 0xf0, 0x1a, 0x44, 0x6e,
                    0x98, 0xc2, 0xcc, 0xf6, 0x16, 0x6a, 0xd2, 0x3b, 0xae,
                    0xce, 0x37, 0xaa, 0xca, 0x39, 0x9b, 0x01, 0x76, 0xe9,
                    0x4a, 0xbe, 0x23, 0x87, 0xa7, 0x0a, 0x79, 0xdd, 0x42,
                    0x62, 0xd6, 0x45, 0x65, 0x8f, 0x99, 0xc3, 0xe3, 0x53,
                    0xc5, 0x2e, 0x9c, 0x10, 0x30, 0x9e, 0x13, 0x80, 0xe2,
                    0x47, 0xb9, 0x2c, 0x4c, 0xb5, 0x23, 0x43, 0xa5, 0x0e,
                    0x7c, 0xdd, 0x4f, 0xc8, 0xf6, 0x16, 0x36, 0x60, 0x6a,
                    0x94, 0xbe, 0xe8, 0x12, 0x3c, 0x66, 0x90, 0xba, 0xe4,
                    0x0e, 0x38, 0x62, 0x8c, 0xb6, 0xe0, 0x0a, 0x34, 0x5e,
                    0x88, 0xb2, 0xdc, 0x06, 0x30, 0x5a, 0x84, 0xae, 0xd8,
                    0x02, 0x2c, 0x36, 0x36 }; /* no, it's not ascii */
    char str2[] = { 0x45, 0xb3, 0x27, 0x8c, 0xfe, 0x1e, 0x7f, 0x9f, 0x0d,
                    0x82, 0xef, 0x51, 0xb6, 0x28, 0x48, 0xbc, 0x2b, 0x4b,
                    0xad, 0x12, 0x32, 0x95, 0x04, 0x72, 0xe8, 0x4d, 0xbf,
                    0x33, 0x98, 0xfc, 0x1c, 0x90, 0xff, 0x1f, 0x87, 0xec,
                    0x64, 0x9e, 0xbe, 0xbe }; // ascii strings are NULL terminated
    char str3[] = { 0x54, 0xbc, 0x25, 0x98, 0xb8, 0x21, 0x94, 0xb4, 0x28,
                    0x90, 0xf5, 0x15, 0x84, 0xf9, 0x6d, 0xdd, 0x52, 0xc6,
                    0xe6, 0x55, 0xbb, 0xdb, 0x52, 0xb3, 0x2c, 0x4c, 0x7d,
                    0x87, 0x87 };
    char str4[] = { 0x54, 0xbc, 0x25, 0x98, 0xb8, 0x21, 0x94, 0xb4, 0x28,
                    0x90, 0xf5, 0x15, 0x84, 0xf9, 0x6d, 0xdd, 0x52, 0xc6,
                    0xe6, 0x55, 0xbb, 0xdb, 0x52, 0xb3, 0x2c, 0x4c, 0x7e,
                    0x88, 0x88 };
    void **p;
    print_str(str1);
    print_str(str2);
    scanf("%d", *((!(*((!(p = malloc(sizeof(int))) ? abort() : p), p) = 
                     malloc(sizeof(int))) ? abort() : p), p));
    void *buf;

    /* way one */
    sprintf((((buf = malloc(42 /* more than the meaning of life */)) == NULL
             ? abort() : buf), buf), "%x", 0[0[(int **) p]]);
    print_str(str3);

    while (*(char *)buf) {
        if (*(char *)buf &gt;= 'a')
            *(char *)buf -= 'a' - 10;
        else
            *(char *)buf &amp;= ~'0';
        printf("%d", (*(char *)buf &amp; 8) &gt;&gt; 3);
        printf("%d", (*(char *)buf &amp; 4) &gt;&gt; 2);
        printf("%d", (*(char *)buf &amp; 2) &gt;&gt; 1);
        printf("%d", (*(char *)buf &amp; 1));
        buf++;
    }
    printf("n");

    /* way two */
    print_str(str4);
    char i, j;
    i = 3 + (j ^= j);
    do {
        if (!(j % 8)) {
            i[0[(char **) p]] = ((i[0[(char **) p]] &amp; 0xf0) &gt;&gt; 4) |
                                ((i[0[(char **) p]] &amp; 0x0f) &lt;&lt; 4);
            i[0[(char **) p]] = ((i[0[(char **) p]] &amp; 0xcc) &gt;&gt; 2) |
                                ((i[0[(char **) p]] &amp; 0x33) &lt;&lt; 2);
            i[0[(char **) p]] = ((i[0[(char **) p]] &amp; 0xaa) &gt;&gt; 1) |
                                ((i[0[(char **) p]] &amp; 0x55) &lt;&lt; 1);
        }
        printf("%d", (i[0[(char **) p]] &gt;&gt; j) &amp; 1);
    } while ((j = (++j % 8)) || i--);
    printf("n");
    return 0;
}

Have fun if you try to decipher it  :-P

Link to comment
Share on other sites

it's definitely valid... output:

jordan@jordan-desktop:~$ gcc -Wall -o main01 main01.c 
main01.c: In function ‘main’:
main01.c:42: warning: pointer targets in passing argument 1 of ‘print_str’ differ in signedness
main01.c:43: warning: pointer targets in passing argument 1 of ‘print_str’ differ in signedness
main01.c:45: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘void *’
main01.c:51: warning: pointer targets in passing argument 1 of ‘print_str’ differ in signedness
main01.c:67: warning: pointer targets in passing argument 1 of ‘print_str’ differ in signedness
jordan@jordan-desktop:~$ ./main01 
*****************************
* This is obfusated code to *
* print numbers in binary.  *
*****************************
Enter a number to be converted to hex: 100
This is the output of way 1
01100100
This is the output of way 2
00000000000000000000000001100100
jordan@jordan-desktop:~$ ./main01 
*****************************
* This is obfusated code to *
* print numbers in binary.  *
*****************************
Enter a number to be converted to hex: 64
This is the output of way 1
01000000
This is the output of way 2
00000000000000000000000001000000
jordan@jordan-desktop:~$ ./main01 
*****************************
* This is obfusated code to *
* print numbers in binary.  *
*****************************
Enter a number to be converted to hex: 256
This is the output of way 1
000100000000
This is the output of way 2
00000000000000000000000100000000
jordan@jordan-desktop:~$

The compiler complains a bit and it says 'converted to hex' instead of 'converted to binary', but it does the job, twice (one uses printf to get it to hex first and I decided that was cheating, so I solved it a different way too).

Link to comment
Share on other sites

:-P easy!

#include &lt;stdio.h&gt;
int main( ){
         void bin (int); //function proto
         unsigned int i; 
          printf("nnInput: ");
          scanf(" %d", &amp;i); //get input
          printf("Bin = ");
          bin(i); //send i to get converted
          printf("nOct = %o n", i); //octal
          printf("Hex = %x n", i); //hex
          return 0;}
   
void bin (int i){
  while (i != 0){
    printf ("%d",i % 2);
    i = i /2;}
  return;
}

Link to comment
Share on other sites

wtf, why does he win?  You cant even read the damn code!  Are we trying to make the best bloatware?

Im not trying to say mine was better, but we should strive to promote, easy to read, and well written code.

just a thought  :P , looking forward to next challenge!

Link to comment
Share on other sites

wtf, why does he win?  You cant even read the damn code!  Are we trying to make the best bloatware?

That was the point... Also, I'm pretty sure bloatware is unneeded features/programs.  This would be classified as obfuscated code.  Hint:

Code for the challenge IOCCC style...

I was kinda trying to make illegible code...

In other news, the next challenge may (should) be deciphering what I wrote...

Link to comment
Share on other sites

  • 2 weeks later...
*** Coding Challange April 11-25 2008 ***


1. Must be written in C / C++ / VBS
2. Take in any number of values (can be user defined or read values until EOF)
3. Sort Values, Largest to Smallest
4. Use least chrs possible (white space/comments don't count)
5. Code should be clean and well commented
6. Must print entire array before/after sort
7. You can only use one Print function (you can use as many other functions as you want)

~2 Weeks, Good Luck
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.

×
×
  • Create New...