Let A be an array of n elements. Write a template function, maxfunc(...), which
ID: 3762321 • Letter: L
Question
Let A be an array of n elements. Write a template function, maxfunc(...), which takes an unsorted array of type as an input parameter and returns the element with the maximum value. Assume the operators are defined for the class T. In this question, you may hardcode n=5. You must write all functions you call, and also need to write a main () to test the following data arrays stored in a data file (data.dat): In addition, the results should be printed out in main() routine to the screen and to an output file (out.txt).Explanation / Answer
#include <stdio.h>
void intSwap(int *x, int *y);
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
void reverseIntArray(int a[], int n);
int main(void) {
int x[10];
int hmny;
hmny = getIntArray(x, 10, 0);
printf("The array was: ");
printIntArray(x,hmny);
reverseIntArray(x,hmny);
printf("after reverse it is: ");
printIntArray(x,hmny);
}
void intSwap(int *x, int *y)
/* It swaps the content of x and y */
{
#include <stdio.h>
void intSwap(int *x, int *y);
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
void reverseIntArray(int a[], int n);
int main(void) {
int x[10];
int hmny;
hmny = getIntArray(x, 10, 0);
printf("The array was: ");
printIntArray(x,hmny);
reverseIntArray(x,hmny);
printf("after reverse it is: ");
printIntArray(x,hmny);
}
void intSwap(int *x, int *y)
/* It swaps the content of x and y */
{
do {
printf("Enter integer [%d to terminate] : ", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full ");
else
a[n++] = temp;
}while (1);
return n;
}
/* It reverse the order of the first n elements of array */
void reverseIntArray(int a[], int n)
{
int i;
for(i=0;i<n/2;i++){
intSwap(&a[i],&a[n-i-1]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.