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.

``````#include <iostream>
using namespace std;
#include <iomanip>
#include <algorithm>

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

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;
} }}}}``````
4
Contributors
8
Replies
9
Views
9 Years
Discussion Span
Last Post by ivailosp

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:

``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?

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.

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...

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....

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.

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.

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...

``````#include <iostream>
using namespace std;

int main() {
for (;;) {
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 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;
}``````

bubble sort

``````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;
}
}
}
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.