Why is not the array ordered correctly in c? [closed]

1

When I run the program it shows me the array created correctly, but the problem is that it does not command it. I do not know if it will be a problem of how I pass the arrangement as a parameter to the functions, since I am learning and it still costs me a bit that topic. Thank you very much

#include <iostream>
#include <stdlib.h>
#include <time.h>

int *generarArreglo(int size);
void OrdenaMburbujaD (int * a1);
void OrdenaMburbujaA (int * a1);
int main()
{
char op;
int *a;
a=generarArreglo(100);
if (a==NULL)
 { printf("No hya memoria suficiente para el arreglo"); }
else {    

printf("Arreglo sin ordenar \n");
for (int i=0;i<=99;i++)
   { printf("%d   ", a[i]);}
printf ("\n");

printf("Ingrese la letra D para ordenar el arreglo de forma Descendente \n"
       "Ingrese la letra A para ordenar el arreglo de forma Ascendente \n");
op=getchar();

if (op= 'd')
  { OrdenaMburbujaD(a); }
else if (op= 'a') {OrdenaMburbujaA(a);}
printf("Arreglo ordenado \n");
for (int i=0;i<=99;i++)
   { printf("%d   ", a[i]);} }







system("PAUSE");
return 0;
}
int *generarArreglo(int size)
{   
int idx;
int *arr;

arr = (int *)calloc( size, sizeof( int ) );

if( arr ) {
srand( time( NULL ) ); }

for( idx = 0; idx < size; ++idx )
  {arr[idx] = rand( ) % 100;} 
  return arr; }

  void OrdenaMburbujaD (int * a1)
 {

 int pasada, j,almacena1;
 for (pasada=0;pasada<=99;pasada++)
    {  for(j=0;j<=99;j++)
          { if (a1[j]<a1[j+1])
              { almacena1=a1[j];
                a1[j]=a1[j+1];
                a1[j+1]=almacena1;}}} 
 }

 void OrdenaMburbujaA (int * a1)
{

 int pasada, j, almacena;
 for (pasada=0;pasada<=99;pasada++)
    {  for(j=0;j<=99;j++)
          { if (a1[j]>a1[j+1])
              { almacena=a1[j];
                a1[j]=a1[j+1];
                a1[j+1]=almacena;}}} 
 }
    
asked by Maca Igoillo 27.01.2017 в 15:37
source

3 answers

1

I think the error is that you are not returning the arranged arrangement, try this

 int* OrdenaMburbujaA (int * a1){
   int pasada, j, almacena;
   for (pasada=0;pasada<=99;pasada++)
   {  for(j=0;j<=99;j++)
      { if (a1[j]>a1[j+1])
          { almacena=a1[j];
            a1[j]=a1[j+1];
            a1[j+1]=almacena;}}} 
    return a1;
  }

And then obviously

if (op= 'd')
  { a = OrdenaMburbujaD(a); }
    
answered by 27.01.2017 в 15:45
1

You still use #include <iostream> , and then label the question as C .

That the code works for you, indicates that you are using a C ++ compiler, which, at some point, will lead to an incompatibility difficult to diagnose. Change it to

#include <stdio.h>

and compile with C, or change the label from C to C ++ .

Following, you do

if( op= 'd' ) {
  OrdenaMburbujaD( a );
} else if( op = 'a' ) {
  OrdenaMburbujaA(a);
}

Although that is valid, in C and C ++ to compare the == operator is used:

if( op == 'd' ) {
  OrdenaMburbujaD( a );
} else if( op == 'a' ) {
  OrdenaMburbujaA(a);
}

With those simple changes, your code works perfect .

    
answered by 27.01.2017 в 18:49
1

The problem I think is in the conditions if that check the option typed. I do not understand if you have another problem because I have compiled yours by changing the = allocations for == comparisons within if and it worked fine for me.

By the way, here's your code fixed and using something more than C ++ (I replaced the C libraries with the C ++ equivalents):

#include <iostream>
#include <cstdlib>
#include <ctime>

int *generarArreglo(int size);
void OrdenaMburbujaD (int * a1);
void OrdenaMburbujaA (int * a1);

int main()
{
  char op;
  int *a;
  a = generarArreglo(100);
  if (a == NULL) {
    std::cout << "No hya memoria suficiente para el arreglo" << std::endl;
  }
  else {    

    std::cout << "Arreglo sin ordenar" << std::endl;
    for (int i = 0; i <= 99; i++) {
      std::cout << a[i] << std::endl;
    }
    std::cout << std::endl;

    std::cout << "Ingrese la letra D para ordenar el arreglo de forma Descendente" << std::endl <<
      "Ingrese la letra A para ordenar el arreglo de forma Ascendente" << std::endl;
    std::cin >> op;
    if (op == 'd') {
      OrdenaMburbujaD(a);
    } else if (op == 'a') {
      OrdenaMburbujaA(a);
    }
    std::cout << "Arreglo ordenado" << std::endl;
    for (int i=0; i <= 99; i++) {
      std::cout << a[i] << " ";
    }
  }
  std::cout << std::endl;
  return 0;
}

int *generarArreglo(int size)
{   
  int idx;
  int *arr;

  arr = (int *)std::calloc(size, sizeof(int));

  if ( arr ) {
    std::srand( std::time( NULL ) ); }

  for( idx = 0; idx < size; ++idx )
  {arr[idx] = std::rand( ) % 100;} 
  return arr; }

void OrdenaMburbujaD (int * a1)
{

  int pasada, j,almacena1;
  for (pasada=0;pasada<=99;pasada++)
  {  for(j=0;j<=99;j++)
    { if (a1[j]<a1[j+1])
      { almacena1=a1[j];
        a1[j]=a1[j+1];
        a1[j+1]=almacena1;}}} 
}

void OrdenaMburbujaA (int * a1)
{

  int pasada, j, almacena;
  for (pasada=0;pasada<=99;pasada++)
  {  for(j=0;j<=99;j++)
    { if (a1[j]>a1[j+1])
      { almacena=a1[j];
        a1[j]=a1[j+1];
        a1[j+1]=almacena;}}} 
}

PS: If you do not like to put std:: you can add at the top using namespace std; , but I prefer to always be clear to which namespace each function belongs.

    
answered by 27.01.2017 в 19:41