Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,482 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,741 people online right now. Registration is fast and FREE... Join Now!




Single Subscripted Array

 
Reply to this topicStart new topic

Single Subscripted Array

f26434
28 Jan, 2008 - 07:20 AM
Post #1

New D.I.C Head
*

Joined: 28 Nov, 2007
Posts: 8


My Contributions
A simple bubble sort algorithm is inefficient for large arrays because it continues to pass
through all elements of the array even when many of them have been sorted. Find out about
simple bubble sort algorithms (easily found on web or programming books). Write a routine
to sort the ten-number list shown in ‘Sample Screen Output’ below, and then make the
following modifications to improve the performance of the bubble sort:
a) After the first pass, the largest number is guaranteed to be in the highest-numbered element
of the array; after the second pass, the two highest numbers are “in place,” and so on. Instead
of making nine comparisons on every pass, modify the bubble sort to make eight comparisons
on the second pass, seven on the third pass, and so on.
cool.gif The data in the array may already be in the proper order or near-proper order, so why make
nine passes if fewer will suffice? Modify the sort to check at the end of each pass if any swaps
have been made. If none have been made, then the data must already be in the proper order, so
the program should terminate. If swaps have been made, then at least one more pass is
needed.

I AM NOT GETTING THIS REQUIRED OUTPUT AS:

Data items in original order
2 6 4 8 10 12 89 68 45 37
After pass 0: 2 4 6 8 10 12 68 45 37 89
After pass 1: 2 4 6 8 10 12 45 37 68
After pass 2: 2 4 6 8 10 12 37 45
After pass 3: 2 4 6 8 10 12 37
After pass 4: 2 4 6 8 10 12
After pass 5: 2 4 6 8 10
After pass 6: 2 4 6 8
After pass 7: 2 4 6
After pass 8: 2 4
Data items in ascending order
2 4 6 8 10 12 37 45 68 89
Number of comparisons = 45







THIS IS WHAT I HAVE DONE SO FAR::::
CODE

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

using std::cout;
using std::cin;
using std::endl;
using std::setw;

int main()
{
    const int size = 10;
    int a[size] = {2,6,4,8,10,12,89,68,45,37};
    int numberofcomparisons = 0;
    //int compare;
    //bool swapcheck = true;
      
    cout << "Data items in original order\n";
    
    for (int i =0; i<size; ++i)
    cout << setw(4) << a[i];
    
    cout <<"\n\n";
    

    for (int count=0; count < 9; count++)
    
{
        int temp = 0;
        for (int p=0; p < 9-count; p++)
{
           int q = p + 1;
           if (a[p] > a[q])
{
               temp = a[q];
               a[q] = a[p];
               a[p] = temp;
              
}
numberofcomparisons = numberofcomparisons +1;
}


for (int x = 0; x<size-count; ++x)
cout << setw(4)<<a[x];

}

cout << "\nData in ascending order\n";

for (int j =0; j <size; ++j)
cout << setw(4) << a[j];

cout << "\n\nNumber of comparisons = " << numberofcomparisons <<endl;

    
printf ("\nPress ENTER to continue.\n");
  getchar ();


              
return 0;
}

User is offlineProfile CardPM
+Quote Post

baavgai
RE: Single Subscripted Array
28 Jan, 2008 - 08:08 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,031



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
You know, the program is entirely correct.

The only thing it's missing is a linefeed in the right place. If you have to ask for that, I'd be very surprised if you actually wrote this.

User is online!Profile CardPM
+Quote Post

f26434
RE: Single Subscripted Array
28 Jan, 2008 - 09:08 AM
Post #3

New D.I.C Head
*

Joined: 28 Nov, 2007
Posts: 8


My Contributions
Well I know that I need a linefeed, but I am totally confused at the moment and cannot think properly.

I tried inserting "\n" and also << endl, but instead, the results were vertically alligned and did not look good.

It will be great if you help me out with this.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Single Subscripted Array
28 Jan, 2008 - 09:24 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,031



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Fair enough.

CODE

for (int count=0; count < 9; count++) {
   // your sorting pass
   ...
   //where you print out the current array, minus the count
   for (int x = 0; x<size-count; ++x)
      cout << setw(4)<<a[x];
   // after this, you want a linefeed to finish it off
   cout << endl;
}


Hope this helps.

User is online!Profile CardPM
+Quote Post

f26434
RE: Single Subscripted Array
28 Jan, 2008 - 09:29 AM
Post #5

New D.I.C Head
*

Joined: 28 Nov, 2007
Posts: 8


My Contributions
Thanx 4 ur help. Much appreciated. I was adding endl; , but it was on the sam line I did not think of seperate cout<<endl;

Thanx very much.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 07:03PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month