Question
Please help with this question (the two functions). I've attempted it but it's not working. We have to complete the 2 functions int largest(int *
Please help with this question (the two functions). I've attempted it but it's not working.
We have to complete the 2 functions int largest(int * x); and void display(int *arr); (one prints the values in array and the other gets max value in array).
But we can only use pointer indirection and address arithmetic to access and traverse the array. No array index [] should be used in the functions.
C PROGRAMMING:
-----------------
/* Passing an array to a function. */
#include
int main() { 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 s = 0; while(*arr != '\0') { s++; /* increase the address until the end */ } printf("%d", s); // int length = arr s; /* Subtract the two addresses, end - start */ // int i = 0; // for(i = 0; i < length; i++){ // printf("%d ", *(arr + i)); }
/* Function largest() returns the largest value */ /* in an integer array */
int largest(int *arr) { int *maximum = arr; int c = 0; for (c = 0; c < (sizeof(arr)*2)-1; c++){ if (*(arr+c) > *maximum) *maximum = *(arr+c); } }
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