I am writing a program that reads ten integers from standard input, and determin
ID: 3800411 • Letter: I
Question
I am writing a program that reads ten integers from standard input, and determines if they are going up or down or neither. Can I get some help? I have supplied what I have already.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int isGoingUp(int [a]);
int isGoingDown(int [b]);
int main(void) {
int array[10];
printf("Enter ten integers : ");
for (int a = 0; a < 10; a++) scanf("%d", &array[a]);
if (isGoingUp(array) == 1)
printf("The values are going up ");
else if (isGoingDown(array) == 1)
printf("The values are going down ");
else
printf("The values are neither going up or going down ");
return 0;
}
Explanation / Answer
Program:-
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//implemented the isGoingUp() function to check array is in ascending order
int isGoingUp(int a[])
{
int i;
for(i = 1; i< 9 ; i++){
if(a[i] < a[i-1])
return 0;
}
return 1;
}
//implemented the isGoingDown() function to check array is in descending order
int isGoingDown(int b[])
{
int i;
for(i = 1; i< 9 ; i++){
if(b[i] > b[i-1])
return 0;
}
return 1;
}
//drive function it will scan's 10 integers from the input and check whether the array is going up or down or neither of them
int main(void) {
int array[10];
printf("Enter ten integers : ");
for (int i = 0; i < 10; i++)
scanf("%d", &array[i]);
if (isGoingUp(array) == 1)
printf("The values are going up ");
else if (isGoingDown(array) == 1)
printf("The values are going down ");
else
printf("The values are neither going up or going down ");
return 0;
}
Output:-
Enter ten integers : 9 88 7 6 78 4 3 2 8
The values are neither going up or going down
Enter ten integers : 1 2 3 4 5 6 7 8 9 10
The values are going up
Enter ten integers : 10 9 8 7 6 5 4 3 2 1
The values are going down
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.