Assuming the array x has been defined as: int x[n]; for some n and that values h
ID: 3552173 • Letter: A
Question
Assuming the array x has been defined as: int x[n]; for some n and that values have been assigned to all the elements. Print "increasing" if the value in each element is less than the value in the next element, "decreasing" if the value in each element is greater than the value in the next element, or "neither" otherwise. For example, if the values of the array are: [1, 11, 18], then your code should print "increasing"; if the values of the array are: [9, 4, 3, 2, 1, -1, -4], then your code should print "decreasing"; if the values of the array are: [1, 6, 7, 7, 8, 9, 11, 18, 21, 43, 88], then your code should print "neither". Assume there are at least two elements in the array (i.e., the value of n is at least 2).Explanation / Answer
#include<stdio.h>
const int n = 16;
int order(int x[],int size)
{
int i;
int inc=0;
int dec=0;
for(i=0; i<size-1; i++)
{
if(x[i]<=x[i+1]) inc = 1;
if(x[i]>=x[i+1]) dec = 1;
}
if(inc==1 && dec == 0) return 1;
else if(inc==0 && dec == 1) return -1;
return 0;
}
int main()
{
int a[3] = {1,11,18};
int b[7] = {9,4,3,2,1,-1,-4};
int c[11] = {1, 6, 7, 7, 8, 9, 11, 18, 21, 43, 88};
if(order(a,3)==1)
printf("increasing ");
else if(order(a,3)==0)
printf("neither ");
else if(order(a,3)==-1)
printf("decreasing ");
if(order(b,7)==1)
printf("increasing ");
else if(order(b,7)==0)
printf("neither ");
else if(order(b,7)==-1)
printf("decreasing ");
if(order(c,11)==1)
printf("increasing ");
else if(order(c,11)==0)
printf("neither ");
else if(order(c,11)==-1)
printf("decreasing ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.