I declared and initialized an array in main method of C program and defined a method to print the array elements, but it prints some garbage values instead of the actual value.
I tried the following code.
#include <stdio.h>
void arraySize(int arr[]);
int main() {
int array[] = {15, 50, 34, 20, 10, 79, 100};
arraySize(array);
return 0;
}
void arraySize(int arr[]) {
int i = 0;
while(arr[i]) {
printf("The value of arr[%d] is %d \n", i, arr[i]);
i++;
}
}
I'm getting the following output
The value of arr[1] is 50
The value of arr[2] is 34
The value of arr[3] is 20
The value of arr[4] is 10
The value of arr[5] is 79
The value of arr[6] is 100
The value of arr[7] is 3538944
The value of arr[8] is 28
The value of arr[9] is 6422300
The value of arr[10] is 3538944
The value of arr[11] is 6422368
The value of arr[12] is 4198966
The value of arr[13] is 1
The value of arr[14] is 8263032
The value of arr[15] is 8266360
The value of arr[16] is 2
The value of arr[17] is 0
Kindly assist me how to print the array values using while loop and terminate the loop once end of record reached.