Hello,
I was looking at the code and the first thing that I noticed as how long the main function is. Writing small meaningful function would save you a lot of time debugging and your code will be more readable. I did not change a lot in the code because I would like for you to understand and code it. Perhaps after this major refactoring is done the bug or the tricky logic you are having problems with will disappear.
So this is my approach:
Create a header file to out in there, constants, enumerators, function prototypes.
Divide the code into smaller functions. Ex each command should be a function.
So my header (called calculator.h) looks like this:
Code:
/* make sure all the items are defined*/
#ifndef CALCULATOR_H
#define CALCULATOR_H
#define FALSE 0 /*false has zero value */
#define TRUE 1 /*true has one value*/
#define MAX_MENU_OPTIONS 8 /* maximum options from menu */
#define MAX_NUMBERS = 100 /*maximum amount of numbers in array*/
/* function prototypes */
/* menu and header fucntions */
void printHeading();
void printMenu();
int getMenuOption();
/* program features*/
void getListOfNumbers(int list[]);
int getSmallest(int list[]);
int getLargest(int list[]);
double getAverage(int list[]);
int getStandardDev(int list[]);
void reverseList(int inList[], int outList[]);
void rotateToRight(int list[]);
void rotateToLeft(int list[]);
/* auxiliary and other */
void printList(int list []);
#endif
and the file for the mail program like this:
Code:
#include <stdio.h>
#include <math.h>
#include "calculator.h"
int main()
{
int done;
int choice;
float average, sum = 0, sum2 = 0, std_dev, var;
int n, c, d, temp, min, max;
int x[MAX_NUMBERS], b[MAX_NUMBERS];
printHeading();
//average = FALSE;
done = FALSE;
while(!done) {
printMenu();
choice = getMenuOption();
switch(choice) /*Command Dispatcher*/
{
case 0:
getListOfNumbers(x);
break;
case 1:
min = x[0];
for( c = 0; c < n; c++)
{
if( x[c] < min )
{
min = x[c];
}
}
printf("The smallest number is: %d\n", min);
break;
case 2:
max = x[0];
for( c = 0; c < n; c++)
{
if( x[c] > max )
{
max = x[c];
}
}
printf("The largest number is: %d\n", max);
break;
case 3:
for( c = 0; c < n; c++)
{
sum = sum + x[c];
}
average = sum / (float) n;
printf("The average is: %.2f\n", average);
//average = TRUE;
printf("Average has been flagged");
break;
case 4:
for( c = 0; c < n; c++)
{
sum2 = sum2 + pow((x[c] - average), 2);
}
var = sum2 / (float) n;
std_dev = sqrt(var);
printf("The standard deviation is: %.2f\n", std_dev);
break;
case 5:
for (c = n - 1, d = 0; c >= 0; c--, d++)
b[d] = x[c];
for (c = 0; c < n; c++)
x[c] = b[c];
printf("Array is now:\n");
for (c = 0; c < n; c++)
printf("x[%d] = %d\n", c, x[c]);
break;
case 6:
temp = x[n-1];
for( c = n-1; c > 0; c--)
{
x[c] = x[c-1];
}
x[0] = temp;
printf("Array is now:\n");
for( c = 0; c < n; c++)
printf("x[%d] = %d\n", c, x[c]);
break;
case 7:
temp = x[0];
for( c = 0; c < n - c; c++)
{
x[c] = x[c+1];
}
x[n-1] = temp;
for( c = 0; c < n; c++)
printf("x[%d] = %d\n", c, x[c]);
break;
case 8:
printf("GOODBYE!!\n");
done = TRUE;
break;
default:
printf("invalid program option %d selected\n", choice);
}
}
}
/*Prints the header of the project */
void printHeading()
{
char name[32] = "Antonio Lee Jr";
printf("\n");
printf("%s\n", name);
printf("CSC 130 Spring 2016\n");
printf("Programming Project 5\n");
printf("Arrays Project\n");
printf("Program to process a list of numbers\n");
}
/*Prints the header of the project */
void printMenu(){
printf("\nSelect the desired operation:\n");
printf("\t0 - Enter list of numbers\n");
printf("\t1 - Find the smallest number\n");
printf("\t2 - Find the largest number\n");
printf("\t3 - Find the average of the numbers\n");
printf("\t4 - Find the standard deviation of the numbers\n");
printf("\t5 - Reverse the list of numbers\n");
printf("\t6 - Rotate the list of numbers to the right\n");
printf("\t7 - Rotate the list of numbers to the left\n");
printf("\t8 - Exit the program\n");
}
/*gets an option from the menu */
int getMenuOption(){
int choice = 0;
do{
printf("%s", "Choice? ");
fflush(stdin); //flushes buffer
} while((scanf("%d", &choice) < 1));
return choice;
}
/*gets a list of numbers from the user */
void getListOfNumbers(int list[]){
int total = 0;
printf("How many numbers? ");
scanf("%d", &total);
int c;
for (c = 0; c < total; c++){
printf("x[%d] = ", c);
scanf("%d", &list[c]);
}
}
I hope this enough to get you started