/*-*-mode:c-*- *************************************************************************** CSN381: Project 1 (2:a) http://homepage.mac.com/hteric/Project1.pdf *************************************************************************** File : sumofodd.c Author : Jeremy Russell Date : September 5, 2002 Description : 2. You may use the result from the above question (or not) (b) Write a function called g which takes an integer n as an argument and returns an integer which satisfies the following conditions: a. if the argument n is odd return 3*n+1 b. if the argument n is even return n/2 Put the function g in c source file called "test.c", add a main to print out g(n) for all n from 1 to 100. Submit source and hard copy. ***************************************************************************/ /* Inlcude section. */ #include <stdio.h> /*************************************************************************** Function : main Author : Jeremy Russell Date : September 5, 2002 Description : Main function of the sumofodd program. Prints the sum of all odd numbers bewteen 1 and 1000, inclusive. Arguments : void Returns : void Notes : None See Also : None ***************************************************************************/ int main ( void ) { /* Vatiable declaration. */ int iterate; long int sum = 0; /* Main body. */ for ( iterate = 1; iterate <= 1000; iterate++) { if ( isodd( iterate ) ) sum = iterate + sum; } printf ("The sum of all odd numbers between 1 and 1000, inclusive, is: %ld\n", sum); } /*************************************************************************** Function : isodd Author : Jeremy Russell Date : September 5, 2002 Description : Evaluate an integer to seeif it is odd. Arguments : * int number Returns : true or false (0 || 1) Notes : None See Also : None ***************************************************************************/ int isodd ( int number ) { return number % 2; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 2117 | Jeremy Russell |
Project 1 nsplit.c has problesm still. |
||
#1 | 2115 | Jeremy Russell | Project 1, 2a |