So the problem that i have with my code is the following:
1. when i run the program and enter view (v) the array is suppose to be empty but instead i get this [21696 1 ]. after i reset the array using my reset function (r) the array is empty. i don't understand why my array has numbers in it whenever it is suppose to be empty.
2. what i'm trying to accomplish with this program is being able to insert numbers in an array that can hold 10 positiv integers. so when the program has its 10 numbers it should not enter more numbers or if it has less than 10 numbers then it should start from where it left off
ex: if we want to enter 5 digits in our array and then move on to perhaps view the elements in the array then we should be able to see our 5 elements. but if we then choose to continue adding elements i want my program to start from position nr 6 and not from 0.
i hope i was clear in my explanation please do ask if you need further information
Code:
#include <stdio.h>
#define length 11
void insert (int a[],int size);
void print (int a[]);
void reset (int a[]);
void compute (int a[], int size);
int main (void)
{
printf("Measurment tool 1.2\n");
for (;;)
{
int a[length];
int size = sizeof(a)/sizeof (a[0]);
char kod;
printf("VECRQ? ");
scanf(" %c", &kod);
switch (kod)
{
case 'e': insert(a,size); break;
case 'v': print(a); break;
case 'c': compute(a, size); break;
case 'r': reset(a); break;
case 'q': return 0; break;
default:
break;
}
}
}
void insert (int a[],int size)
{
int i;
for (i=0; i<size; i++)
{
printf("Enter measurement #%d (or 0)? ", i);
scanf("%d", &a[i]);
if (a[i]==0)
{
break;
}
}
}
void print (int a[])
{
int i;
printf("[");
if (a[i]>0){
for (i=0; a[i] != 0; i++)
{
printf("%d ", a[i]);
}
}
else printf("NO MEASURMENTS");
printf("]\n");
}
void reset (int a[])
{
int i;
for (i=0; i<a[i]; i++)
{
a[i]=a[i+length];
}
}
void compute (int a[], int size)
{
int i, max = 0, min=0, sum;
float medel = 0.0;
min=a[0];
for (i=0; i<a[i]; i++) {
if (max<a[i])
{
max=a[i];
}
}
for (i=0; i<a[i]; i++)
{
if (min>a[i]) {
min=a[i];
}
}
for (i=0; i<a[i]; i++)
{
sum+=a[i];
}
medel=sum/sizeof(a[i]); //medel means average
printf("largest number in the array is %d\n", max);
printf("Smallest number in the array is %d\n", min);
printf("average number in the array is %.2f\n", medel); //medel means average
}