I'm not sure how to do this with arrays. Can someone please explain arrays and pointers to me and maybe figure this out?
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 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]
}
Also there's this one...Which compiles fine but it always says that it does not contain hydroxide, so I'm not sure where the error is here.
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 0;
}
Lastly I've been working on this program for awhile now and it has not been able to compile. Please help me with it if you can. (ignore the parts about the phonebook the example here is supposed to be for an array of integers)
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);
{ printf("The correct number was found.");
; // found the correct number
break; // break out of the while loop
}
else if (found < true);
{
top = middle + 1; // throw out mid and the left half of the list
}
else (found > true);
{
bottom = middle - 1; // throw out mid and the right half of the list
}
else
{printf("Number was not the midpoint.");
}
return 0;
}
I need all the help I can get! Thanks for your time!