Log in

View Full Version : C programming


johnny1667
Oct 29, 2008, 07:52 PM
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
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.



/*
* 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;
}



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

#include <stdlib.h>
#include <malloc.h>

Then change the following line:

char *input = NULL;
to

char *input = (char *)malloc(1024); //Allocate a 1024 byte space for our input string

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:

if(str[size] == 0x4F || str[size] == 0x6F)
if(str[size + 1] == 0x48 || str[size + 1] == 0x68)
return TRUE;

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.