Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


C - annoying question
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Thu Sep 10, 2009 10:59 am    Post subject: C - annoying question Reply with quote

i'v got this question i can't solve...
no matter how hard i think i can't think of a solution
i have to write a program that gets as input a letter (Capital)
and output the forward letter that comes after
and if the input is 'Z' so it should output 'A'
but the catch is i'm not allowed to use conditions
so it suppose to be some ascii code manipulation
please help me lol XD i tried so much time
thanks
Back to top
View user's profile Send private message
kot1990
Expert Cheater
Reputation: 1

Joined: 06 Sep 2009
Posts: 131
Location: Greece

PostPosted: Thu Sep 10, 2009 11:38 am    Post subject: Reply with quote

Why can't you use conditions? What kind of programming is this? Give an example.
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Thu Sep 10, 2009 12:00 pm    Post subject: Reply with quote

Code:
char GetNextCapitalChar(char theChar)
{
   int f = (int)(theChar - 'A') / ('Z' - 'A');
   return f*('A') + (f^1)*theChar + (f^1)*1;
}


Be sure to put my name in the comments when you hand it in. Rolling Eyes
Back to top
View user's profile Send private message
kot1990
Expert Cheater
Reputation: 1

Joined: 06 Sep 2009
Posts: 131
Location: Greece

PostPosted: Thu Sep 10, 2009 12:28 pm    Post subject: Reply with quote

Well I've got a simpler solution by creating a charset with one more letter after Z, so the next will be A again

Code:
#include <stdio.h>

int main()
{
   int i;
   char charset[27]; //memory for 27 characters. 26 for A-Z kai one more for the problem of Z.
   char letter,enter;
   for(i=0;i<=25;i++) //fills the charset with the correct character values.
      charset[i] = i + 65;
   charset[26] = 65; //the 27th letter will be again A :)
   printf("Give a letter: ");
   scanf("%c",&letter);
   scanf("%c",&enter);
   printf("%c",charset[letter-65+1]); //shows the next letter from charset.
   getchar();
}
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Thu Sep 10, 2009 1:54 pm    Post subject: Reply with quote

nice way athaem but i forgot to mention that i can't use bitwise operations
i only got + - * / and module (%).
and my guess it should start by using %, correct me if i'm wrong
Back to top
View user's profile Send private message
kot1990
Expert Cheater
Reputation: 1

Joined: 06 Sep 2009
Posts: 131
Location: Greece

PostPosted: Thu Sep 10, 2009 2:12 pm    Post subject: Reply with quote

Great code Athaem Smile
I was looking to your code about half an hour to understand what it does!
How could you think something like that? just super. I just wanted to say that maybe bitwise operation is still a compare like if but I'm not sure.
I can't think of another code, because the operation is like
output = input + 1;
if it gets to "Z" you will need a condition to check, so output = input + 1; won't work.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Sep 10, 2009 3:03 pm    Post subject: Reply with quote

Code:
char NextCapital( char GivenChar )
// Precondition : 'A' <= GivenChar <= 'Z'
{
  return (char)(((int)(GivenChar + 1 - 'A'))%26 + (int)'A');
}


you can probably get rid of some of the typecasts/brackets but you get the idea
Back to top
View user's profile Send private message
kot1990
Expert Cheater
Reputation: 1

Joined: 06 Sep 2009
Posts: 131
Location: Greece

PostPosted: Thu Sep 10, 2009 3:17 pm    Post subject: Reply with quote

Just a single simple line xD. Slugsnack owns!!! , and uses no extra memory and loops like mine.
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Thu Sep 10, 2009 3:38 pm    Post subject: Reply with quote

I suggest inlining the function.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Sep 10, 2009 3:41 pm    Post subject: Reply with quote

void:] wrote:
I suggest inlining the function.

i'm pretty sure it's for some sort of assignment where he just has to have that function
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Sep 10, 2009 6:34 pm    Post subject: Reply with quote

kot1990 wrote:
Well I've got a simpler solution by creating a charset with one more letter after Z, so the next will be A again

Code:
#include <stdio.h>

int main()
{
   int i;
   char charset[27]; //memory for 27 characters. 26 for A-Z kai one more for the problem of Z.
   char letter,enter;
   for(i=0;i<=25;i++) //fills the charset with the correct character values.
      charset[i] = i + 65;
   charset[26] = 65; //the 27th letter will be again A :)
   printf("Give a letter: ");
   scanf("%c",&letter);
   scanf("%c",&enter);
   printf("%c",charset[letter-65+1]); //shows the next letter from charset.
   getchar();
}

btw, short note. the 65 in your case is of course ascii for 'A'. in programming, numbers such as this, ie. numbers with a significant value, are called magic numbers. magic numbers should not be simply embedded in the code as you have done, it is considered very bad practice. you should either use 'A' instead, which will generate the same code but improve readability or you could use #define like so :

Code:
#define FIRSTCAPITAL 65


or :
Code:
#define FIRSTCAPITAL 'A'


Last edited by Slugsnack on Sat Sep 12, 2009 4:51 pm; edited 1 time in total
Back to top
View user's profile Send private message
FullyAwesome
I post too much
Reputation: 0

Joined: 05 Apr 2007
Posts: 4438
Location: Land Down Under

PostPosted: Fri Sep 11, 2009 3:13 am    Post subject: Reply with quote

this also works:

Code:
char GetOpposite(char c)
{
   return 'Z' - c + 'A';
}


where logically, you want to get the order the letter is in the alphabet (ie B = 2) by subtracting your letter's ANSCII code from the largest possible, and then add where the letters start in ANSCII code ('A' or 65).

_________________
Back to top
View user's profile Send private message MSN Messenger
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Fri Sep 11, 2009 3:30 am    Post subject: Reply with quote

Modularity. Razz
Code:
(c + 1 - 'A') % 26 + 'A'

Work in a modular way, c is the character, get next letter by adding 1 and subtract by 'A' to convert to integer instead ASCII, then use modulo 26 to stay within the range 0~25, since there are 26 letters in the English abc. then simply add 'A' to convert back to ASCII.

For c = 'A' you get 'A' + 1 = 'B' (65 + 1 = 66), subtract 65 and you get 1 (note that 'A' = 0 and 'B' = 1. starting from 0), 1 % 26 = 1, then when you add 'A' you get 'A' + 1 again, which is 'B'.

For c = 'Z' you get 'Z' + 1 = 90 + 1 = 91, subtract 65 and you get 26. 26 % 26 is 0, so you get 'A' + 0 = 'A'.

Edit: Oh, I see slugsnack already showed this way. Surprised
My bad...
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Fri Sep 11, 2009 5:28 am    Post subject: Reply with quote

too bad he's not allowed conditions else he could do something like this :
Code:
char NextCapital( char GivenChar )
// Precondition : 'A' <= GivenChar <= 'Z'
{
  return (GivenChar == 'Z'?'A':(GivenChar+1));
}
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Fri Sep 11, 2009 9:52 am    Post subject: Reply with quote

awesome way Slugsnack really helped me
and thanks for all the others you're awesome too :]
took me few minutes to get the idea of your way Slugsnack
and then i got that with Z entered it'd be 0 value :]
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites