| | |
HELP!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 17
Reputation:
Solved Threads: 0
Hi guys I'm new here and basically new to programming. Unfortuanetly i'm probably gonna be quite annoying to you just because you guys know what you're doing and I'm like the little slow train that's trying to catch up. Though hopefully you'll help... Here's my situation I'm supposed to write a program, which I've already started and am halfway done with, what the program is supposed to do is create an array from user input and either 1. just display their input. 2.Just sort the array. 3. show the sorted array (only if they've already sorted. 4. And then show the address of the first element of the array. I'm stuck on the sorting cause I thought I had the bubble sort alorithm right but it doesn't output when i try it... Any ideas? Or any way to make it simpler.
//ECET 164 19926
//Lab 11
//This program takes 20 inputs from the user, and gives the choice to show sorted data,
//sort the data into descending order, display the sorted data, and display the address of the first element.
//ECET 164 19926
//Lab 11
//This program takes 20 inputs from the user, and gives the choice to show sorted data,
//sort the data into descending order, display the sorted data, and display the address of the first element.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; #include <iomanip> #include <algorithm> int main() { int i,num[20],sec[20],j,choice; 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; } if (choice==2){ { int i,temp,thing; for(i=0; i < i-1; i++) { thing= i; for (j=i+1; j<i; j++) { if (num[j] < num[thing]) thing=j; } temp = num[i]; num[i]= num[j]; num[j] = temp; cout<<"Here are your numbers:"<<temp<<endl; } }}}}
Last edited by Ancient Dragon; Apr 4th, 2008 at 12:15 pm. Reason: replace ICODE with CODE tags
•
•
Join Date: Mar 2008
Posts: 64
Reputation:
Solved Threads: 5
after choice==2 you have two brackets, why? I'll continue to comb through, just a suggestion, if you didn't have it formatted in your .cpp file, you might want to read up on style a bit. When you ask for the choices (1-4), and the user enters, none of it works. I'll edit it in a second when I find out why. Also, this line:
Near the middle, I usually find it best to declare all of your variables in one place. Also, you declare the variable i twice, which for me usually results in a run-error. Can you tell me what compiler you are using?
C++ Syntax (Toggle Plain Text)
int i,temp,thing;
Last edited by Maulth; Apr 4th, 2008 at 12:31 pm.
line 49: for your own sanity and the benefit of others who must read and understand your program put those } braces on separate lines and in line with the opening {. Don't be afraid to make liberal use of white space in your code.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2008
Posts: 17
Reputation:
Solved Threads: 0
Yeah those brackets have been driving me nut just wasn't sure how to do it cause I've seen my professor do it both ways. Though I'm thinking I'm just going to move them and if I lose points so be it, it'll help out some...And I did have it somewhat lined up in my .cpp file I'm not sure why somethings moved. Though I'll admit my format does need some work...
•
•
Join Date: Apr 2008
Posts: 17
Reputation:
Solved Threads: 0
Hey sry I'm having to get ready for my digital circuit class. I'm using Visual Studio 2005 as my compiler. I'm gonna have to head to class but I'll be back in about an hour and half to two hours depending on traffic. So don't think I'm bailing on you guys. And the reason I ihad sec[20] was because my professor said to declare two arrays. Though I never knew why...
•
•
Join Date: Apr 2008
Posts: 129
Reputation:
Solved Threads: 22
how to make menu...
bubble sort
CPP Syntax (Toggle Plain Text)
#include <iostream> using namespace std; unsigned int menu(); int main() { for (;;) { switch (menu()) { case 0: return 0; case 1: //some function to dispaly input break; case 2: //some function to sort break; case 3: //some function to show array break; case 4: //some function to show adress of 1st el. break; } } return 0; } unsigned int menu() { unsigned int c = -1; while (c > 4) { cout <<"<0> Exit\n" "<1> Display their input\n" "<2> Sort the array\n" "<3> Show the sorted array\n" "<4> Show the address of the first element\n"; cin >> c; if (c > 4) { cout << "Try again\n"; } } return c; }
CPP Syntax (Toggle Plain Text)
void bubble_sort() { bool isChange = true; while (isChange) { isChange = false; for (int i = 0; i < size_of_array-1; ++i) { if (array[i] > array[i+1]) { array[i] ^= array[i+1] ^= array[i] ^= array[i+1]; isChange = true; } } } }
Last edited by ivailosp; Apr 4th, 2008 at 12:51 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: help me check a simple game's error ^^
- Next Thread: Quick question
Views: 566 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bmp 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 java lib library lines linkedlist linker loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






