Old 07-18-2016, 03:37 PM   #1
iCeCuBEz v2
XFD
FFR Veteran
 
iCeCuBEz v2's Avatar
 
Join Date: Mar 2008
Location: Connecticut
Age: 33
Posts: 4,924
Default 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";
}
__________________
I bring my math homework to church. It helps me find a higher power.

Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane, Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette, Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette, Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee, Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned.
iCeCuBEz v2 is offline   Reply With Quote
Old 07-18-2016, 04:00 PM   #2
Dinglesberry
longing
FFR Veteran
 
Dinglesberry's Avatar
 
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
Default 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.

Last edited by Dinglesberry; 07-18-2016 at 04:01 PM..
Dinglesberry is offline   Reply With Quote
Old 07-18-2016, 04:04 PM   #3
iCeCuBEz v2
XFD
FFR Veteran
 
iCeCuBEz v2's Avatar
 
Join Date: Mar 2008
Location: Connecticut
Age: 33
Posts: 4,924
Default 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?
__________________
I bring my math homework to church. It helps me find a higher power.

Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane, Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette, Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette, Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee, Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned.
iCeCuBEz v2 is offline   Reply With Quote
Old 07-18-2016, 04:05 PM   #4
Dinglesberry
longing
FFR Veteran
 
Dinglesberry's Avatar
 
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
Default 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
Dinglesberry is offline   Reply With Quote
Old 07-18-2016, 04:14 PM   #5
iCeCuBEz v2
XFD
FFR Veteran
 
iCeCuBEz v2's Avatar
 
Join Date: Mar 2008
Location: Connecticut
Age: 33
Posts: 4,924
Default 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
__________________
I bring my math homework to church. It helps me find a higher power.

Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane, Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette, Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette, Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee, Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned.

Last edited by iCeCuBEz v2; 07-18-2016 at 04:16 PM..
iCeCuBEz v2 is offline   Reply With Quote
Old 07-18-2016, 04:19 PM   #6
Dinglesberry
longing
FFR Veteran
 
Dinglesberry's Avatar
 
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
Default 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 is offline   Reply With Quote
Old 07-18-2016, 04:23 PM   #7
Dinglesberry
longing
FFR Veteran
 
Dinglesberry's Avatar
 
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
Default 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

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.

Last edited by Dinglesberry; 07-18-2016 at 04:40 PM..
Dinglesberry is offline   Reply With Quote
Old 07-18-2016, 05:40 PM   #8
igotrhythm
Fractals!
FFR Veteran
 
igotrhythm's Avatar
 
Join Date: Sep 2004
Location: Meesheegan
Age: 38
Posts: 6,534
Send a message via Skype™ to igotrhythm
Default 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.
__________________
Quote:
Originally Posted by thesunfan View Post
I literally spent 10 minutes in the library looking for the TWG forum on Smogon and couldn't find it what the fuck is this witchcraft IGR
igotrhythm is offline   Reply With Quote
Old 07-18-2016, 11:24 PM   #9
Soundwave-
Carry your failures proud
FFR Veteran
 
Soundwave-'s Avatar
 
Join Date: Sep 2015
Age: 23
Posts: 644
Default 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.
__________________
Quote:
Originally Posted by [11:38 PM] Hakulyte
only person who can legit tilt me is like YoshL
Quote:
Originally Posted by スンファンさん
右に3回回らない限り間違います。

Last edited by Soundwave-; 07-18-2016 at 11:40 PM..
Soundwave- is offline   Reply With Quote
Old 07-19-2016, 12:04 AM   #10
inDheart
Picker @ JAX2
FFR Simfile Author
 
inDheart's Avatar
 
Join Date: Aug 2011
Posts: 505
Default Re: programming question

Quote:
Originally Posted by Soundwave- View Post
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.
inDheart is offline   Reply With Quote
Old 07-19-2016, 08:17 AM   #11
Soundwave-
Carry your failures proud
FFR Veteran
 
Soundwave-'s Avatar
 
Join Date: Sep 2015
Age: 23
Posts: 644
Default Re: programming question

Quote:
Originally Posted by inDheart View Post
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.
__________________
Quote:
Originally Posted by [11:38 PM] Hakulyte
only person who can legit tilt me is like YoshL
Quote:
Originally Posted by スンファンさん
右に3回回らない限り間違います。
Soundwave- is offline   Reply With Quote
Old 07-20-2016, 09:50 AM   #12
Dinglesberry
longing
FFR Veteran
 
Dinglesberry's Avatar
 
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
Default 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?
Dinglesberry is offline   Reply With Quote
Old 07-20-2016, 10:02 AM   #13
xXOpkillerXx
Forever OP
Simfile JudgeFFR Simfile AuthorD8 Godly KeysmasherFFR Veteran
 
xXOpkillerXx's Avatar
 
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
Default Re: programming question

Quote:
Originally Posted by Dinglesberry View Post
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
xXOpkillerXx is offline   Reply With Quote
Old 07-20-2016, 12:06 PM   #14
Dinglesberry
longing
FFR Veteran
 
Dinglesberry's Avatar
 
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
Default Re: programming question

Quote:
Originally Posted by xXOpkillerXx View Post
P=NP
I think he ended up getting it in O(n!) time.. regardless though, he got it, and that's what counts.
Dinglesberry is offline   Reply With Quote
Old 07-20-2016, 12:11 PM   #15
xXOpkillerXx
Forever OP
Simfile JudgeFFR Simfile AuthorD8 Godly KeysmasherFFR Veteran
 
xXOpkillerXx's Avatar
 
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
Default Re: programming question

Quote:
Originally Posted by Dinglesberry View Post
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 !!
xXOpkillerXx is offline   Reply With Quote
Old 07-20-2016, 02:29 PM   #16
Dinglesberry
longing
FFR Veteran
 
Dinglesberry's Avatar
 
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
Default Re: programming question

Quote:
Originally Posted by xXOpkillerXx View Post
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 :/
Dinglesberry is offline   Reply With Quote
Old 07-20-2016, 02:34 PM   #17
Soundwave-
Carry your failures proud
FFR Veteran
 
Soundwave-'s Avatar
 
Join Date: Sep 2015
Age: 23
Posts: 644
Default Re: programming question

Quote:
Originally Posted by Dinglesberry View Post
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.
__________________
Quote:
Originally Posted by [11:38 PM] Hakulyte
only person who can legit tilt me is like YoshL
Quote:
Originally Posted by スンファンさん
右に3回回らない限り間違います。
Soundwave- is offline   Reply With Quote
Old 07-20-2016, 03:56 PM   #18
reuben_tate
Kawaii Desu Ne?
Retired StaffFFR Veteran
 
reuben_tate's Avatar
 
Join Date: Dec 2007
Location: The Kawaiian Island~
Age: 30
Posts: 4,182
Default Re: programming question

Is catching on fire a constant-time operation?
__________________
AMA: http://ask.fm/benguino

Not happening now! Don't click to join!



Quote:
Originally Posted by Spenner View Post
(^)> peck peck says the heels
Quote:
Originally Posted by Xx{Midnight}xX
And god made ben, and realized he was doomed to miss. And said it was good.
Quote:
Originally Posted by Zakvvv666
awww :< crushing my dreams; was looking foward to you attempting to shoot yourself point blank and missing
reuben_tate is offline   Reply With Quote
Old 07-20-2016, 04:24 PM   #19
AutotelicBrown
Under the scarlet moon
FFR Simfile AuthorD7 Elite KeysmasherFFR Veteran
 
AutotelicBrown's Avatar
 
Join Date: Jan 2014
Age: 31
Posts: 921
Default Re: programming question

Can you branch predict a multi-track drift?
AutotelicBrown is offline   Reply With Quote
Old 07-20-2016, 04:32 PM   #20
xXOpkillerXx
Forever OP
Simfile JudgeFFR Simfile AuthorD8 Godly KeysmasherFFR Veteran
 
xXOpkillerXx's Avatar
 
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
Default Re: programming question

Quote:
Originally Posted by reuben_tate View Post
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.
xXOpkillerXx is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 10:07 AM.


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