-4

I need to input a number (user enters -1 to exit) and the program needs to tell if it increasing monotone sequence.

I did it in do-while loop, I know that if I want to check if it is increasing I need to check the previous number and then compare the previous and the current number.

But still, its hard to me to imagine the process of the loop.

Here is the code:

#include <stdio.h>

void main()

{

    int num, i = 0, numPrev = 0;
    printf("Enter a number:");

    do {

        scanf("%d", &num);

        numPrev = num;
        i++;

        if (num > numPrev) 

    } while (num != -1);

        printf("The Serias is Monotinic Acsending\n");

}
Aditi Rawat
  • 784
  • 1
  • 12
  • 15
AndrewM
  • 55
  • 7

2 Answers2

0

You should make the following changes. I have added comments where required. Assuming that you wish to read the entire input!

#include <stdio.h>

int main(void)//refer the link given below

{

    int  i = 0, numCurr, numPrev = 0; //for storing current no. and previous no.
    int flag = 0;
    printf("Enter input:");

    do {

        scanf("%d", &numCurr);

        if(i != 0 && numCurr != -1 && numPrev > numCurr)//so that comparison begins from the second element
            flag = 1;

        numPrev = numCurr;
        i++;

    } while (numCurr != -1);

    if(flag == 0)printf("The Serias is Monotinic Acsending\n");
    else    printf("The Serias is not Monotinic Acsending\n");  

    return 0;
 }

Also do hve a look :What should main() return in C and C++?

Aditi Rawat
  • 784
  • 1
  • 12
  • 15
  • Thanks for the answer, But I didn't understand the flag=1; can explain, please. – AndrewM Jan 04 '18 at 13:42
  • @AndrewM We need to somehow store the info whether in the series any previous element is greater than current. Hence we initialize `flag=0` . While taking the input if we come across elements such that `previous>current` we change the value of `flag` to 1. So in the end if we still have `flag` equals to zero it implies the list is monotnically increasing. Do tell if anything is still unclear. – Aditi Rawat Jan 04 '18 at 17:17
  • Thank you for the help! I try to compile it initializing the Flag=0 (because maybe you forgot to initialize it?) and it won't runs as I want to it to run. the sequence is always increasing. – AndrewM Jan 05 '18 at 11:20
  • @AndrewM yes..my bad. I have edited the answer. And it works fine now. – Aditi Rawat Jan 05 '18 at 13:36
0

Key points:

  • Validate user input (check scanf return value)
  • Comparison of numbers only makes sense after the second number was inputted by the user
  • Compare previous to current number before re-assigning previous

Consider the following code

#include <stdio.h>

int main(void)
{
    int num, i = 0, numPrev = 0;
    printf("Enter a number:");

    if (!scanf("%d", &numPrev)) return -1;

    while (numPrev != -1)
    {
        if (!scanf("%d", &num)) return -1;

        if (num != -1 && num <= numPrev)
        {
            printf("The Serias is not Monotinic Acsending\n");
            return 0;
        }
        numPrev = num;
    }

    printf("The Serias is Monotinic Acsending\n");

    return 0;
}

If you can't return directly when a not-monotone series was found, you may need some sort of result variable to decide whether the loop was canceled (not monotone) or completed (monotone).

grek40
  • 13,113
  • 1
  • 24
  • 50