silentknight329 Posted January 18, 2012 Share Posted January 18, 2012 Hey guys, I'm working on a challenge that our professor gave us today, its not worth marks or anything but just something to do on our spare time. We only know the very basics of "c" as its early in the term. we know things like scanf, printf, if, char, int, etc... Here's the challenge: write a program which will ask the user to input three letters. Determine which letter comes first in the alphabet and display it on the screen. (hint: A equals 97 in the ASCII table). I'm basically having trouble getting started.... here is the code i have: /* Filename: atoz.c Written By: Silentknight329 Date: Jan 18, 2012 */ #include <stdio.h> #include <stdlib.h> int main() { char let1; char let2; char let3; printf("Input three letters:"); scanf("%c, %c, %c",&let1, &let2, &let3); system("PAUSE"); return 0; } If you guys wouldnt mind giving me some pointers or even links to really good tutorials would be rad Thanks Silentknight329 Quote Link to comment Share on other sites More sharing options...
Mr-Protocol Posted January 18, 2012 Share Posted January 18, 2012 The farthest you have gotten is to input the characters? Write out some pseudo code. That should get you thinking in the right direction. Quote Link to comment Share on other sites More sharing options...
digip Posted January 19, 2012 Share Posted January 19, 2012 I'm not a programmer, but I would create an index of the letters and assign a value. a=1, b=2, etc, maybe even use some sort of array to count them, then check the three letters, get their number assignments, if item 1 number is less then 2 and 3, it comes first, and so on, doing compares, finding the smallest number assigned to them, and then that letter would be the one to echo back as the answer if its number was the smallest or "less than" the other values. Quote Link to comment Share on other sites More sharing options...
Mr-Protocol Posted January 19, 2012 Share Posted January 19, 2012 Convert to ascii and rank by number of ascii. Much easier than an array of every character. Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted January 19, 2012 Share Posted January 19, 2012 (edited) As it is C then this works, just remember to input your letters in the format "b, a, c" #include <stdio.h> #include <stdlib.h> int main() { char let1; char let2; char let3; printf("Input three letters:"); scanf("%c, %c, %c",&let1, &let2, &let3); if(let1<=let2 && let1<=let3) { printf("%c is the lowest\n",let1); } else if(let2<=let3) { printf("%c is the lowest\n",let2); } else { printf("%c is the lowest\n",let3); } return 0; } Now that we have done your homework tell us why it works and then write the code to allow you to compare as many characters as you like. Edited January 19, 2012 by Jason Cooper Quote Link to comment Share on other sites More sharing options...
Mr-Protocol Posted January 19, 2012 Share Posted January 19, 2012 Simple, use sort. http://en.wikipedia.org/wiki/Sort_(C%2B%2B) Quote Link to comment Share on other sites More sharing options...
silentknight329 Posted January 19, 2012 Author Share Posted January 19, 2012 (edited) Thanks for the help guys, I am a really novice programmer, dont even know how to loop yet I'm not to sure what the else does, I've never seen it before, I'll google it right after, here's what I'm understanding the code to be: #include <stdio.h> #include <stdlib.h> int main() { char let1; char let2; char let3; printf("Input three letters:"); scanf("%c, %c, %c",&let1, &let2, &let3); if(let1<=let2 && let1<=let3) /* let1 is less than or equal to let2 and let3 print let1 */ { printf("%c is the lowest\n",let1); } else if(let2<=let3) /* not to sure what the "else" statement does, but im assuming it means if the statement above isnt true and if let2 is less than or equal to let3 print let2 lowest */ { printf("%c is the lowest\n",let2); } else /* if neither of the above statements are true print let3 lowest */ { printf("%c is the lowest\n",let3); } system("PAUSE"); return 0; } Also for the very first "if" do you have to use && like saying equals being ==? I'll work on editing that code for this next hour here to compare as many letters as possible Thanks for the help guys, appreciate it Edited January 19, 2012 by silentknight329 Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted January 19, 2012 Share Posted January 19, 2012 You are bang on with how the else works, have a play and you will soon get the hang of them. The && is a logical AND, so if both the left hand and right hand conditions are true it is true, if either is false then it is false. A single & is bitwise AND which is very different, e.g. if X has a value of 1 (01 in binary) and Y has a value of 3 (11 in binary) then X&Y would equal 1. The bitwise logic works on each bit in the values, so the first bits are compared from X and Y, which are 0 and 1. As they are not both 1 then the value is 0. The second bits are then compared (1 and 1) as they are both 1 the value of the second bit is 1. Things to read up on are Boolean logic, AND (&&), OR(||), NOT(!)), and Bitwise Operators, AND (&), OR(|), NOT(~), XOR (^). Quote Link to comment Share on other sites More sharing options...
r3b00tz Posted January 19, 2012 Share Posted January 19, 2012 Also, read up on how C treats characters. There's a comparison based solution that should immediately pop out to you once you read that! A couple posts touched on it already. Quote Link to comment Share on other sites More sharing options...
silentknight329 Posted January 20, 2012 Author Share Posted January 20, 2012 I'm still trying to figure out how to be able to compare as many characters as i want, just a little stuck, would using an array help me get started? Thanks Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted January 20, 2012 Share Posted January 20, 2012 Think loops rather than arrays. Start with asking the user how many they want to compare and then use a for loop to compare that many letters and finish with telling the user what the lowest value letter was. Quote Link to comment Share on other sites More sharing options...
silentknight329 Posted January 23, 2012 Author Share Posted January 23, 2012 Haven't learned anything about loops yet, I'll do some research into it and see what i can do. Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.