Reputation: 31
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]); } }
Upvotes: 3
Views: 44046
Reputation: 64
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
}
Upvotes: 0
Reputation: 11
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.
}
}
Upvotes: 0
Reputation: 962
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
Upvotes: 1
Reputation: 22252
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).
Upvotes: 2
Reputation: 22379
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.
Upvotes: 3
Reputation: 7959
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 */ }
}
Upvotes: 9