Question: Problem E. Using terminator token. Subject Explore putting a special sentinel token at the end of array, like the case of string. Specification Write an
Problem E. Using terminator token.
Subject
Explore putting a special sentinel token at the end of array, like the case of string.
Specification
Write an C program that reads a list of positive integer values (including 0), until EOF is read in, and then outputs the largest value among in the input integers. Assume there are no more than 20 integers.
Implementation
Please follow the given template below to start with.
Keep on reading integers using scanf and a loop, and put the integers into an array, until EOF is read.
In earlier labs we have experienced how getchar detects end of file. We have used scanf to detect quit but not end of file. So far we have ignored the fact that scanf also has a return value, which is an integer indicating the number of characters read in, and, same as getchar, function scanf also returns EOF if end of file is reached. You can issue man 3 scanf | grep return or man 3 scanf | grep EOF in the terminal to see details.
Note that several input integers can appear on the same line. So far we have used scanf to read a line of input a time (which contains no spaces). Here you can observe that scanf with a loop can read inputs that appear on the same line, as well as on multiple lines.
In main, you should only use array index notation [] in declaring the array. For the rest of code in main, you should use pointer indirection and address arithmetic to access and update the array. No array index [] should be used.
Define a function void display(int *),which, given an integer array, prints the array elements. In this function, use pointer indirection and address arithmetic to access and traverse the array. No array index [] should be used in the function.
Define a function int largest(int *), which, given an integer array, returns the largest integer in the array. In this function, use pointer indirection and address arithmetic to access and traverse the array. No array index [] should be used in the function.
Do not use global variables.
Sample Inputs/Outputs:
1 2 33
445
23
^D
Inputs: 1 2 33 445 23
Largest value: 445
Template code to follow:
/* Passing an array to a function. */
#include
#define MAX 20
int largest(int * x);
void display(int *arr);
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)
{ }
/* Function largest() returns the largest value */
/* in an integer array */
int largest(int * arr)
{ }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
