I am writing a program that reads ten integers from standard input, and determin
ID: 3800390 • 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int isGoingUp(int a[]);
int isGoingDown(int b[]);
int main(void) {
int array[10];
int a;
printf("Enter ten integers : ");
for (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;
}
int isGoingUp(int a[]){
int i;
for(i=0; i<9; i++){
if(a[i]>a[i+1]){
return 0;
}
}
return 1;
}
int isGoingDown(int b[]){
int i;
for(i=0; i<9; i++){
if(b[i]<b[i+1]){
return 0;
}
}
return 1;
}
Output:
sh-4.2$ gcc -o main *.c
sh-4.2$ main
Enter ten integers : 1 2 3 4 5 6 7 8 9 10
The values are going up
sh-4.2$ gcc -o main *.c
sh-4.2$ main
Enter ten integers : 10 9 8 7 6 5 4 3 2 1
The values are going down
sh-4.2$ main
Enter ten integers : 10 9 8 7 1 2 3 4 5 6
The values are neither going up or going down
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.