Jump to content

A Little Challenge In C


Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Jason Cooper
Link to comment
Share on other sites

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 by silentknight329
Link to comment
Share on other sites

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 (^).

Link to comment
Share on other sites

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.

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...