C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-28-2009, 06:59 PM   #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!
to4st3r is offline   Reply With Quote
Old 10-28-2009, 08:12 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
What specifically are you having problems with on these? We're not here to just fill in the blanks and do your homework.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 10-28-2009, 08:25 PM   #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
to4st3r is offline   Reply With Quote
Old 10-28-2009, 08:26 PM   #4
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
So why don't you download a compiler and get them working?


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 10-28-2009, 08:30 PM   #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
to4st3r is offline   Reply With Quote
Old 10-28-2009, 08:53 PM   #6
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
Cygwin works fine for me. The forum has many threads on compilers. One's even stickied IIRC.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 10-28-2009, 09:00 PM   #7
Registered User
 
Join Date: Oct 2009
Posts: 4
Okay thank you hopefully I can get one working I need more practice with code.
to4st3r is offline   Reply With Quote
Reply

Tags
algorithm, arrays, loops, stings

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:02 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22