writing a program that is not allowed to have loops.

This is a discussion on writing a program that is not allowed to have loops. within the C++ Programming forums, part of the General Programming Boards category; Integers will be read from the file and should be bubble sorted.. its a lot of if statements to avoid ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    34

    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. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,415
    O_o

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

    Soma

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    I didnt kno my question asked for somebody to take time out to bull........ on here. but thanks.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,677
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    Quote Originally Posted by grumpy View Post
    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. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A few program questions regarding while loops
    By tigerfan88 in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2008, 05:41 AM
  2. program loops twice
    By ruffle in forum C Programming
    Replies: 6
    Last Post: 11-17-2005, 01:02 PM
  3. Writing forever loops - using for(;;) or while(1)?
    By hzmonte in forum C Programming
    Replies: 8
    Last Post: 07-15-2005, 10:02 PM
  4. payroll program with loops
    By indigo0086 in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2002, 10:49 PM
  5. simple program on loops
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-11-2002, 11:24 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21