3

I'm trying to print all of the values in the four arrays by sending them to a separate function. But, the problem is I can't get the function to print the all of the integers in the array because I'm not sure what I could set the condition statement in the for loop to, that would be universal to any array of any size.

Right now the function only prints the first 11 numbers. I assume that's because the first number in that array is 11.

    #include <stdio.h>

    void print_array(int a[]);

    void find_max(int b[]);

    void find_min(int c[]);

    void search(int d[]);

    void SORT(int e[]);

    int main(void)
    {
        int first[11] = {7,7,7,7,7,7,7,7,7,7,7};

        int second[14] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2};

        int third[16] = {-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

        int fourth[23] = {-3, 4, 33, 22, 9, -100, 2, 56, 57, 55, 2, 90, 2234, 32, 8, 123, 2, 33, 22, 22, 33, -1, -3}; 


        print_array(&second[0]);


        return(0);
    }

    void print_array(int a[])
    {

        int i;
        for(i=0;i<*a;i++)
        {
            printf("%d ",a[i]);

        }
    }

6 Answers 6

9

Pass a second argument to your function that takes the length of the array. For example:

print_array(int *array, int length)
{
    for (int i = 0; i < length; i++) { /* whatever */ }
}
Sign up to request clarification or add additional context in comments.

Comments

3

The function has no way of knowing when the array ends. This piece of data simply does not exist unless you pass it manually. The array is just a sequence of bytes in the memory, it has no end delimiter. So you should add a parameter to the function telling it the length of the array.

Yep, this is how it works in C.

2 Comments

anyway i can assign the value in the brackets[] of the array to a variable?
@Mike Exactly, you should make your program aware of this value somehow, for example by assigning it to a variable.
2

Change the function to:

void print_array(int a[], size_t a_size) {
    int i;
    for(i=0; i< a_size;i++)
    // ...

And change the calling of the function to pass in the size:

    print_array(second, sizeof(second)/sizeof(second[0]));

Which will calculate the memory size of the array (for a 4 int array on a 32 bit system it'll be 16) and divide it by the size of an int (on a 32 bit system, it's 4 bytes).

Comments

1

in C you can make it with a function and macro:

void printArray_(int *a, int len) {
    for (int i = 0; i < len; i++) printf("%d ", a[i]);
}

#define printArray(arr) printArray_((arr), sizeof(arr)/sizeof(arr[0]))

int main(int argc, _TCHAR* argv[])
{   
    int data[] = { 1,2,3,4 };
    printArray(data);
    return 0;
}

output:

1 2 3 4

1 Comment

If you call this within a function that receives a pointer-to-char argument, it will fail, as the sizeof(arr) call will return sizeof(char *) instead, due to "array decay".
0

Change this line

print_array(&second[0]);

To

print_array(&second);

Because, &second[0] just passes the reference to the element at 0th position,which will not be able to traverse the array.

And we cannot traverse the array passed by reference without the size.As there are arrays of varied size, we can compute the size of the array by,

             int array_length = sizeof(array)/sizeof(array[0]);

Change the line

void print_array(int a[])

To

void print_array(int *a,int array_length)

And the function of array printing will be as,

void print_array(int *a,int array_length){
    int i;
    for(i=0;i<array_length;i++){
        printf("%d ",*a);
        a++;          //for incrementing the position of array.
    }
}

Comments

0

You need to modify ur print array

void print_array(int a[], int size) {
    int i;
    for(i = 0; i < size; i++) { // loop through each element in the array
        printf("%d ", a[i]); // print the value at the current index
    }
    printf("\n"); // add a newline at the end
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.