Thread: Need help writing code! My minds blanking out!

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    Unhappy Need help writing code! My minds blanking out!

    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!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What specifically are you having problems with on these? We're not here to just fill in the blanks and do your homework.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    I am having a problem on 2 with figuring how to have the answer right when it gets compiled. The third one has compiling problems, but I do not know where the error is or what it is since the lab is now closed. I should taken notes of what they were....
    Sorry

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So why don't you download a compiler and get them working?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    I've tried to use different ones, like Cygwin but they haven't worked. Is there a good C one u can send me a link of?

    Thanks Again

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Cygwin works fine for me. The forum has many threads on compilers. One's even stickied IIRC.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    Okay thank you hopefully I can get one working I need more practice with code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 12:27 AM
  2. Writing code for a program in C
    By Sure in forum C Programming
    Replies: 7
    Last Post: 06-11-2005, 01:33 PM
  3. professional programmers, do you spend more time writing or modifying code
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 11-25-2002, 10:54 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread