Question
I'm getting the wrong output...I tried int len = sizeof(arr)/sizeof(int) but that doesn't work either. Please help. THE INPUT IS (in this row by row
I'm getting the wrong output...I tried int len = sizeof(arr)/sizeof(int) but that doesn't work either. Please help.
THE INPUT IS (in this row by row format with the first entry in row #3 missing):
7 5 3 6 9 18 33 44 5 12 9 34 534 128 78
THE OUTPUT SHOULD BE:
Inputs: 7 5 3 6 9 18 33 44 5 12 9 0 34 534 128 78 Largest value: 534
--------------------------------------------
MY CODE:
#include
#define MAX 20
int largest(int *arr);
void display(int *arr);
int main(int argc, char *argv[])
{
int array[MAX], count;
/* Input MAX values from the keyboard. */
int i; count=0;
while ( scanf("%d", &i) != EOF){
*(array + count) = i; // store in array[count]
count++;
}
/* Call the function and display the return value. */
printf("Inputs: ");
display(array);
printf(" Largest value: %d ", largest(array));
return 0;
}
/* display a int array */
void display(int *arr)
{
int len = sizeof(arr);
for(int i = 0; i <= len; i++ ) {
printf("%d ", arr[i]);
}
}
/* Function largest() returns the largest value */
/* in an integer array */
int largest(int * arr)
{
int max = *arr; // c[0]
int len = sizeof(arr);
int i=0;
for(int i = 0; i <= len; i++ ){
if (*arr > max) // arr+4
max = *arr;
arr++; // arr jumps 4, pointing to next element
}
return max;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started