Ask Experts Questions for FREE Help !
Ask
    johnny1667's Avatar
    johnny1667 Posts: 3, Reputation: 1
    New Member
     
    #1

    Oct 29, 2008, 07:52 PM
    C programming
    Write and test a function hydroxide that takes a chemical formula as a parameter and returns True if the chemical is a hydroxide (i.e. ends in OH) and returns False otherwise.


    Example:
    Enter a chemical formula: NaOH
    NaOH is a hydroxide.

    Enter a chemical formula: H2O
    H2O is not a hydroxide.
    slapshot_oi's Avatar
    slapshot_oi Posts: 1,537, Reputation: 589
    Ultra Member
     
    #2

    Oct 29, 2008, 09:16 PM
    I'll probably get flack for doing your homework, but I want to see if I can still remember C.

    [CODE=C]
    /*
    * Programmer: slapshot_oi
    * Date: October 30, 2008
    */

    #include <stdio.h>

    #define FALSE 0
    #define TRUE 1

    int isHydroxide(char* str, int size);

    int isHydroxide(char* str, int size){
    /*Checks if the second-to-last char is
    'o' or 'O', and if the last char is 'h' or 'H'*/
    if(str[size] == 0x4F || str[size] == 0x6F)
    if(str[size + 1] == 0x48 || str[size + 1] == 0x68)
    return TRUE;

    return FALSE;
    }

    int main (int argc, char **argv){
    char* input = NULL;
    int i = 0;

    printf("\n\n Enter a chemical compound: ");
    scanf("%s", input)

    /*Finds the length of the array minus 1*/
    while(input[i] != '\0')
    i ++;

    if(isHydroxide(input, i))
    printf("\n\n The compound %s is a hydroxide", input);
    else
    printf("\n\n The compound %s is not a hydroxide", input);

    return 0;
    }

    [/CODE]

    You'd better compile this to check this man, 'cause I didn't. I wrote this all from memory based on what I learned over two years ago.

    By the way, if you can't figure out a simple program like this without asking for help right off the bat, you're in for a tough semester.
    johnny1667's Avatar
    johnny1667 Posts: 3, Reputation: 1
    New Member
     
    #3

    Oct 29, 2008, 09:37 PM
    I've been out for a week but I get the stuff just can't make it compile. Well what you provided me didn't compile but helps me see some stuff, and I've got it to compile but a segment violation comes up.
    slapshot_oi's Avatar
    slapshot_oi Posts: 1,537, Reputation: 589
    Ultra Member
     
    #4

    Oct 30, 2008, 09:51 AM

    Compile it again with debug flags (-g option I believe) and then step through it with GDB. I guarantee there's something wrong with the way I used scanf() or the char*.
    xpl0itz's Avatar
    xpl0itz Posts: 1, Reputation: 1
    New Member
     
    #5

    Aug 5, 2009, 02:57 AM
    In order to fix the access violation error you will need to modify slapshot_io's code a little.

    Include some additional header files:
    [CODE=C]#include <stdlib.h>
    #include <malloc.h>[/CODE]

    Then change the following line:
    [CODE=C]char *input = NULL;[/CODE]
    to
    [CODE=C]char *input = (char *)malloc(1024); //Allocate a 1024 byte space for our input string[/CODE]

    This piece of code I changed is what caused the access violation error, it sets input to NULL, or 0. The scanf() call that follows then tries to read the input string into a buffer located at 0x0000000, but that memory is not allocated and you will get an access violation error. I changed this piece of code to allocate 1024 bytes of memory and then put the location of that piece of into input, instead of 0x0000000.

    There's also a second error in the code:
    [CODE=C] if(str[size] == 0x4F || str[size] == 0x6F)
    if(str[size + 1] == 0x48 || str[size + 1] == 0x68)
    return TRUE;[/CODE]

    Here it reads str[size] and str[size + 1] to get the second-to-last and last item, respectively. This is incorrect. It should be str[size - 2] and str[size - 1]. (remember, C arrays start indexing at zero. The highest index in an array of 30 items will be 29.)

    I made these changes, and then tested the program. It worked just fine.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

C programming [ 2 Answers ]

I'll be extremely thankful if u could provide me with a c program that convert a given nfa to dfa

Qt programming [ 1 Answers ]

Hi, My name is anshahmed.I am doing final year B.TECH.Now I am doing a project and I want to use qt programming .Whie I was compiling the qt programm the compiler giving errors in header file(qpplicaton.h) .how can I compile the qt programm.


View more questions Search