Help with writing a bit of code

Reply

Join Date: Sep 2005
Posts: 3
Reputation: psionic is an unknown quantity at this point 
Solved Threads: 0
psionic psionic is offline Offline
Newbie Poster

Help with writing a bit of code

 
0
  #1
Sep 21st, 2005
This may really simple to most people on here but i'm going crazy trying to work it out.

I was set a task by someone to write a bit of code in C to do the following:

1) Ask Operator to enter 2 things - a letter of the alphabet, (in upper or lower case) and a number.

2) Using the operator input - display to screen the letter of the alphabet - that many numbers in advance - in either upper or lower case depending on what the operator input.

3) After display to the screen, ask if they want to enter any more. If not - then exit.

E.g. - Operator enters letter f and number 3, display the character i.
Operator enters letter Z and number 5 display E.

Please help, i've tried loads of things but nothing i do seems to work, this is really doing my head in.

I'm very new to C, so any help would be most appreciated.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 902
Reputation: chrisbliss18 is an unknown quantity at this point 
Solved Threads: 23
chrisbliss18's Avatar
chrisbliss18 chrisbliss18 is offline Offline
Posting Shark

Re: Help with writing a bit of code

 
0
  #2
Sep 21st, 2005
If you've already put together some code, post the code here (put it inside [code][/code] tags so it's readable), and we can help you find your problems.
Did we help you? Did we miss the point entirely? Update your thread and let us know.
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 279
Reputation: J_Search is an unknown quantity at this point 
Solved Threads: 6
J_Search J_Search is offline Offline
Posting Whiz in Training

Re: Help with writing a bit of code

 
0
  #3
Sep 21st, 2005
psionic,

I don't know C, but I put together the following "get started" code from the following website...
http://www.experts-exchange.com/Prog..._20483573.html

It won't compile I'm sure, but the basic idea is right. I don't know anything about printing upper vs lower case, but I know you can find a true/false value for isupper. Once you find that, you can then make the output match whether or not the input was upper vs. lower case. How to do this programmatically, is beyond me.

----------------------
  1. #include <stdio.h>
  2.  
  3. int count;
  4. char alpha1;
  5. char alphabet[26];
  6.  
  7. main()
  8.  
  9. /*load aphabet into an array*/
  10. for(int i = 1; i <= 26; i++)
  11. {
  12. alphabet[1] = 'A';
  13. // keep writing till..
  14. alphabet[26] = 'Z';
  15. }
  16.  
  17. /*ask for input letter*/
  18. printf("Please enter a letter: ");
  19. scanf("%c", &alpha1);
  20.  
  21. /*ask for input number*/
  22. printf("Please enter a number: ");
  23. scanf("%int", &count);
  24.  
  25. /*find the letter's number from the array*/
  26. while(!(alphabet[i] == alpha1))
  27. {
  28. if( alphabet[i] == alpha1)
  29. position = i;
  30. else
  31. i++;
  32. }
  33.  
  34. /*add the input number to the array number*/
  35. i = i + count;
  36.  
  37. /*print the array letter, assiciated with the array number*/
  38. printf alphabet[i];

--------
It's not the answer, it may not even be right, but hopefully it helps.

J_
Last edited by Dave Sinkula; Sep 21st, 2005 at 1:25 pm. Reason: Added [code][/code] tags.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 279
Reputation: J_Search is an unknown quantity at this point 
Solved Threads: 6
J_Search J_Search is offline Offline
Posting Whiz in Training

Re: Help with writing a bit of code

 
0
  #4
Sep 21st, 2005
psionic,

I'm sorry, this completely slipped my mind.

Just before: printf alphabet[i]; you'll want to check for numbers > 26, because keep in mind the addition can end up being larger than 26 and no array values are above that.

This is simple, however, not truly functional. It won't work if you have a value larger than 52. So for this, you have to do some math.

I'm going to write it in psuedo code, becuase I really can't write it in C.
  1. {
  2.  
  3. if ( i > 52 ) /*check for numbers larger than 52 (we'll use 58)*/
  4.  
  5. d = i / 26; /* d = 58 / 26 >> d = 2.2307...*/
  6.  
  7. (some function to remove the decimal)/* d = 2 */
  8.  
  9. e = 26 * d; /* e = 26 * 2 >> e = 52 */
  10.  
  11. i = i - e /* i = 58 - 52 >> i = 6 */
  12.  
  13. printf alphabet[i]; /* " f " */
  14.  
  15. else if (i > 26) /* perhaps the number is less than 52, yet larger than 26*/
  16.  
  17. i = 26 - i /* simple subtraction */
  18.  
  19. printf alphabet[i];
  20.  
  21. else /* less than or equal to 26 */
  22.  
  23. printf alphabet[i];
  24.  
  25. }

ok so i didn't write this next part in because i didn't want to confuse you any more than I already am.

You'll need to have the program enter into a for loop where it keeps decrementing the > 52 if statement. Meaning if " i " was originaly 100, the 1st go around would end with 78. You need to again compare 78 with 52. Since it is larger, re-apply the calculation to get it to a value lower than 27.

No wonder why I'm not a programmer.

J_
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 3
Reputation: psionic is an unknown quantity at this point 
Solved Threads: 0
psionic psionic is offline Offline
Newbie Poster

Re: Help with writing a bit of code

 
0
  #5
Sep 22nd, 2005
Thanks to J_Search for all your help,

So far i've writte the below, and it all seems to work

  1. #include <stdio.h>
  2.  
  3. int count;
  4. char alpha1;
  5. int position = 0;
  6. int i = 0;
  7.  
  8. main()
  9. {
  10. /*load alphabet into an array*/
  11. char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  12.  
  13. /*ask for input letter*/
  14. printf("Please enter a letter: ");
  15. scanf("%c", &alpha1);
  16.  
  17. /*ask for input number*/
  18. printf("Please enter a number: ");
  19. scanf("%int", &count);
  20.  
  21. /*find the letter's number from the array*/
  22. for( i = 0; i < 26 && alphabet[i] != alpha1; i++);
  23. /*Check if user has not entered a letter on first question*/
  24. if(i >= 26)
  25. { /* Output message if user has not inputted a letter, then exits program*/
  26. printf("\n You have not entered a letter");
  27. }
  28.  
  29. else
  30. {
  31. /*add the input number to the array number*/
  32. i += count;
  33. i %= 26 ;
  34.  
  35. /*print the array letter, associated with the array number*/
  36. printf ("The Letter you have asked for is %c\n",alphabet[i]);
  37. }
  38. return 0;
  39. }

All i need to add now is the ability for it to accept uppercase letters, also to loop if the user wants to have another go. If anyones got some ideas on how to get it to accept uppercase from the user input, i would would be most grateful .

Thanks again
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 3
Reputation: psionic is an unknown quantity at this point 
Solved Threads: 0
psionic psionic is offline Offline
Newbie Poster

Re: Help with writing a bit of code

 
0
  #6
Sep 26th, 2005
I have one problem left that i really hoping someone can help me with.
I need to get the program to ask the user if they want another turn, then if they enter Y the program loops, if they enter N then it exits.
Everyhting i try fails.

If anyone can help please reply.

what i've got so far is below.

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. int count;
  5. char alpha1;
  6. int position = 0;
  7. int i = 0;
  8. int token1 = 0;
  9. char go[2];
  10. int flag = 0;
  11.  
  12. main()
  13. {
  14. do
  15. {
  16. /*load alphabet into an array*/
  17. char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  18.  
  19. /*ask for input letter*/
  20. printf("Please enter a letter: ");
  21. scanf("%c", &alpha1);
  22.  
  23. /*ask for input number*/
  24. printf("Please enter a number: ");
  25. scanf("%int", &count);
  26.  
  27. if(isupper(alpha1))
  28. {
  29. alpha1 = tolower(alpha1);
  30. token1 = 1;
  31. }
  32.  
  33. /*find the letter's number from the array*/
  34. for( i = 0; i < 26 && alphabet[i] != alpha1; i++);
  35. /*Check if user has not entered a letter on first question*/
  36.  
  37. if(i >= 26)
  38. {
  39. /* Output message if user has not inputted a letter, then exits program*/
  40. printf("\n You have not entered a letter");
  41. }
  42.  
  43. else
  44. {
  45. /*add the input number to the array number*/
  46. i += count;
  47. i %= 26 ;
  48.  
  49.  
  50. if(token1 == 1)
  51. alphabet[i] = toupper(alphabet[i]);
  52.  
  53. /*print the array letter, associated with the array number*/
  54. printf ("The Letter you have asked for is %c\n",alphabet[i]);
  55.  
  56. }
  57.  
  58. do
  59. {
  60.  
  61. printf("\nAnother go (Y/N)?");
  62. scanf("%s", go);
  63. // go = toupper(go);
  64. }
  65.  
  66. while ((go !='Y') && (go !='N'));
  67. }
  68.  
  69. return 0;
  70. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 11
Reputation: ramcfloyn is an unknown quantity at this point 
Solved Threads: 0
ramcfloyn ramcfloyn is offline Offline
Newbie Poster

Re: Help with writing a bit of code

 
0
  #7
Sep 26th, 2005
That seems a bit overkill, why not just cast the character to an integer and add the number then recast back to character? This also deals with the idea of outputting in the same case as was input. Of course you could also put in a check to make sure the number entered is under 25 and that the output is not greater than Z.

[PHP]
#include <stdlib>
#include <stdio.h>

int main(int argc, char* argv[])
{
char letter;
int number;
int charNum;
printf("Enter letter: ");
scanf("%c", &letter);
printf("Enter number: ");
scanf("%d", &number);
charNum = (int)letter;
charNum = charNum + number;
letter = (char)charNum;
printf ("The Letter you have asked for is %c\n", letter);
system("PAUSE");
return 0;
}[/PHP]
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help with writing a bit of code

 
0
  #8
Sep 26th, 2005
>That seems a bit overkill
It does at first glance.

>why not just cast the character to an integer and add the number then recast back to character?
Well, first because there's no need to cast, and second because letters in the basic character set are not required to be consecutive. So your code could (and would!) fail mysteriously if run on a system that uses, say, EBCDIC.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC