2nd try

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 17
Reputation: Trckst3 is an unknown quantity at this point 
Solved Threads: 0
Trckst3 Trckst3 is offline Offline
Newbie Poster

2nd try

 
0
  #1
Apr 5th, 2008
Hey I'm trying to get this code going but i can't get my if(choice==2) to output anything any ideas?

  
#include <iostream>
#include <iomanip>
#include <algorithm>

using namespace std;

int main()
{
    int i,num[20],n,j,choice,tmp;

    cout<< "Please enter 20 integers";
    for (i=0; i<20; i++)
    {
        cout<<"\nEnter next value:";
        cin>>num[i];

    }

    cout<<"\n1.Display original data.\n";
    cout<<"2.Sort the data into descending order\n";
    cout<<"3.Display the sorted data (Only if you've already sorted)\n";
    cout<<"4.Get the address of the first element of array.\n\n";
    cin>>choice;
	
    if (choice==1)
    {

        for (i=0; i<20; i++)
        {
            cout<<"\n"<<num[i]<<endl;
        }
		return 0;
	}
	if (choice==2)
        {
            for (i=0; i<n-1; i++)
            {
                for (j=0; j<n-1-i; j++)
				{
                    if (num[j+1] < num[j])    /* compare the two neighbors */
                    {
                        tmp = num[j];         /* swap a[j] and a[j+1]      */
                        num[j] = num[j+1];
                        num[j+1] = tmp;

                        cout<<"Here are your numbers:"<<tmp<<endl;
                    }
				}
            }
		}
    
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 807
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: 2nd try

 
0
  #2
Apr 5th, 2008
  
	
    if (choice==1)
    {
        //HERE CHOICE = 1
        for (i=0; i<20; i++)
        {
            cout<<"\n"<<num[i]<<endl;
        }
		return 0;
	}
        // HERE CHOICE STILL EQUALS 1!
	if (choice==2)
        {
            for (i=0; i<n-1; i++)
            {
                for (j=0; j<n-1-i; j++)
				{
                    if (num[j+1] < num[j])    /* compare the two neighbors */
                    {
                        tmp = num[j];         /* swap a[j] and a[j+1]      */
                        num[j] = num[j+1];
                        num[j+1] = tmp;

                        cout<<"Here are your numbers:"<<tmp<<endl;
                    }
				}
            }
		}
    
}

Please see my comments in your code for a hint...
Last edited by darkagn; Apr 5th, 2008 at 3:02 am.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 17
Reputation: Trckst3 is an unknown quantity at this point 
Solved Threads: 0
Trckst3 Trckst3 is offline Offline
Newbie Poster

Re: 2nd try

 
0
  #3
Apr 5th, 2008
Ok i figured out part of my problem i didn't set n equal 20. Though I'm still not getting an output of the users input in the correct order...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 17
Reputation: Trckst3 is an unknown quantity at this point 
Solved Threads: 0
Trckst3 Trckst3 is offline Offline
Newbie Poster

Re: 2nd try

 
0
  #4
Apr 5th, 2008
Ok then stupid question to end if(choice==1) if add another } would that close it off so that it owuldn't affect if(2). Cause i'm honestly stumped on that one
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 807
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: 2nd try

 
0
  #5
Apr 5th, 2008
Yes, but to make your code easier to follow it might be worth writing "else if (choice==2)"...
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 17
Reputation: Trckst3 is an unknown quantity at this point 
Solved Threads: 0
Trckst3 Trckst3 is offline Offline
Newbie Poster

Re: 2nd try

 
0
  #6
Apr 5th, 2008
Hmm... Ok I've finally got it running but when it outputs it's only outputting 11 numbers and it should be 20 did i mess up the bubble sort to the point where i would lose data?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 807
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: 2nd try

 
0
  #7
Apr 5th, 2008
You have two loops

  1. for (i=0; i<n-1; i++)
  2. {
  3. for (j=0; j<n-1-i; j++)
  4. {

but I can't see where you initialise n. Try printing the value of n to the console just before that line and see what happens...

EDIT: Also, I think you will not need the "-1" in each of those loops, but we'll come to that once we work out what is happening with n.
Last edited by darkagn; Apr 5th, 2008 at 3:20 am.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 17
Reputation: Trckst3 is an unknown quantity at this point 
Solved Threads: 0
Trckst3 Trckst3 is offline Offline
Newbie Poster

Re: 2nd try

 
0
  #8
Apr 5th, 2008
I initialized n here:

1.n=20
2.for (i=0; i<n-1; i++)
3.{
4.for (j=0; j<n-1-i; j++)
5.{
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 17
Reputation: Trckst3 is an unknown quantity at this point 
Solved Threads: 0
Trckst3 Trckst3 is offline Offline
Newbie Poster

Re: 2nd try

 
0
  #9
Apr 5th, 2008
Oh dang I almost got it i have it actually working right i initialized n and then took out the -1's and it works but it doesn't show the last two numbers of the 20 inputs that are sorted only shows 18

--Okk... I lied i ran it again and used big numbers like 120 and such and it doesn't output right at all it just kinda puts them where it wants them instead of sorting them
Last edited by Trckst3; Apr 5th, 2008 at 3:28 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 585 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC