This program compiles, but it does not give the right answer when there is an OH, can you spot anything wrong?
Code:
/*Purpose: The purpose of this program is to write a function called hydroxide and return a 1 for true if its arguments end in the substring of OH.*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define FALSE 0
#define TRUE 1
/*Function: isHydroxide
Purpose: determine if input string ends in oh or OH
Arguments: none
Returns: TRUE or FALSE */
int isHydroxide(char* str, int size);
int isHydroxide(char* str, int size)
{
//checks that it ends in OH or oh
if(str[size] == 0x4F || str[size] == 0x6F)
if(str[size - 2] == 0x48 || str[size - 1] == 0x68)
return TRUE; //if it contains OH
return FALSE; //if it does not contain OH
}
/*Function: main
Purpose: determine if compound ends in oh or OH
Arguments: none
Returns: string, 0 upon completion*/
int main (int argc, char **argv)
{
char *input = (char *)malloc(512); //allocate memory for string
int i = 0;
printf("\nEnter a chemical compound: ");
scanf("%s", input); //get string input
//Finds the length of the array minus 1
while(input[i] != '\0')
i ++;
if(isHydroxide (input, i))
printf("\n The compound %s is a hydroxide\n\n", input);
else
printf("\n The compound %s is not a hydroxide\n\n", input);
return TRUE;
}
Than there is this one here that compiles but it will not return any numbers or how many indexes it had to run.
Code:
/*Purpose: The purpose of this program is to construct and algorithm to find a name in the phone book. It needs to locate the name of the middle in the book. If the name is not at the middle it needs to determine if it is before or after it and find the middle of that section. It repeats until the name is found. This algorithm has to test a function called binary_srch and needs to inplement the algorithm for an array of integers.*/
#include <stdio.h>
#define MAX_ITEM 10
/*Function: main
Purpose: to find a name by going to the midpoint, determining if that is the name or if it is before or after that midpoint and repeating until the name is found.
Arguments: none
Returns: return 0 upon completion, double*/
/*found = false
bottom = last name
top = first name
middle = middle of first and last name in phonebook*/
int main (void)
{ int i;
double x[MAX_ITEM]; //max number of inputs
double top;
double middle;
double bottom;
double found;
double true;
double y;
for (i = 0; i < MAX_ITEM; i++);
{
printf("Enter %d numbers seperated by blanks\n>", MAX_ITEM);
scanf("%lf", &x[i]); //make array with inputs
printf("Enter number you would like to look up in the array.");
scanf("%lf", &y);
}
while ( bottom >= top ) // begin while loop
{true = y;
middle = (bottom + top)/2; // find the mid-section
if (found < true)
{
top = middle + 1; // throw out mid and the left half of the list
}
else if (found > true)
{
bottom = middle - 1; // throw out mid and the right half of the list
}
else if (found == true)
{
printf("The correct number was found.");
// found the correct number
break; // break out of the while loop
}
else if (found != true)
{
printf("Number was not the midpoint.");
}
}return 0;
}
Lastly I have no clue how to do this problem because it seems so general. This is all I have, I do not know where to go or if I'm even on the right track.
Code:
/*Purpose: The purpose of this program is to take two numerical lists of the same length ending in a sentinel value and store the lists in two arrays named x and y. Each of these arrays will have 20 elements. The products of the two elements x and y will be stored in z. Then the arrays will be displayed in a table with three columns. After this display the square root of the sum of the products in z.*/
#include <stdio.h>
#include <math.h>
#define MAX_ITEM 20 //set max number of elements to 20
int main (void)
{
double x[MAX_ITEM], //data list
double y[
double sum, //sum of data
double sum_sqr, //sum of the sums square root
int i;
//get the data
printf("Enter %d numbers seperated by blanks\n> ", MAX_ITEM); //get string of numbers
for (i = 0; i M < MAX_ITEM; i++)
scanf
{
sum += x[i];
sum_sqr += x[i] * x[i]
}
//displays the difference
Any help or suggestions are greatly appreciated! Thanks for your time!