Thread: issues with arrays

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    20

    issues with arrays

    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
    
    
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    1. At line #63, the value of "i" is unknown. You need to initialize it before using it.

    2. If you want your array to be zero-terminated (like how strings are), you need to replicate similar functionality to the strlen() and strcat() functions, to find the current size and add an integer at the end respectively.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    20
    Quote Originally Posted by GReaper View Post
    1. At line #63, the value of "i" is unknown. You need to initialize it before using it.

    2. If you want your array to be zero-terminated (like how strings are), you need to replicate similar functionality to the strlen() and strcat() functions, to find the current size and add an integer at the end respectively.
    Thank you for your reply. the solved the first issue by initializing my array to zero a[i]=0
    but about the second point. i might have been unclear with my explanation but what i want my program to do is that i want it to store numbers and then if i want to enter more i can do so without it restarting from zero again.
    ex i enter 4 numbers [1 2 3 4]
    then i view them the result should be [1 2 3 4]
    if i want to compute then i should get the appropriate results
    but if i want to enter more numbers i should be able to enter the fifth digit without having to start over from zero again.

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    There is no such thing as an empty array. But since you are only interested in positive integers but you are storing them in ints, it is fine that you have represented empty with zero in your logic. But you forgot to initialize the array to zeros, which is necessary with this approach.

    However, although your way is an equally valid way of doing the job, I think I would find it easier to keep track of how many numbers I have, than to measure how full the array is. For example, I could store the number of elements in the zeroth slot of the array.
    0 ? ? ? ? ? ? ? ? ? ?
    1 100 ? ? ? ? ? ? ? ? ?
    2 1 2 ? ? ? ? ? ? ? ?
    10 1 2 3 4 5 6 7 8 9 10
    It doesn't matter what is in the ?s, because I would never read there, but the zeroth slot still needs to be init to 0 to indicate an empty array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 49
    Last Post: 06-09-2013, 10:09 AM
  2. Memory issues in C++ vectors compared to ordinary arrays
    By FortranLevelC++ in forum C++ Programming
    Replies: 9
    Last Post: 05-09-2013, 03:58 AM
  3. issues combining pointers\arrays\functions :(
    By jacka1993 in forum C Programming
    Replies: 7
    Last Post: 10-21-2012, 04:03 AM
  4. Completely new to programming: Having issues with arrays
    By x.sophie.x in forum C Programming
    Replies: 3
    Last Post: 09-06-2012, 01:37 PM
  5. Replies: 16
    Last Post: 01-01-2008, 04:07 PM

Tags for this Thread