Hey guys,

I'm a newbie in C and I've got to make an app:

Users enter 10 numbers;
I've got to compare them;
At the end tell the user if
a) Number are decreasing (ex: 10 9 8 7 6 5 4 3 2 1 0)
b) Number are creasing (ex: 1 2 3 4 5 6 7 8 9 10)
c) Number are equals (ex: 4 4 4 4 4 4 4 4 4 4)
d) Number are disorderly (ex: 9 8 2 7 4 9 2 3 0)

I've made this:

#include <stdlib.h>
#include <stdio.h>

int main ()

{

    int Vecteur[10] = {0}, i, CompteurA = 0, CompteurB = 0;

    printf("Entrez vos dix nombres: \n");
    for(i = 0; i < 9; i++)
    {
        scanf("%d ", &Vecteur[i]);
    }
    for(i = 0; i < 9; i++)
    {
        if(Vecteur[i] > Vecteur[i+1])
        {
            CompteurA++;
        }
        if(Vecteur[i] < Vecteur[i+1])
        {
            CompteurB++;
        }
    }


    printf("%d\n", CompteurA);
    printf("%d", CompteurB);
    if(CompteurA == 9)
    {
        printf("Decroissant");
    }
    else
    {
        if(CompteurB == 8)
        {
            printf("Croissant");
        }
    }
}

I haven't done yet the rest but I was wondering if we were oblige to use all these "if"...

Is there an other way but a newbies way?

Thanks.

Try this program:

# include <stdio.h>

int main()
  {
   int i,flag=0;
   int num[10];
   printf("\n\n\t\t Enter 10 values:");
   for(i=0;i<10;i++)
     scanf("%d",&num[i]);

   for(i=0;i<9;i++)
    {
       if(num[i]<num[i+1])
	  flag++;
       else if(num[i]>num[i+1])
	  flag--;
    }

   if(flag == -9)
      printf("\n\n  decreasing..");
   else if(flag == 9)
      printf("\n\n  increasing..");
   else if(flag == 0)
      printf("\n\n  equals");
   else
      printf("\n\n Disorderly");


   return 0;
}

Thanks vinit.
So it's the best way if-else... Thanks

there could be another ways to solve this but i have doubt about simplicity. It's already explained in a simple way.

This article has been dead for over six months. Start a new discussion instead.