Output Should be in increasing order input a series of integers(ending with 0):1
ID: 3832881 • Letter: O
Question
Output Should be in increasing order
input a series of integers(ending with 0):1 2 3 0
Input a number to find in the series
:7
Question, How to make the program break or ask user to try again ONLY IF user numbers are not in increasing order
input a series of integers(ending with 0):3 2 1 0 <- if numbers are not in increasing order
the program should break or ask user to try again
if the user numbers are in incresing order the program should continue
please do not use count funtion
Code
#include<stdio.h>
#include<stdbool.h>
#define N 10//N is defined 10
bool search( int a[],int m, int i)
{
int j;// function is as defined
bool found=false;// bound set to false
for(j=0;j<=i;j++)// loop inside array
{
if(a[j]==m)//j in the array is compared to m
{
found=true;// it is true if integer is found
break;
}
}
return found;
}
int main()
{
int a[N],found, i, m;// functions are defined
printf("input a series of integers(ending with 0)", N);//ask user to input numbers
for (i=0;i
{
scanf("%d",&a[i]);
if(a[i]==0)// if 0 is entered program will stop
{
break;// if 0 is entered program will stop
}
}
printf("Input a number to find in the series :");//ask the user to input a number to find
scanf("%d",&m);
found=search(a,m,i);//it will search and number of elements as parmeters
if(found)
{
printf(" The number %d is found in the series ",m);
}
else
{
printf(" The number %d is not in the series ",m);
}
return 0;
}
Explanation / Answer
// C code
#include <stdio.h>
#include <stdbool.h>
#define N 10//N is defined 10
bool search( int a[],int m, int i)
{
int j;// function is as defined
bool found=false;// bound set to false
for(j=0;j<=i;j++)// loop inside array
{
if(a[j]==m)//j in the array is compared to m
{
found=true;// it is true if integer is found
break;
}
}
return found;
}
int main()
{
int a[N],found, i, m;// functions are defined
printf("Input a series of integers(ending with 0): ");//ask user to input numbers
// taking input first number before the for loop
scanf("%d",&a[0]);
if(a[0] == 0)
{
return 0;
}
// now taking input other number and checking the increasing order
for (i=1;i<10;i++)
{
scanf("%d",&a[i]);
if(a[i]==0)// if 0 is entered program will stop
{
break;// if 0 is entered program will stop
}
else if(a[i] < a[i-1])
{
printf("Only Increasing order allowed, Try again ");
return 0;
}
}
printf("Input a number to find in the series: ");//ask the user to input a number to find
scanf("%d",&m);
found=search(a,m,i);//it will search and number of elements as parmeters
if(found)
{
printf(" The number %d is found in the series ",m);
}
else
{
printf(" The number %d is not in the series ",m);
}
return 0;
}
/*
output:
Input a series of integers(ending with 0):
1
2
3
4
5
2
Only Increasing order allowed, Try again
Input a series of integers(ending with 0):
1
2
3
4
5
6
7
8
0
Input a number to find in the series: 5
The number 5 is found in the series
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.