-
Need help writing a bubble sort with no loops
Ive written a long program that should be a bubble sort , unfortunately I cant use loops ...is there any way i can shorten this and make it indeed a bubble sort? Its sorting atm but its not entirely correct.
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;
}
-
Re: Need help writing a bubble sort with no loops
Why can't you use loops? I suppose you could use recursion here.
-
Re: Need help writing a bubble sort with no loops
its jus what ive been asked to do.. Ive gotten suggestions about recursion but im still a bit lost as to how id implement it with the whole base case condition etc etc
-
Re: Need help writing a bubble sort with no loops
Who asked you to do this and why?
Recursion would be the only practical alternative, although it's unusual to implement a bubble sort using it. Any decent search engine will bring you to a page explaining how to do it.
-
Re: Need help writing a bubble sort with no loops
Its some homework I have to do.. I have googled recursion and tried finding bubble sorts using it but i cant find any that dont have loops or that work so ive come here..
-
Re: Need help writing a bubble sort with no loops
This might get you started. Here is the way to use recursion to print out the contents of a vector, instead of using a loop to do so:
Code:
#include <vector>
#include <iostream>
using namespace std;
// Prints all entries of vector 'vecInt',
// starting at index 'start' and going until the end
void recurse_print(const vector<int>& vecInt, const int start)
{
if (start >= vecInt.size()) return;
cout << vecInt[start] << " ";
recurse_print(vecInt, start+1);
}
int main()
{
// Fill up the vector
vector<int> vecInt;
for (int i=0; i<10; ++i) {
vecInt.push_back(i);
}
recurse_print(vecInt, 0);
cout << endl;
return 0;
}
Instead of explicitly looping over the contents of the vector, we process one entry then call the recursive function again on the remainder of the vector. The base case (when 'start' is past the end of the vector) simply returns.
You will need to do something similar to replace the loops in the bubble sort.
-
Re: Need help writing a bubble sort with no loops
Code:
#include <iostream>
using namespace std;
void MyFunction(int a)
{
cout << "i = " << a << "\n";
if(a < 2)
{
cout << "exit condition met!\n\n";
}
else
MyFunction(a-1);
}
int main()
{
int a=10;
cout << "Recursion is about to occur \n";
MyFunction(a);
system("pause");
return 0;
}
Ok i have this simple code which output 10 int just like the above. I understand that it can replace a loop but how i would integrate it in to this bubble sort code I have is beyond me. The recursive code prints 10 numbers in order but how i would switch it up to sort an array ... Im seriously drawing a blank.
Code:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void bubble(int a[],int n)
{
int i,j,t;
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}//end for 1.
}//end function.
int main()
{
int a[100],n,i;
//clrscr();
printf("\n\n Enter integer value for total no.s of elements to be sorted: ");
scanf("%d",&n);
for( i=0;i<=n-1;i++)
{ printf("\n\n Enter integer value for element no.%d : ",i+1);
scanf("%d",&a[i]);
}
bubble(a,n);
printf("\n\n Finally sorted array is: ");
for( i=0;i<=n-1;i++)
printf("%3d",a[i]);
system("pause");
} //end program.
-
Re: Need help writing a bubble sort with no loops
OK, I've just had a bit of fun refactoring your bubble sort and replacing the iteration with recursion. :)
Giving advice on this is a bit tricky without effectively doing it for you :( But I'll do my best.
The first thing you should do is split the bubble sort function into several, so you can disentangle the two loops.
Here is your code:
Code:
void bubble(int a[],int n)
{
for(int i=n-2;i>=0;i--) {
for(int j=0;j<=i;j++) {
if(a[j]>a[j+1]) {
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
I would recommend you start by taking the inner loop out and putting it into a new function. This function would need to have three parameters - the array a[], length of array n, and the value of i from the outer loop. Then call this function from the bubble function.
Once you have done this you will have two functions, each with just the one loop.
Then I'll give you tips on removing the loops... :)
-
Re: Need help writing a bubble sort with no loops
Ok ive tried, bare with me if theyre any stupid errors
Code:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void function(int a[],int n.length, int i){
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
void bubble(int a[],int n)
{
int i,j,t;
for(i=n-2;i>=0;i--)
{
function(int a,int n,int i);
}//end for 1.
}//end function.
int main()
{
int a[100],n,i;
//clrscr();
printf("\n\n Enter integer value for total no.s of elements to be sorted: ");
scanf("%d",&n);
for( i=0;i<=n-1;i++)
{ printf("\n\n Enter integer value for element no.%d : ",i+1);
scanf("%d",&a[i]);
}
bubble(a,n);
printf("\n\n Finally sorted array is: ");
for( i=0;i<=n-1;i++)
printf("%3d",a[i]);
system("pause");
} //end program.
-
Re: Need help writing a bubble sort with no loops
It's best to check it compiles and works properly at each stage of the refactoring :)
Tidying up the indentation would be nice too...
-
Re: Need help writing a bubble sort with no loops
I have a recursive selection sort, so im thinking i can make this code into a bubble sort, I'm not exactly sure what i should change or what the REAL difference is in the coding for the 2 types of sorting....
Code:
#include <iostream>
using namespace std;
const int MAX = 10;
void InnerLoop(int values[10], int& min, int j)
{
if(j != MAX)
{
if(values[min] > values[j])
{
min = j;
}
InnerLoop(values, min, j+1);
}
}
void OuterLoop(int values[10], int i)
{
int min = i;
int tempVal = 0;
if(i != MAX)
{
int j = i;
InnerLoop(values, min, j);
tempVal = values[i];
values[i] = values[min];
values[min] = tempVal;
OuterLoop(values, i+1);
}
}
int main()
{
int i = 0;
int values[10] = { 25, 3, 17, 12, 9, 22, 4, 16, 8, 56 };
cout << "Recursive selection sort \n\n";
cout << "values unsorted:\n";
for(int i = 0; i < MAX; i++)
cout << values[i] << "\n";
cout << "\nvalues sorted\n";
OuterLoop(values, i);
for(int i = 0; i < MAX; i++)
cout << values[i] << "\n";
cout << "\nDone\n";
system("pause");
return 0;
}
-
Re: Need help writing a bubble sort with no loops
I know there needs to be a part where values[i] is compared to values[i-1] i jus dont kno where to place this condition... and also what i need to take out..
-
Re: Need help writing a bubble sort with no loops
Quote:
Originally Posted by
kristyy_mariee
I know there needs to be a part where values[i] is compared to values[i-1] i jus dont kno where to place this condition... and also what i need to take out..
I think your basic issue is that you more than likely did not work this out on paper first.
Before writing one line of code, you should have had an algorithm worked out recursively that sorts a range of numbers by comparing adjacent items, starting from the first item to the last. That is basically what a bubble sort is. Doing this requires no knowledge of C++, for loop syntax, or anything like that. You could even write the algorithm in English if you had to -- as long as each step is well-defined, that's what you initially needed to do.
If you did that, then the code to do this using C++, and quite honestly, any language that looks like C++ would have been trivial. Right now, you're trying to work this all out by writing a program, which is the wrong way to go about doing this.
Regards,
Paul McKenzie
-
Re: Need help writing a bubble sort with no loops
As to the psuedo-code -- a bubble sort can be checked for termination by simply setting a boolean and checking if any swaps occurred. If that boolean suggests that no swaps occurred, then you know the list is sorted and its time to quit. If you've reached the end of the list, and the boolean suggests that you made a swap somewhere, then you know you have to go back to the start of the list and go through the list again.
You know that you can't use loops, so the first thing is to write a function that will call itself up until it reaches the end of the list. So to this function, you must pass a variable that denotes the number of items in the list, the current item you're looking at now, and of course, the array itself, and the boolean that tests if you need to keep sorting. You set this boolean to "I am sorted", and then call the swapper recursive function from the first item (item 0).
Then you do some checks in this "swapper" function:
1) If you are at the last item, determine whether you need to start the swapper from item 0 again (check the boolean for "I am not sorted"), or quit swapper because you know that nothing was swapped and you've sorted the list successfully (check the boolean to see if "I am sorted"). If you need to go back to the beginning, then you also need to set the boolean to "I am sorted".
2) If you are not at the last item , determine if you need to swap the item and the one next to it. If so, set the boolean to "I am not sorted", swap the items, and then call the "swapper" recursion with the next item number.
See, no loops -- all I do is call this "swapper" and I return from everything when that boolean determines nothing was swapped, and I went through the entire list.
Now is this right? I don't know for sure -- maybe I missed something. The point is I can go through it by hand, draw it up on paper, and see if it does what I expect it to do by trying a small number of items. Once I make the corrections to the above, then and only then do I consider how to write this using C++.
At least I have some sort of blueprint in front of me that seems to do the job in terms of psuedo-code. Again, the trick is to draw this up on paper without loops.
Regards,
Paul McKenzie
-
Re: Need help writing a bubble sort with no loops
Paul's suggestion to write the recursive algorithm from scratch would obviously work. But given that you already have a working version, but one which uses loops, it is easier to refactor this working version into a form which uses recursion to simulate the loops. This is the approach I was taking when I suggested splitting the original bubble function into two functions, each with one loop. Each of these functions can then be simply refactored to replace the loop with a recursive call to the function itself.
So with that in mind, do you have a version of this code (your initial refactoring) which compiles?
Quote:
Originally Posted by
kristyy_mariee
Code:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void function(int a[],int n.length, int i){
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
void bubble(int a[],int n)
{
int i,j,t;
for(i=n-2;i>=0;i--)
{
function(int a,int n,int i);
}//end for 1.
}//end function.
int main()
{
int a[100],n,i;
//clrscr();
printf("\n\n Enter integer value for total no.s of elements to be sorted: ");
scanf("%d",&n);
for( i=0;i<=n-1;i++)
{ printf("\n\n Enter integer value for element no.%d : ",i+1);
scanf("%d",&a[i]);
}
bubble(a,n);
printf("\n\n Finally sorted array is: ");
for( i=0;i<=n-1;i++)
printf("%3d",a[i]);
system("pause");
} //end program.
Once you do you can replace the loops in both 'function' and 'bubble' with recursive calls.
This is a brief outline as to how this is done.
Suppose we have this (pseudo)code:
Code:
void func(arguments)
{
for (init_loop_variable; check_loop_variable; update_loop_variable) {
stuff_to_do(arguments, loop_variable(?));
}
}
where init_loop_variable, check_loop_variable and update_loop_variable are appropriate expressions for the relevant parts of the for statement, and 'arguments' is whatever arguments the function takes. I have put a ? mark after loop_variable in the argument list for stuff_to_do as it may or may not be needed in any particular case.
This is equivalent to:
Code:
void func(arguments)
{
init_loop_variable;
for (;check_loop_variable;) {
stuff_to_do(arguments, loop_variable(?));
update_loop_variable;
}
}
We now need to split this into two functions
Code:
void func(arguments)
{
init_loop_variable;
func2(arguments, loop_variable);
}
void func2(arguments, loop_variable)
{
for (;check_loop_variable;) {
stuff_to_do(arguments, loop_variable(?));
update_loop_variable;
}
}
The new function (func2) can then be refactored to replace the loop with recursive calls to func2 itself:
Code:
void func2(arguments, loop_variable)
{
if (check_loop_variable) {
stuff_to_do(arguments, loop_variable(?));
update_loop_variable;
func2(arguments, loop_variable);
}
}
We now have code which is exactly equivalent to the original version, which had a for loop, but which uses recursion to simulate the for loop.
This procedure can be done on any function which consists of a single loop - I used a for loop for the example but while loops and do/while loops can be turned into for loops so are also covered.
-
Re: Need help writing a bubble sort with no loops
Ok i only just saw the last two replies I did this today its a bit off but i tried.
Ok this is what ive done so far..
Code:
#include <iostream>
using namespace::std;
int bubblesort( int arrayf[], int size);
int bubblesort( int arrayf[], int size)
{
int temp;
int i=0;
arrayf[size];
bool swapped = false;
if (arrayf[i]>arrayf[i+1])
{
swapped = true;
temp = arrayf[i];
arrayf[i] = arrayf[i+1];
arrayf[i+1] = temp;
bubblesort(arrayf,0);
cout<<arrayf[i];
}
else {
if (arrayf[i]>arrayf[size-1])
{
bubblesort(arrayf, size--);
}
}
}
int main()
{
int j = 15;
int arr[j];
bubblesort(arr, j);
getchar();
return 0;
}
-
Re: Need help writing a bubble sort with no loops
This is not accomplishing anything.
-
Re: Need help writing a bubble sort with no loops
Quote:
Originally Posted by
kristyy_mariee
Ok i only just saw the last two replies I did this today its a bit off but i tried.
Ok this is what ive done so far..
First, you should pass the array, the size, and the current index. So right at the start, your bubblesort() function doesn't have enough information to know where to start the comparison, and whether to finish or not.
Code:
int bubblesort( int arrayf[], int currentItem, int arraySize, bool isSwapped)
{
//... stuff missing...
// somewhere code like this will be made to test the items
if (arrayf[currentItem] > arrayf[currentItem + 1] )
{
// stuff to do
}
// more stuff to do...
//...
// somewhere, a call like this will be made to compare the next two items
bubblesort( arrayf, currentItem + 1, arraySize, isSwapped );
}
int main()
{
int arr[] = { 10, 2, 45, 6, 7, 1, -3, 44, 3, 0, 12, 8 };
const int numValues = sizeof(arr) / sizeof(arr[0]);
bool bSwapped = false;
bubblesort(arr, 0, numValues, bSwapped); // start from the first item
}
The main() function is complete. There is nothing else to add to get the bubblesort to start. Also note the dummy data, and the way I computed the number of items so that you are not stuck on 15 items.
The part you need to fill in is the function, and what I described is how you should be proceeding.
The bubblesort function knows the array, it knows the current item, it knows the array size, and it knows when the items are swapped. So you know everything you need to know as to whether to stop sorting or keep sorting, or go back to the top of the array and start over, etc. I just put in two random lines of code as to a hint of what needs to be done. There is a lot of missing code, for example, checking if you're at the end of the array, you need to decide whether to quit or to go back to the beginning.
So go through this by hand.
The first thing you do is determine if currentItem is at the end of the array. If not, then you will be testing items 0 and 1 (10 and 2). They are out of sequence, so you a) swap them and b) set the bSwapped to true c) call bubblesort again to go to the next two items. Etc...
Regards,
Paul McKenzie
-
Re: Need help writing a bubble sort with no loops
I had to include a file as its what my assignment is asking for... I changed my variables to yours and made some changes to the code but its still outputting bogus numbers, do i still need more code?
Code:
#include <iostream>
#include <fstream>
using namespace::std;
int bubblesort( int arrayf[], int currentitem, int arraysize, bool isswapped);
int bubblesort( int arrayf[], int currentitem, int arraysize, bool isswapped)
{
int temp;
if (arrayf[currentitem]!=arraysize){ //checking for end of array
if (arrayf[currentitem]>arrayf[currentitem+1])//swapping if smaller
{
temp = arrayf[currentitem];
arrayf[currentitem] = arrayf[currentitem+1];
arrayf[currentitem+1] = temp;
isswapped = true;
//else {
//if(arrayf[currentitem]<arrayf[currentitem+1])
//isswapped=false;
bubblesort(arrayf,currentitem+1,arraysize-1,isswapped);
}
}
}
int main()
{
int j = 15;
int arr[j];
const int numValues = sizeof(arr) / sizeof(arr[0]);
//opens file
ifstream myfile ("integers.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{cout<<"unsorted";
for(j=0;j<=14;j++){
myfile>>arr[j];
cout << arr[j] << endl;
}
bool bSwapped = false;
bubblesort(arr, 0, numValues, bSwapped); // start from the first item
cout<<arr[j];
myfile.close();
getchar();
return 0;
}
}}
-
Re: Need help writing a bubble sort with no loops
Quote:
Originally Posted by
kristyy_mariee
I had to include a file as its what my assignment is asking for... I changed my variables to yours and made some changes to the code but its still outputting bogus numbers, do i still need more code?
1) Are you using a debugger to debug your code? You're supposed to be debugging your code and see where it goes wrong.
2) Start with 3 or 4 numbers, not 15. If you can't get it to work with 3 or 4 numbers, then trying to do 15 will just cause you to lose track.
3)
Code:
if (arrayf[currentitem]!=arraysize){ //checking for end of array
This check is not correct. If currentItem is arraysize-1, then you know you've reached the end of the array. The reason is that you're comparing pairs, and the last pair that will be compared are
Code:
arraysize-2, arraysize-1
4) Why are you calling bubblesort() this way?
Code:
bubblesort(arrayf,currentitem+1, arraysize-1,isswapped);
Why are you calling it with arraysize-1? Do you know that the original arraysize is practically the entire driving force with the recursion? So why are you changing it? Just keep passing arraysize.
Otherwise, what you end up doing is starting the sort in the middle of the array, and you're making your list smaller by changing arraysize. You're making your list disappear at both ends, the beginning and the end, for each call to bubblesort(), and that is not correct.
Regards,
Paul McKenzie
-
Re: Need help writing a bubble sort with no loops
Also, why not check for the end of the array first and act upon whether you've reached the end? Then the code becomes much more simple and easier to understand:
Code:
bubblesort(array, currentItem, numberofitems, swapped)
...
Have I reached the end of the array?
Yes ->
Check if swapped is true.
Yes -> start bubble sort at the beginning again
set isSwapped to false
call bubblesort starting at item 0.
No -> Just return. Everything is sorted.
No, not reached the end of the array ->
Check if current item and next item needs to be swapped.
Yes -> they need to be swapped
Swap them.
Set isSwapped to true.
Call bubblesort with next item (item + 1)
No -> they do not need to be swapped
Call bubblesort with next item (item + 1)
That is it. That is practically the entire function without writing it in C++. The goal now is can you follow it? Again, start with 4 numbers that are not sorted. Go through this by hand if you have to.
And just to let you know, the difference between this version of bubblesort and Peter's version is that his version unconditionally loops N*N times, even if the items are already sorted (where N is the number of items). Nothing wrong with that, as that is how the bubble sort is described in most texts as far as the number of comparisons to be made. This version does the same thing (compares adjacent pairs), with the difference being it knows when to stop sorting by checking a boolean "swapped" value. So it has just a bit of an optimization added to it.
Regards,
Paul McKenzie
-
Re: Need help writing a bubble sort with no loops
Check if swapped is true.
Yes -> start bubble sort at the beginning again
set isSwapped to false
call bubblesort starting at item 0.
No -> Just return. Everything is sorted.
Code:
if (arrayf[currentitem]=arraysize-1){ //checking for end of array
if (isswapped==true){
isswapped=false;
bubblesort(arrayf,0,arraysize,isswapped);
}
else
{
if (isswapped==false)
return bubblesort(arrayf,currentitem+1,arraysize,isswapped);
}
}
is the same as?
-
Re: Need help writing a bubble sort with no loops
-
Re: Need help writing a bubble sort with no loops
No, it is not OK. It also would help if you properly formatted your code.
Code:
if (arrayf[currentitem]=arraysize-1)
There are several things wrong. First, why are you comparing the contents of the last item to the sizeof the array - 1? You're supposed to compare the index of the item and see if it is the last one.
What if the last item in the array is -1000, and the number of items in the array is 20? Are you going to compare -1000 to 19? Of course not. You want to compare the index of the current item, i.e. 19 to the number of items-1, i.e. 19.
Second, to compare items, you use ==, not =.
Code:
if ( currentitem == arraysize - 1 )
That is what that line is supposed to be.
Now this:
Quote:
No -> Just return. Everything is sorted.
This should be self-explanatory, but somehow you don't understand. Where do you do a return if everything is sorted? Instead, you do this right away:
Code:
if (isswapped==false)
return bubblesort(arrayf,currentitem+1,arraysize,isswapped);
}
}
Where in the steps is it outlined that if you're on next to the last item, and no items are swapped, to keep calling bubblesort()? You have to follow the steps carefully.
I practically handed you the answer in my last email, but I just didn't write it in C++. There's little I can do more for you -- at this point it should have been very easy to understand the steps (again, you don't need to write a program -- follow the steps by hand with just a few unsorted numbers).
Regards,
Paul McKenzie
-
Re: Need help writing a bubble sort with no loops
Just a wild question: Is the size of the array you are supposed to sort known when you compile?
Because that is the only explanation that I can find that would make sense of your no loop requirement, and would be implementable in a logical fashion...
In a word: It would change EVERYTHING.