HELP!

Please support our C++ advertiser: Intel Parallel Studio Home
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

HELP!

 
0
  #1
Apr 4th, 2008
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.

  1. #include <iostream>
  2. using namespace std;
  3. #include <iomanip>
  4. #include <algorithm>
  5.  
  6. int main()
  7. {
  8. int i,num[20],sec[20],j,choice;
  9.  
  10.  
  11. cout<< "Please enter 20 integers";
  12. for (i=0; i<20; i++)
  13.  
  14. {
  15. cout<<"\nEnter next value:";
  16. cin>>num[i];
  17.  
  18. }
  19.  
  20.  
  21.  
  22. cout<<"\n1.Display original data.\n";
  23. cout<<"2.Sort the data into descending order\n";
  24. cout<<"3.Display the sorted data (Only if you've already sorted)\n";
  25. cout<<"4.Get the address of the first element of array.\n\n";
  26. cin>>choice;
  27.  
  28. if (choice==1){
  29.  
  30. for (i=0; i<20; i++){
  31. cout<<"\n"<<num[i]<<endl;
  32. }
  33. if (choice==2){
  34. {
  35. int i,temp,thing;
  36.  
  37. for(i=0; i < i-1; i++)
  38. {
  39. thing= i;
  40. for (j=i+1; j<i; j++)
  41. {
  42. if (num[j] < num[thing])
  43. thing=j;
  44. }
  45. temp = num[i];
  46. num[i]= num[j];
  47. num[j] = temp;
  48. cout<<"Here are your numbers:"<<temp<<endl;
  49. } }}}}
Last edited by Ancient Dragon; Apr 4th, 2008 at 12:15 pm. Reason: replace ICODE with CODE tags
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 64
Reputation: Maulth is an unknown quantity at this point 
Solved Threads: 5
Maulth Maulth is offline Offline
Junior Poster in Training

Re: HELP!

 
0
  #2
Apr 4th, 2008
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:
  1. int i,temp,thing;
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?
Last edited by Maulth; Apr 4th, 2008 at 12:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,647
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: HELP!

 
0
  #3
Apr 4th, 2008
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.
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: HELP!

 
0
  #4
Apr 4th, 2008
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...
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: HELP!

 
0
  #5
Apr 4th, 2008
None of them work for you?! I know the last don't work cause I haven't made them yet. So I understand if no help on those I need to try them myself. Though option one works fine for me....
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 64
Reputation: Maulth is an unknown quantity at this point 
Solved Threads: 5
Maulth Maulth is offline Offline
Junior Poster in Training

Re: HELP!

 
0
  #6
Apr 4th, 2008
Let me retry, I was altering your code a bit to see what worked and what didn't. As ancient said, those brackets made my brain hurt :p
As I said above you may try and declare your variable only once >.< and all in one place. Also, sec[20] isn't used. Still debugging, I'll edit this again in a sec.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 64
Reputation: Maulth is an unknown quantity at this point 
Solved Threads: 5
Maulth Maulth is offline Offline
Junior Poster in Training

Re: HELP!

 
0
  #7
Apr 4th, 2008
I'm sorry I can't help, I'm getting ready to switch classes, I might be able to look on the next class. Goodluck with this though.
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: HELP!

 
0
  #8
Apr 4th, 2008
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...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: HELP!

 
0
  #9
Apr 4th, 2008
how to make menu...
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. unsigned int menu();
  5.  
  6. int main() {
  7. for (;;) {
  8. switch (menu()) {
  9. case 0:
  10. return 0;
  11. case 1:
  12. //some function to dispaly input
  13. break;
  14. case 2:
  15. //some function to sort
  16. break;
  17. case 3:
  18. //some function to show array
  19. break;
  20. case 4:
  21. //some function to show adress of 1st el.
  22. break;
  23. }
  24. }
  25. return 0;
  26. }
  27.  
  28. unsigned int menu() {
  29. unsigned int c = -1;
  30. while (c > 4) {
  31. cout <<"<0> Exit\n"
  32. "<1> Display their input\n"
  33. "<2> Sort the array\n"
  34. "<3> Show the sorted array\n"
  35. "<4> Show the address of the first element\n";
  36.  
  37. cin >> c;
  38. if (c > 4) {
  39. cout << "Try again\n";
  40. }
  41. }
  42. return c;
  43. }
bubble sort
  1. void bubble_sort() {
  2. bool isChange = true;
  3. while (isChange) {
  4. isChange = false;
  5. for (int i = 0; i < size_of_array-1; ++i) {
  6. if (array[i] > array[i+1]) {
  7. array[i] ^= array[i+1] ^= array[i] ^= array[i+1];
  8. isChange = true;
  9. }
  10. }
  11. }
  12. }
Last edited by ivailosp; Apr 4th, 2008 at 12:51 pm.
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum


Views: 566 | 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