Code:
//Attempt at making a faster dictionary program using binary search
#include <stdio.h>
struct entry
{
char word[100];
char definition[200];
};
int compareStrings (const char s1[], const char s2[])
{
int i = 0, answer;
while (s1[i] == s2[i] && s1[i] != '\0'&& s2[i] != '\0')
++i;
if (s1[i] < s2[i])
answer = -1;
else if (s1[i] == s2[i])
answer = 0;
else
answer = 1;
return answer;
}
int lookup (const struct entry dictionary[], const char search[], const int entries)
{
int low = 0;
int high = entries - 1;
int mid, result;
int compareStrings (const char s1[], const char s2[]);
while (low <= high)
{
mid = (low+high) / 2;
result = compareStrings (dictionary[mid].word, search);
if (result == -1)
low = mid + 1;
else if (result == 1)
high = mid - 1;
else
return mid;
}
return -1;
}
int main (void)
{
const struct entry dictionary[200] =
{ { "absolute zero", "The zero point on the Kelvin temperature scale, equivalent to -273°C; all molecular motion theoretically stops at this temperature."},
{ "accepted value", "A quantity used by general agreement of the scientific community."},
{ "accuracy", "The closeness of a measurement to the true value of what is being measured."},
{ "acid", "A compound containing hydrogen that ionizes to yield hydrogen ions (H+) in water."},
{ "acid dissociation constant", "(Ka) The ratio of the concentration of the dissociated form of an acid to the undissociated form; stronger acids have large Ka values than weaker acids."},
{ "acidic solution", "Any solution in which the hydrogen-ion concentration is greater than the hydroxide-ion concentration."},
{ "activated complex", "An unstable arrangement of atoms that exists momentarily at the peak of the activation energy barrier; it represents an intermediate or transitional structure formed during the course of a reaction."},
{ "activation energy", "The minimun energy colliding particles must have in order to react."},
{ "activity series of metals", "A table listing metals in order of decreasing activity."},
{ "actual yield", "The amount of product that forms when a reaction is carried out in the laboratory."},
{ "addition reaction", "A reaction in which a substance is added at the double bond of an alkene or at the triple bond of an alkyne."},
{ "alcohol", "An organic compound having an -OH (hydroxyl) group; the general structure R-OH."},
{ "aldehyde", "An organic compound in which the carbon of the carbonyl group is joined to at least one hydrogen; the general formula is RCHO."},
{ "aliphatic compound", "A hydrocarbon compound that does not contain a ring structure."},
{ "alkali metal", "Any metal in group 1A of the periodic table."},
{ "alkali earth metal", "Any metal in group 2A of the periodic table."},
{ "alkaline solution", "A basic solution."},
{ "alkane", "A hydrocarbon containing only single covalent bonds; alkanes are saturated hydrocarbons."},
{ "alkene", "A hydrocarbon containing one or more carbon-carbon double bonds; alkenes are unsaturated hydrocarbons."},
{ "alkyl group", "A hydrocarbon substituent; methyl (CH3) is an alkyl group."},
{ "alkyl halide", "A halocarbon in which one or more halogen atoms are attached to the carbon atoms of an aliphatic chain."},
{ "alkyne", "A hydrocarbon containing a carbon-carbon triple bond; alkynes are unsaturated hydrocarbons."},
{ "allotrope", "A molecular form of an element that exists in two or more different forms in the same physical state; oxygen, O2, and ozone O3 are allotropes of the element oxygen."},
{ "alpha particle", "A positively charged particle emitted from certain charged nuclei; it consists of two protons and two neutrons and is identical to the nucleus of a helium atom."},
{ "alpha radiation", "Alpha particles emitted from the radioactive source."},
{ "amino acid", "An organic compound having amino (-NH2) and carboxylic acid (-COOH) groups in the same molecule; proteins are made of the twenty naturally occurring amino acids."},
{ "amphoteric", "A substance that can act both as an acid and a base; water is amphoteric."},
{ "amplitude", "The height of a wave from the origin to the crest."},
{ "amorphous solid", "A term used to describe a solid that lacks an ordered internal structure; denotes a random arrangement of atoms."},
{ "analytical chemistry", "The study of the composition of substances."},
{ "anion", "Any atom or group or group of atoms with a negative charge."},
{ "anode", "The electrode at which oxidation occurs."},
{ "antibonding orbital", "A molecular orbital whose energy is higher than that of the atomic orbitals from which it is formed."},
{ "aquous solution", "A solution in which the solvent is water."},
{ "arene", "Any member of a special group of unsaturated cyclic hydrocarbons."},
{ "aromatic compound", "A name originally given to the arenes because many of them have pleasant odors."},
{ "aryl halide", "A halocarbon in which one or more halogens are attached to the carbon atoms of an arene ring."},
{ "asymmetric carbon", "A carbon atom that has four different groups attached."},
{ "atom", "The smallest particle of an element that retains the properties of that element."},
{ "atomic emission spectrum", "The pattern of frequencies obtained by passing light emitted by atoms of an element in the gaseous state through a prism; the emission spectrum of each element is unique to that element."},
{ "atomic mass", "The weighted average of the masses of the isotopes of an element."},
{ "atomic mass unit", "A unit of mass equal to one-twelth the mass of a carbon-12 atom."},
{ "atomic number", "The number of protons in the nucleus of an atom of an element."},
{ "atomic orbital", "A region in space around the nucleus of an atom where there is a high probability of finding an electron."},
{ "atomic radius", "One-half the distance between the nuclei in a molecule consisting of identical atoms."},
{ "atmospheric pressure", "The pressure exerted by air molecules in the atmosphere sorrounding the earth, resulting from collisions of air molecules with objects."},
{ "Aufbau principle", "Electrons enter orbitals of lower energy first."},
{ "Avogadro's hypothesis", "Equal volumes of gases at the same temperature and pressure contain equal numbers."} };
int entries = 50;
char word[100];
int entry;
int lookup (const struct entry dictionary[], const char search[], const int entries);
printf ("Enter desired term: ");
scanf ("%s", word);
entry = lookup (dictionary, word, entries);
if (entry != -1)
printf ("%s\n", dictionary[entry].definition);
else
printf("Sorry, term could not be found\n");
getchar();
getchar();
return 0;
}