/*********************************************
* File: proj4.c
* Author: Jonathan Rivera
* Date: 4/29/07
* Section: 102
* Email: jriv1@umbc.edu
*
* This program calculates the results of a
* hailstone sequence for integers and ranges
* of integers.
**********************************************/
void PrintGreeting (void);
char PrintMenu (void);
void DetermineOutput (char);
int HailstoneSequence (int);
#include <stdio.h>
#define MIN 1
#define MAX 9999
int main()
{
char choice; /* User's menu choice */
PrintGreeting();
choice = PrintMenu();
DetermineOutput(choice);
return 0;
}
void PrintGreeting (void)
{
printf("\nWelcome to Hailstone Sequences. This program\n");
printf("takes an integer and:\n");
printf(" a) cuts it in half if its odd or\n");
printf(" b) multiplies it by 3 and adds 1 if even\n");
printf("until the number reaches 1.\n\n");
}
char PrintMenu (void)
{
char choice;
char newline;
printf(" I - view sequence for an Individual value\n");
printf(" R - view sequences for a Range of values\n");
printf(" H - view the sequences and a Histogram of a range\n");
printf(" W - Write sequences and a histogram of a range to a file\n");
printf(" Q - Quit\n");
printf("Enter choice : ");
scanf("%c", &choice);
scanf("%c", &newline);
return choice;
}
void DetermineOutput (char)
{
int value; /* Single value for hailstone operation */
int firstRange; /* First number in the range of values */
int lastRange; /* Last number in the range of values */
while (choice != 'Q' || choice != 'q')
{
switch (choice)
{
case 'i':
printf("Please enter an integer between ");
printf("%d and %d : ", MIN, MAX);
scanf("%d", &value);
HailstoneSequence(value);
break;
case 'I':
printf("Please enter an integer between ");
printf("%d and %d : ", MIN, MAX);
scanf("%d", &value);
HailstoneSequence(value);
break;
case 'r':
printf("First number in the range :\n");
printf("Please enter an integer between ");
printf("%d and %d : ", MIN, MAX - 1);
break;
}
}
}
int HailstoneSequence (int)
{
int lengthOfSequence = 1;
printf("%5d :%8d", int, int);
while (int != 1)
{
if (int % 2 == 0)
{
int = int / 2;
}
elseif (int % 2 == 1)
{
int = (int * 3) + 1;
}
printf("%8d, int);
lengthOfSequence++;
if (lengthOfSequence % 9 == 0)
{
printf("\n ");
}
}
printf("\nlength of sequence = %d\n\n", lengthOfSequence);
choice = PrintMenu();
return lengthOfSequence;
}