How do I write these code blocks as separate functions? (C programming) Right no
ID: 3806924 • Letter: H
Question
How do I write these code blocks as separate functions? (C programming)
Right now, the code blocks below are in my main function. However, they need to be their own outside functions and I'm still having trouble writing them. What would these be as their own functions and how would I call them in the main?
What I have declared at the top of my main: int a[10], i, max, *max;
1. User enters a series of integers with 0 as the sentinel to break the loop
for (i=0; i<10; i++) {
printf ("Please enter integers (no more than 10 numbers) with a 0 as the last number ");
scanf ("%d", a+i);
if ((a+i)==0) {
break;
}
}
2. Prints the elements in the array of the user input numbers
printf ("You entered: ");
for (i=0; i<10; i++) {
printf (" %d", *(a+i));
}
3. Finds the largest number in the array
max=a;
*max=*a;
for (i=1; i<10; i++) {
if (*(a+i)>*max) {
*max=*(a+i);
}
}
printf ("The largest number in the series is: " %d, max);
Explanation / Answer
Here is the broken down working code
#include <stdio.h>
int inputArr(int *a)
{
int i;
printf ("Please enter integers (no more than 10 numbers) with a 0 as the last number ");
for (i=0; i<10; i++) {
scanf ("%d", a+i);
if (*(a+i)==0) {
break;
}
}
return i;
}
void printArr(int *a, int n)
{
printf ("You entered: ");
int i;
for (i = 0; i < n; i++) {
printf (" %d", *(a+i));
}
printf(" ");
}
void printLargest(int *a, int n)
{
int *max;
max=a;
int i;
for (i=1; i<n; i++) {
if (*(a+i)>*max) {
*max=*(a+i);
}
}
printf ("The largest number in the series is:%d " , *max);
}
int main()
{
int a[10];
int n;
n = inputArr(a);
printArr(a, n);
printLargest(a, n);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.