-6
#include <iostream>
using namespace std;

int main() {
    int a=70, b=40, c=5, d=1, e=-20, f=90, g=2, mid;
    int array[7]={a, b, c, d, e, f, g};
    for (int i = 0; i<=5; i++) {
        while (i=0) {
            if (a>b && a>c && a>d && a>e && a>f && a>g) {
                mid=g;
                g=a;
                a=mid;
            }
                else if (b>a && b>c && b>d && b>e && b>f && b>g) {
                    mid=g;
                    g=b;
                    b=mid;
                }
                else if (c>a && c>b && c>d && c>e && c>f && c>g) {
                    mid=g;
                    g=c;
                    c=mid;
                }
                else if (d>a && d>c && d>b && d>e && d>f && d>g) {
                    mid=g;
                    g=d;
                    d=mid;
                }
                else if (e>a && e>c && e>d && e>b && e>f && e>g) {
                    mid=g;
                    g=e;
                    e=mid;
                }
                else if (f>a && f>c && f>d && f>e && f>b && f>g) {
                    mid=g;
                    g=f;
                    f=mid;
                }
                else if (g>a && g>c && g>d && g>e && g>f && g>b) {
                    mid=g;
                    g=g;
                    g=mid;
                }
        }
        while (i=1) {
            if (a>b && a>c && a>d && a>e && a>f) {
                mid=f;
                f=a;
                a=mid;
            }
                else if (b>a && b>c && b>d && b>e && b>f) {
                    mid=f;
                    f=b;
                    b=mid;
                }
                else if (c>a && c>b && c>d && c>e && c>f) {
                    mid=f;
                    f=c;
                    c=mid;
                }
                else if (d>a && d>c && d>b && d>e && d>f) {
                    mid=f;
                    f=d;
                    d=mid;
                }
                else if (e>a && e>c && e>d && e>b && e>f) {
                    mid=f;
                    f=e;
                    e=mid;
                }
                else if (f>a && f>c && f>d && f>e && f>b) {
                    mid=f;
                    f=b;
                    b=mid;
                }
        }
        while (i=2) {
            if (a>b && a>c && a>d && a>e) {
                mid=e;
                e=a;
                a=mid;
            }
                else if (b>a && b>c && b>d && b>e) {
                    mid=e;
                    e=b;
                    b=mid;
                }
                else if (c>a && c>b && c>d && c>e) {
                    mid=e;
                    e=c;
                    c=mid;
                }
                else if (d>a && d>c && d>b && d>e) {
                    mid=e;
                    e=d;
                    d=mid;
                }
                else if (e>a && e>c && e>d && e>b) {
                    mid=e;
                    e=e;
                    e=mid;
                }

        }
        while (i=3) {
            if (a>b && a>c && a>d) {
                mid=d;
                d=a;
                a=mid;
            }
                else if (b>a && b>c && b>d) {
                    mid=d;
                    d=b;
                    b=mid;
                }
                else if (c>a && c>b && c>d) {
                    mid=d;
                    d=c;
                    c=mid;
                }
                else if (d>a && d>c && d>b) {
                    mid=d;
                    d=d;
                    d=mid;
                }

        }
        while (i=4) {
            if (a>b && a>c) {
                mid=c;
                c=a;
                a=mid;
            }
                else if (b>a && b>c) {
                    mid=c;
                    c=b;
                    b=mid;
                }
                else if (c>a && c>b) {
                    mid=c;
                    c=c;
                    c=mid;
                }


        }
        while (i=5) {
            if (a>b) {
                mid=b;
                b=a;
                a=mid;
            }
                else if (b>a) {
                    mid=b;
                    b=b;
                    b=mid;
                }
        }
    }

    for (int k=0; k<7; k++){
        cout<<array[k]<<" ";
    }

}

This program does not give me an output... What do I do instead? I have tried everything, but it just won't get through to the execution part? What do I do?

acraig5075
  • 10,588
  • 3
  • 31
  • 50
bmgballer
  • 1
  • 2
  • 6
    What's wrong with `int array[7]={70, 40, 5, 1, -20, 90, 3}; std::sort(std::begin(array), std::end(array));`? – Some programmer dude Jan 27 '20 at 08:47
  • 3
    The purpose of those posting restrictions is that the user gives a more detailed error description than *"This program does not give me an output... What do I do instead? I have tried everything"*, not that he slaps a wall of filler on the question. *"What do I do?"* If your code is unexpectedly not getting to a part, [debug the program](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) to see why that happens. – Blaze Jan 27 '20 at 08:48
  • As for your problem, think about what you do in your initialization of the array, and which variables you change. Do you really modify the elements in the array? Perhaps you should invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and start reading from the very beginning? – Some programmer dude Jan 27 '20 at 08:48
  • 3
    Lastly please take some time to refresh (or actually read) [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jan 27 '20 at 08:50
  • you have infinity loops, `std::sort()` is much better – Raffallo Jan 27 '20 at 08:51
  • 2
    In general `while(i = x)` will _never_ run if x is 0 and else be an infinite loop. You confused assignment (`=`) with comaprison (`==`) – Lukas-T Jan 27 '20 at 08:54

2 Answers2

1
  1. You confused assignment (=) with comaprison (==). while(i = x) will assign x to i, then evaluate i, so it's equal to while(x), which does never run if x is 0 and endless otherwise.

  2. Your loops are still endless. Take for example the (fixed) loop while (i == 3). i is never modified inside that loop and there is no break or return in it, so even the fixed version will run endlessy. But that's a design issue you need to solve. Maybe there should be a else { break; } after all those if's?

  3. You will still get the wrong result, because you swap the values of the variables, a, b, c, d, e, f and g, but never modify array. However in the end, you print the values of array. The array does not store items by reference, but copies their values. Why do you even use so many variables and don't operate on the array directly?

  4. The last else if(..) block in each while is useless. Take a close look at - for example - this code:

    mid=d;
    d=d;
    d=mid;
    

    It assigns the value of d to d. Twice.

  5. As already mentioned in the comments, sorting is implemented in the standard library. Use std::sort instead of writing your own sort function:

    std::sort(std::begin(array), std::end(array));
    
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
0

You can use std or boost library for sorting. Or else you can code yourself. Please go through the below code.

#include<iostream> 
using namespace std;

void selectionSort(int a[], int n) {
   int i, j, min, temp;
   for (i = 0; i < n - 1; i++) {
      min = i;
      for (j = i + 1; j < n; j++)
         if (a[j] < a[min])
            min = j;
      temp = a[i];
      a[i] = a[min];
      a[min] = temp;
   }
}
int main() {
   int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
   int n = sizeof(a)/ sizeof(a[0]);
   int i;
   cout<<"Given array is:"<<endl;
   for (i = 0; i < n; i++)
   cout<< a[i] <<" ";
   cout<<endl;
   selectionSort(a, n);      // calling my function selection sort. This will sort as ascending order

   for (i = 0; i < n; i++)  
   cout<< a[i] <<" ";       // Printing sorted array
   return 0;
}

Above code is for ascending order.

For Descending order refer: https://www.includehelp.com/cpp-programs/sort-an-array-in-descending-order.aspx

Akhil Pathania
  • 542
  • 2
  • 5
  • 19
  • That wasn't the question. Don't do his homework please. The real answer is that he confused `=` with `==` among other problems. This causes an endless loop, so no output. – Goswin von Brederlow Jan 27 '20 at 11:07
  • Correct @GoswinvonBrederlow But i am not doing his homework. I am just telling him the right way to sort. – Akhil Pathania Jan 27 '20 at 11:09
  • What is the "right" way to sort depends on many factors. Why would it be insertion sort? Also OP tried to implement a kind of hardcoded bubble sort. Please don't just throw complete code at people, especially, if they didn't ask you to do so. It leads to [Cargo cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming). – Lukas-T Jan 27 '20 at 12:02