# Thread: writing a program that is not allowed to have loops.

1. ## writing a program that is not allowed to have loops.

Integers will be read from the file and should be bubble sorted.. its a lot of if statements to avoid the for loop and its still not sorting correctly..Is there any way i can shorten this code and have it bubble sorted at the same time?

Code:
``` #include <stdio.h>
#include <iostream>
#include<fstream>
#include<string>

void swap(int *, int *);

using namespace std;
int main(int argc, char *argv[])
{
int key,a[14],i;
string line;
cout<<"Enter search key";
cin>>key;
//opens file
ifstream myfile ("integers.txt");

if (myfile.is_open())
{
while ( myfile.good() )
{
for(i=0;i<=14;i++){
myfile>>a[i];

cout << a[i] << endl;

}

if (a[0] > a[1]) swap(a, a+1);
if (a[1] > a[2]) swap(a+1, a+2);
if (a[2] > a[3]) swap(a+2, a+3);
if (a[3] > a[4]) swap(a+3, a+4);
if (a[4] > a[5]) swap(a+4, a+5);
if (a[5] > a[6]) swap(a+5, a+6);
if (a[6] > a[7]) swap(a+6, a+7);
if (a[7] > a[8]) swap(a+7, a+8);
if (a[8] > a[9]) swap(a+8, a+9);
if (a[9] > a[10]) swap(a+9, a+10);
if (a[10] > a[11]) swap(a+10, a+11);
if (a[11] > a[12]) swap(a+11, a+12);
if (a[12] > a[13]) swap(a+12, a+13);
if (a[13] > a[14]) swap(a+13, a+14);

if (a[0] > a[1]) swap(a, a+1);
if (a[1] > a[2]) swap(a+1, a+2);
if (a[2] > a[3]) swap(a+2, a+3);
if (a[3] > a[4]) swap(a+3, a+4);
if (a[5] > a[6]) swap(a+5, a+6);
if (a[6] > a[7]) swap(a+6, a+7);
if (a[7] > a[8]) swap(a+7, a+8);
if (a[8] > a[9]) swap(a+8, a+9);
if (a[9] > a[10]) swap(a+9, a+10);
if (a[10] > a[11]) swap(a+10, a+11);
if (a[11] > a[12]) swap(a+11, a+12);
if (a[12] > a[13]) swap(a+12, a+13);
if (a[13] > a[14]) swap(a+13, a+14);

if (a[0] > a[1]) swap(a, a+1);
if (a[1] > a[2]) swap(a+1, a+2);
if (a[2] > a[3]) swap(a+2, a+3);
if (a[3] > a[4]) swap(a+3, a+4);
if (a[5] > a[6]) swap(a+5, a+6);
if (a[6] > a[7]) swap(a+6, a+7);
if (a[7] > a[8]) swap(a+7, a+8);
if (a[8] > a[9]) swap(a+8, a+9);
if (a[9] > a[10]) swap(a+9, a+10);
if (a[10] > a[11]) swap(a+10, a+11);
if (a[11] > a[12]) swap(a+11, a+12);
if (a[12] > a[13]) swap(a+12, a+13);
if (a[13] > a[14]) swap(a+13, a+14);

if (a[0] > a[1]) swap(a, a+1);
if (a[1] > a[2]) swap(a+1, a+2);
if (a[2] > a[3]) swap(a+2, a+3);
if (a[3] > a[4]) swap(a+3, a+4);
if (a[5] > a[6]) swap(a+5, a+6);
if (a[6] > a[7]) swap(a+6, a+7);
if (a[7] > a[8]) swap(a+7, a+8);
if (a[8] > a[9]) swap(a+8, a+9);
if (a[9] > a[10]) swap(a+9, a+10);
if (a[10] > a[11]) swap(a+10, a+11);
if (a[11] > a[12]) swap(a+11, a+12);
if (a[12] > a[13]) swap(a+12, a+13);
if (a[13] > a[14]) swap(a+13, a+14);

cout << "Sorted array : ";

for (int i = 0; i < 14; i++) cout << a[i] << " ";

cout << endl;

}
myfile.close();
}

else cout << "Unable to open file";
system("pause");
return 0;
}
void swap(int *x, int *y) {

int temp = *x;

*x = *y;

*y = temp;

}```

2. O_o

So because you were getting quality help in the last thread you posted again with the same problem?

Soma

3. I didnt kno my question asked for somebody to take time out to bull........ on here. but thanks.

4. phantomap is referring to it being bad online etiquette to start multiple threads for one discussion. It is sort of akin to starting a discussion with one group of folks and, before it is over, snubbing the original group in order to pester someone else.

All your code is doing is unrolling the loops (i.e. instead of looping 10 times, you copy and paste the steps 10 times). That is error prone (hard to get right), and hard to maintain. If you want to shorten your code ..... then use a loop construct. That is the purpose of loop constructs.

As mentioned in the other thread, it is possible to implement loops covertly by using recursion. All that achieves is implementing a loop without using an overt looping construct (for(), do-while, while, goto, etc). The loop still exists, it is just packaged differently.

Just as the loop still exists in your code .... the overt looping was implemented in your head using your fingers, in copying and pasting text multiple times.

5. Originally Posted by grumpy
phantomap is referring to it being bad online etiquette to start multiple threads for one discussion. It is sort of akin to starting a discussion with one group of folks and, before it is over, snubbing the original group in order to pester someone else.

All your code is doing is unrolling the loops (i.e. instead of looping 10 times, you copy and paste the steps 10 times). That is error prone (hard to get right), and hard to maintain. If you want to shorten your code ..... then use a loop construct. That is the purpose of loop constructs.

As mentioned in the other thread, it is possible to implement loops covertly by using recursion. All that achieves is implementing a loop without using an overt looping construct (for(), do-while, while, goto, etc). The loop still exists, it is just packaged differently.

Just as the loop still exists in your code .... the overt looping was implemented in your head using your fingers, in copying and pasting text multiple times.
Thanks. I understand what youre saying... i just honestly do not know how to make recursion work for this. i have some code which prints 1-10 without a loop but for something as complex as a bubble sort and im not that advanced at c++ im very confused

6. The point about recursion is that it eventually reduces to a simple case.

For sorting, it is trivial to sort an array with only one element (it's already sorted).

To sort an array of two elements, either they're in order (do nothing) or they're not (and you swap them).

Sorting 3 elements is basically do something like recursive sort to two elements, followed by doing something with the 3rd element.

Study the normal looping bubble sort, and see if you can split it out into
a) a simple operation
b) a recursive call to work on n-1 elements of the array.

You should note that faster sorting functions like quicksort are inherently recursive.

Popular pages Recent additions