| | |
2nd try
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 17
Reputation:
Solved Threads: 0
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; } } } } }
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. You have two loops
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.
c++ Syntax (Toggle Plain Text)
for (i=0; i<n-1; i++) { for (j=0; j<n-1-i; j++) {
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. •
•
Join Date: Apr 2008
Posts: 17
Reputation:
Solved Threads: 0
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
--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.
![]() |
Similar Threads
- Computer won't recognize 2nd Hard Drive (Windows NT / 2000 / XP)
- 2nd Hard Drive installation (Storage)
- Installing a 2nd Harddrive (Storage)
Other Threads in the C++ Forum
- Previous Thread: Why does it loop
- Next Thread: finiding min in array till satisfies condition explained in prbm stmnt
Views: 585 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





