Flash Flash Revolution: Community Forums

Flash Flash Revolution: Community Forums (http://www.flashflashrevolution.com/vbz/index.php)
-   Homework & Help (http://www.flashflashrevolution.com/vbz/forumdisplay.php?f=68)
-   -   programming question (http://www.flashflashrevolution.com/vbz/showthread.php?t=144852)

iCeCuBEz v2 07-18-2016 03:37 PM

programming question
 
My assignment is to use this model bubblesort program.

It currently is set up to bubble sort integers but for my assignment I need to bubble sort an array of characters.

How do I modify the code so that it can take an array of characters instead of integers? I've been pulling my hair out for hours trying to figure this out and I've pretty much given up.

I suck at programming I hate it and I'm pissed I have to take this class.

here's the code

#include <iostream>
using namespace std;
void input(int ulist[], int &numberOfElements);
void Bubblesort(int ulist[], int slist[], int numberOfElements);
void print(int list[], int numberOfElements);


const int MAX_SIZE = 10;
void main()
{
int numberOfElements;
int ulist[MAX_SIZE], slist[MAX_SIZE];
input(ulist, numberOfElements);
cout << "Unsorted";
print(ulist, numberOfElements);
cout << "Sorted";
Bubblesort(ulist, slist, numberOfElements);
print(slist, numberOfElements);
system("pause");
}
void input(int ulist[], int& numberOfElements)
{
int i = 0, value;
cout << "enter value : \n";
cin >> value;

while (i < MAX_SIZE && value != -999)
{
ulist[i] = value;
i++;
if (i<MAX_SIZE)
{
cin >> value;
}
}
numberOfElements = i;
}
void Bubblesort(int unlist[], int sortlist[], int numberOfElements)
{
int temp;
for (int i = 0; i < numberOfElements; i++)
sortlist[i] = unlist[i];

for (int i = 0; i < numberOfElements; i++)
{
for (int j = 0; j<numberOfElements - 1; j++)
{
if (sortlist[j]>sortlist[j + 1])
{
temp = sortlist[j];
sortlist[j] = sortlist[j + 1];
sortlist[j + 1] = temp;
}
}
}
}
void print(int list[], int numberOfElements)
{
int sum = 0;
cout << " list of numbers are : \n";
for (int i = 0; i < numberOfElements; ++i)
{
cout << list[i] << '\n';
sum = sum + list[i];
}

double average = (double)sum / (double)numberOfElements;
cout << "\n\n";
cout << "Average is " << average << "\n";
}

Dinglesberry 07-18-2016 04:00 PM

Re: programming question
 
There's literally nothing to do with the code - the key is, how are characters actually represented?

Perhaps your first approach would be.. Are characters maybe actually integers? Does 'a' correspond to a specific int? Could 'b' perhaps be that int + 1, and 'c' that int + 2? Does the integer value of characters sequentially increase (here's a tip, it does).

The only different is, rather than a int[] array, the function would take a char[] array. Good luck.

iCeCuBEz v2 07-18-2016 04:04 PM

Re: programming question
 
i replaced the int variables with char but still the only data the program accepts are numbers. The second I type a character the program crashes

and if I do input numbers as characters instead the program does execute however the final sorted and unsorted lists are represented as weird symbols.

I created my own program that sorts characters in order but I have to put in each letter individually. Is there a way so I could type the whole string of characters initially and then have it spit it out in order?

Dinglesberry 07-18-2016 04:05 PM

Re: programming question
 
Also a tip, you can't be taking it so literally. If you decide to take programming further, you'll get to learn about the cooler things like comparators - if you really think about it, what differentiates different data types? Did you know a string can also be represented as an array of characters?

Start thinking things like, what's really the different between

String test = "Hello" and char[] test = ['H', 'e', 'l', 'l', 'o']? Is there any difference? Could perhaps the C++ function to print a string just be a loop that goes through the char array and prints every element? Maybe it is ;)

iCeCuBEz v2 07-18-2016 04:14 PM

Re: programming question
 
i couldnt figure out how to convert a string into an array of characters I looked all over the internet and I couldn't find an answer and it drove me nuts.

I might just take a zero for this assignment I just can't figure it out

Dinglesberry 07-18-2016 04:19 PM

Re: programming question
 
http://stackoverflow.com/questions/1...har-array-in-c

You should be able to get a string with cin, even so, you are sure it doesn't work if you ensure the types are correct?

It should just be a matter of iterating through the char array... Make sure you aren't messing up types somewhere.

Dinglesberry 07-18-2016 04:23 PM

Re: programming question
 
Bro check this out too, for example you do, right inside the bubble sort method, "int temp" (it's like the first line of the function). You use an int to save the current value of the array (so you can put it back to the right spot after you swap), but the arrays are now chars, are they not?

This is just one example of ensuring type is correct, and it's VERY important in type safe languages like java, c++ etc. you might enjoy PHP if you aren't a fan of this, but you should wait until you are more familiar with objects and such before starting php :p

EDIT: I'm on my PC now, was on my phone earlier, I'm going to point out specifically what I mean, in this case, and I'll try to explain the bubble sort for you:

Code:

void Bubblesort(int unlist[], int sortlist[], int numberOfElements)
{
int temp;
for (int i = 0; i < numberOfElements; i++)
sortlist[i] = unlist[i];

for (int i = 0; i < numberOfElements; i++)
{
for (int j = 0; j<numberOfElements - 1; j++)
{
if (sortlist[j]>sortlist[j + 1])
{
temp = sortlist[j];
sortlist[j] = sortlist[j + 1];
sortlist[j + 1] = temp;

I'm bolding the key part for you. What you are doing is, you pass the algorithm an unsorted array, as well as an array to hold the sorted list, which I'm going to assume is just an empty array of equal size (might even be just a copy, would have the same result). EDIT: sortlist[i] = unlist[i]; it is a copy, which asks the question why you would have to pass the function a sorted list at all instead of just creating one inside the function itself, but that's not really relevant, works either way...

Bubblesort is not the most efficient algorithm, but what you are doing is: You go through every element of the list, 0 to list.length, one by one. For each of them, you check if the current element (sortlist[j]) is greater than the NEXT thing in the array (sortlist[j + 1]). If it is, lets say [ 3, 2 .... ] (and we are looking at 3), obviously you need to switch their position, so:

1. You save the current value (the element you are looking at... remember, we are stepping through the array, item by item) temp = sortlist[j];
2. You swap the values: We set the current thing we are looking at to the value of the NEXT thing (e.g. [ 3, 2 ...] becomes [ 2, 2 ....].
3. OH SHIT, WHAT HAPPENED TO THE 3!?! OH WAIT, WE SAVED IT IN temp = sortlist[j];! BOOM, we set the NEXT element, j + 1, to whatever we saved in temp - sortlist[j + 1] = temp;
4. Sweet, now the array is like [ 2, 3... ], now we step forward and are comparing the next element.. [2, 3, 6 ....], since we did j++ in the loop, we are comparing 3 and 6 etc...

The bubble sort goes through the entire array.

When you think about how the algorithm works, its honestly trivial what data type the array sorts. Lets say the array is [ 'a', 'g', 's', 'b' ]... the comparisons and the way it executes would be the exact same, because a value like a is really just a number (lets arbitrarily say its like 56). If a is 56, then b is 57, etc.

igotrhythm 07-18-2016 05:40 PM

Re: programming question
 
Oh cool, so you're learning how to take an array of strings and alphabetize it via bubble sort. Neat stuff. I never learned how to do this in my C++ class.

Soundwave- 07-18-2016 11:24 PM

Re: programming question
 
Once upon a time, we had 16 bit computers.

Things were simply then. We had bytes, and we had integers.

That was it.

But then, the 32 bit age came along.

Now we have a problem. We have two types of integers, along with a byte: 16 bit and 32 bit. So we decided to call one of them "long", and one of them "short". We also came up with the general keyword "int".

This has made a lot of people very angry and been widely regarded as a bad move.

See, this "int" usually means "long", so that if somebody wanted an integer they didn't get a "short" when they expected a "long"; there's little harm in expecting a "short" and getting a "long". But sometimes, you'll get a "short" when you want a "long", because nobody *really* said what "int" was supposed to mean. Sometimes this even happens today (although you can pretty much count on getting a "long" these days).

Then we got to the new age of 64 bit. This age became so prevalent that 32 bit computers had to at least be able to handle 64 bit data.

We needed something long. Really long. Like longer than a long. So we came up with the "long long". That's for real a data type. But this is where things get really bad. See, a lot of people don't know about the "long long". They think the "long" is longer than the "int", and as long as the "long long". But more often than an "int" is a "short", a "long" is an "int". Which can be dangerous if you're expecting a "long long" from your "long" which is really an "int".

If you're confused. It's okay. There's literally nothing too important in the above paragraphs.

But I'm coming to a conclusion here.

It's about "short". See, "short" is really cool. "short" is small enough that you can fit two of them in a single computer register, but big enough that you can have a number for virtually every single character in text in any language imaginable.

People don't use "short" as a number very often anymore. It's not very useful. So we renamed "short", to "char", and decided to use it for characters.

"char" is literally a number as much as "int" is. Not even in the sense that you can assign numbers to characters. You can do that. No, this is in the sense that "char" acts like a number any every known way. It just *also* acts like a character, because we decided that's useful.



So you can replace "int" with "char" and expect everything to work.

Sort of. Input is a bitch.

I haven't found any information concerning how streams work with characters. I'm willing to bet "not well" is the answer.

Consider reading in single character strings. You should then just be able to do mystring[0] to get a character.

inDheart 07-19-2016 12:04 AM

Re: programming question
 
Quote:

Originally Posted by Soundwave- (Post 4454870)
People don't use "short" as a number very often anymore. It's not very useful. So we renamed "short", to "char", and decided to use it for characters.

https://en.wikipedia.org/wiki/C_data_types

tl;dr no, short is not char and your post is dangerously confused about data types. i'll even venture a guess that char and short are different sizes on your machine.

sizeof eliminates the chance of getting a short when you want a long or whatever it is you're going on about, and if you really want a guaranteed size you could use the stdint types (though more recently these may be falling out of style in favor of something newer?)

but again, all of that is tangential

OP, the SO link that Dinglesberry posted is probably the right place to start, as that will tell you to convert your cin inputs to C-style strings and then get you to copy them into a char array, which seems to be what you want. depending on your use (are you sorting letters in text?) you may want specifically unsigned chars.

Soundwave- 07-19-2016 08:17 AM

Re: programming question
 
Quote:

Originally Posted by inDheart (Post 4454883)
tl;dr no, short is not char and your post is dangerously confused about data types. i'll even venture a guess that char and short are different sizes on your machine.

The only error seems to be the part where I mistakenly assumed C and Java chars were analogous, which was actually strange to me as a mistake to make considering I'm well acquainted with the concept of a wide character, which exists precisely because C and Java chars, are, in fact not analogous.

Say a char is an integer is entirely correct from a C perspective and mostly correct from a C++ perspective, though it needn't necessarily be the case (which I think should be clear by my explanation of how fucked up C integers are) and also almost certainly not the case that a short is equally as long.

For correctness and tl;dr sake, a char is always a "byte", where a byte is defined as the smallest addressable data size. This will almost certainly be 8 bits except on embedded devices that have memory addressed in 16 bit chunks for speed and simplicity of architecture, and you know, whatever other weird exceptions out there.

And, for record's sake:


An incredibly unsurprising result, given the correction.

Dinglesberry 07-20-2016 09:50 AM

Re: programming question
 
The suspense is killing me... Did this guy go on to become a PROgrammer, and solve the traveling salesman problem?

Or did he retired?

xXOpkillerXx 07-20-2016 10:02 AM

Re: programming question
 
Quote:

Originally Posted by Dinglesberry (Post 4455225)
The suspense is killing me... Did this guy go on to become a PROgrammer, and solve the traveling salesman problem?

Or did he retired?

P=NP

Dinglesberry 07-20-2016 12:06 PM

Re: programming question
 
Quote:

Originally Posted by xXOpkillerXx (Post 4455231)
P=NP

I think he ended up getting it in O(n!) time.. regardless though, he got it, and that's what counts.

xXOpkillerXx 07-20-2016 12:11 PM

Re: programming question
 
Quote:

Originally Posted by Dinglesberry (Post 4455270)
I think he ended up getting it in O(n!) time.. regardless though, he got it, and that's what counts.

Who knows, maybe he only has an O(TREE(n)) solution !!

Dinglesberry 07-20-2016 02:29 PM

Re: programming question
 
Quote:

Originally Posted by xXOpkillerXx (Post 4455274)
Who knows, maybe he only has an O(TREE(n)) solution !!

Oh god, I heard he tried to implement a self-balancing tree, but it just kept spinning around faster and faster until it caught on fire :/

Soundwave- 07-20-2016 02:34 PM

Re: programming question
 
Quote:

Originally Posted by Dinglesberry (Post 4455373)
Oh god, I heard he tried to implement a self-balancing tree, but it just kept spinning around faster and faster until it caught on fire :/

Woah there. For that kind of problem you need a civil engineer.

reuben_tate 07-20-2016 03:56 PM

Re: programming question
 
Is catching on fire a constant-time operation?

AutotelicBrown 07-20-2016 04:24 PM

Re: programming question
 
Can you branch predict a multi-track drift?

xXOpkillerXx 07-20-2016 04:32 PM

Re: programming question
 
Quote:

Originally Posted by reuben_tate (Post 4455394)
Is catching on fire a constant-time operation?

Yeah since catching on fire is an instant action; it's either your are on fire or you aren't.


All times are GMT -5. The time now is 12:09 AM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution