Thread: Some basic array or structure program

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    27

    Talking Some basic array or structure program

    Hi all, I wanted to create program where it would accept alphabets from A-Z and after that, the program will print the next alphabet. EX. C will print out D and Z will back to A again. I am not good in using array or structure so can anyone point out the important functions I should use in the program?

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I suggest first visit www.cprogramming.com.
    You will find a pretty good tutorial.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    Can you point out some examples for me I think am more optimisied in learning by example thanks

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    this will get you a bit closer to your examples. http://www.cprogramming.com/tutorial.html
    When no one helps you out. Call google();

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    man go to www.freeprogrammingresources.com that in there you find various tutorials of C and diverses languages of programmation, you find inclusiv www.cprogramming.com hehehe
    good luck.

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by InvariantLoop
    this will get you a bit closer to your examples. http://www.cprogramming.com/tutorial.html
    What a precision!

    Cheers

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    he asked for examples, there are plenty there.
    When no one helps you out. Call google();

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    Thank you for all your help but should I be using arrays or structure for this simple program?

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    For such a simple program, you could probably just do this with some simple addition (+) and the modulus operator (%) in addition to whatever input/output routines and looping mechanisms you wished to use. Other than that, no special functions or data structures, i.e. arrays or structs, should be absolutely required to complete the program. I suppose you could incorporate something if you really wanted to.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can use addition to get the next letter in the alphabet (e.g. putchar(input_character + 1)). You'll just need to make a special case for 'Z'. You didn't mention what to do if the input character is 'Z', but you can test for it and then output whatever you need to if the condition is true.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    Ok I try my program with the source code as followed, but the compiler stated that Lvalue is required what is the error within my source code?

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    #include<stdlib.h>
    #include<string.h>
    
    char ch[26][1];
    
    void ChangeToNext();
    
    main(){
    clrscr();
    fflush(stdin);
    ChangeToNext();
    getch();
    return 0;
    }
    
    void ChangeToNext(){
    char answer[1];
    int i=0;
    char ch[26][1]={"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"};
    printf("Please enter any alphabet number:");
    scanf("%c",&answer);
    for (i=0;i<26;i++)
    {
    if (answer==ch[i])
    answer = ch[i+1];
    prinf("The answer:", answer);
    getch();
    }
    }

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, aside from the horrific number of non-portable function calls, there are:
    - You define answer as a 1-element array. This is almost always wrong and amazingly enough that's the case in this situation. Just make it a char instead of an array of 1 char.
    - You define ch as an array of 1-element characters. And then you initialize it with 26 2-character strings. Don't forget that strings end with a \0 character. It looks like you actually want to define it as a 26-character array and initialize it with characters instead of strings.

    Also, you didn't pay attention to my previous post at all. This is all way too much work for what you're trying to accomplish. You also don't pay any attention to 'Z' like I suggested. You just want to use an out-of-bounds array element? Bad idea.

    Also, I don't know if it's poor copying-and-pasting or what, but your (lack of) indentation style could use some serious help.

    Personally, I would throw that whole program away, burn it, and start over.

    When designing an algorithm think of how you would perform the task in real life. The answer is right in the question most of the time. "Get an alphabetic character as input and display the character that comes after it alphabetically." The steps are that simple. Get the input character (you do that), then display the next character (input_character + 1). Then all you have to do is figure out what you want to do if the input character is 'Z'. It's the same thing you'd do in real life with the same task.
    Last edited by itsme86; 03-10-2005 at 10:19 AM.
    If you understand what you're doing, you're not learning anything.

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    how does the putchar function that you have mentioned works? Does it add the value of the char or what? Besides the array I cant think of any logic that can place all the alphabets together. How will the program knows to display C if you enter B and so on?
    Last edited by kimimaro; 03-10-2005 at 10:17 AM.

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    In ASCII, all the characters are all thrown together in alphabetic order, so the work is already done for you.

    Here's the man page entry for putchar(). It should explain all you need to know about it: http://www.hmug.org/man/3/putc.html

    Man(ual) pages are an excellent resource for standard functions. You can just do a google search for the function name and include man as one of the search words.
    If you understand what you're doing, you're not learning anything.

  15. #15
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    thanks for your help and I need one more help is that how do you make the program to detect the input is between A to Y ? They are not integers so how do you create a condition by using If and the value must between A and Y

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Strange structure array issue...
    By IndioDoido in forum C Programming
    Replies: 9
    Last Post: 03-23-2008, 06:29 AM
  3. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  4. Need some help with a C program...
    By pityocamptes in forum C Programming
    Replies: 1
    Last Post: 03-25-2005, 06:43 PM
  5. My Program
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 02:41 PM