Question

Please help with this question (the two functions). I've attempted it but it's not working. We...

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 <stdio.h>
#define MAX 20

int largest(int * x);
void display(int *arr);

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("\nLargest value: %d\n", 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\n", *(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);
  
}
}
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code in C:

#include <stdio.h>
#define MAX 20
int largest(int * x);
void display(int *arr);
int main()
{
   int array[MAX], count;
   int i;
   //take MAX number of inputs from user
   for(i=0;i<MAX;i++)
   {
       scanf("%d",(array+i));
   }
   //display() function calling
   printf("Elements in the array are: ");
   display(array);
   //display largest() function calling and print the value
   printf("\nLargest value: %d\n", largest(array));
   return 0;
}
void display(int *arr)
{
   int i;
   //*(arr+i) gives the value of arr at ith index
   for(i=0;i<MAX;i++)
       printf("%d ",*(arr+i));
}
int largest(int *arr)
{
   int max_value=*arr,i;
   //Iterate entire array
   for(i=1;i<MAX;i++)
   {
       //If value at ith index is greate than max_value then update the max_value
       if(*(arr+i)>max_value)
           max_value=*(arr+i);
   }
   return max_value;
}

output screenshot:

Add a comment
Know the answer?
Add Answer to:
Please help with this question (the two functions). I've attempted it but it's not working. We...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT