Alex Alex - 2 months ago 23
C Question

How to normalize data and clear array

Hi i've written a small program that let's me choose from a menu what i want to do ie view, enter values, compute etc. its doing most of the stuff im asking it, so im on the right track. However, there are a few things i dont know how to fix.


  1. when i enter values and exit. When i re-enter it starts from 0 again, as if everything cleared. I want to start off where i left off. Secondly. i want to have MAX 10 values as input. I should not be able to keep entering. I want to erase the array using my function instead.

  2. im trying to compute normalized values but program quits everytime.






#include <stdio.h>
#define LENGTH 10

int enterMeasurements();
void printArray();
int minValue();
int maxValue();
float avgValue();
void normValue();

int main(void)
{
char input;
int nrMeasurements=0;
int arrayTable[LENGTH] = {0};
int normTable[LENGTH] = {0};
int max, min, norm;
float avg;
//main menu
do
{

printf("\nMeasurement tool 1.0\n");
printf("V for (View)\n");
printf("E for (Enter Values)\n");
printf("C for (Compute Values)\n");
printf("R for (Reset Values)\n");
printf("Q for (Quit)\n");
printf("\nEnter input: ");
scanf(" %c", &input);

if(input == 'v' || input == 'V')
{
// function to print array values
printArray(arrayTable, nrMeasurements);
}
else if(input == 'e' || input == 'E')
{
// enter values here
nrMeasurements = enterMeasurements(arrayTable,nrMeasurements);
}
else if (input == 'c' || input == 'C')
{
// enter function to calc min, max and avg and prints it.
max = maxValue(arrayTable, nrMeasurements);
min = minValue(arrayTable, nrMeasurements);
avg = avgValue(arrayTable, nrMeasurements);
printf("Maximum value is: %d\n", max);
printf("Minimum value is: %d\n", min);
printf("Average value is: %.2f\n", avg);
normValue(normTable, nrMeasurements);


}
else if (input == 'r' || input == 'R')
{
// enter function that erase the entire array.
printf("[ erase array ]\n");
}
}
while (input !='q' && input != 'Q');
return 0;
}

int enterMeasurements(int arrayTable[], int nrMeasurements)
{
int i;

for (i = 0; i < LENGTH; i++)
{
printf("Enter Measurement #%i (or 0): ", i+1);
scanf("%d", &arrayTable[i]);


if (arrayTable[i] == 0 )
break;
}
return i;
}

void printArray(int arrayTable[], int nrMeasurements)
{
int i;

for (i = 0; i < nrMeasurements; i++)
{
printf(" array[%2d] : %d\n", i, arrayTable[i]);
}
}

int maxValue(int arrayTable[], int nrMeasurements)
{
int i, max;
max = arrayTable[0];
for (i=1; i<nrMeasurements; i++)
{
if (arrayTable[i]>max)
{
max=arrayTable[i];
}
}
return(max);
}

int minValue(int arrayTable[], int nrMeasurements)
{
int i, min;
min = arrayTable[0];
for (i=1; i<nrMeasurements; i++)
{
if (arrayTable[i]<min)
{
min=arrayTable[i];
}
}
return(min);
}

float avgValue(int arrayTable[], int nrMeasurements)
{
int sum,i;
float avg;
sum=0;
avg=0;

for (i=0; i<nrMeasurements;i++)
{
sum=sum+arrayTable[i];
avg=(float)sum/(i+1);
}
return(avg);
}

void normValue(int min, int max, int avg, int arrayTable[], int nrMeasurements, int normTable[])
{
int i;

for (i=0; i<nrMeasurements; i++)
{
normTable[i]=(arrayTable[i]-min)/ (max-min);
}
for (i=0; i<10; i++)
printf("Normalized Values: %d\n", normTable[i]);
}

Answer

when i enter values and exit. When i re-enter it starts from 0 again, as if everything cleared.

That happens since you are always starting from i = 0; in enterMeasurements. You need to initialize it to the arugment nrMeasurements.

im trying to compute normalized values but program quits everytime.

That's happening most likely because you are not passing enough arguments to normValue.

Change the call to:

normValue(min, max, avg, arrayTable, nrMeasurements, normTable);

You should update the function declarations to contain all the arguments. This would have avoided the above error.

int enterMeasurements(int arrayTable[], int nrMeasurements);
void printArray(int arrayTable[], int nrMeasurements);
int minValue(int arrayTable[], int nrMeasurements);
int maxValue(int arrayTable[], int nrMeasurements);
float avgValue(int arrayTable[], int nrMeasurements);
void normValue(int min, int max, int avg, int arrayTable[], int nrMeasurements, int normTable[]);
Comments