[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 166 KB, 1357x693, cslettercounter.jpg [View same] [iqdb] [saucenao] [google]
[ERROR] No.3768037 [Reply] [Original]

Hey csfags, can you help me out? I'm obviously new to this, and I can't figure out what to do next. I need to create a letter counter that will pass the test shown to the left. It preferably needs to be in this one method. There's the code I have so far.

>> No.3768073

shame bump

>> No.3768089

Well, let's see... since your return value is the variable <span class="math">array[/spoiler], you should probably do something with <span class="math">array[/spoiler] before returning it. Just a geuss.

>> No.3768094
File: 36 KB, 302x351, 1305784377742.jpg [View same] [iqdb] [saucenao] [google]
[ERROR]

>>3768037
>Package explorer

You sure are OP, you sure are...

>> No.3768106
File: 66 KB, 668x600, 1299812706752.jpg [View same] [iqdb] [saucenao] [google]
[ERROR]

>>3768089
Yeah, I know that. But what? I'm so confuseee

>>3768094
What, I can't explore mah packages?

>> No.3768122

>>3768094
Hey, I use package explorer.

>> No.3768147

Hey, how about you show us your purpose and design then we can help you.

Seriously, I have no idea what the fuck you're trying to do.

>> No.3768170

>>3768147
Sorry about that, one second.

>> No.3768182
File: 102 KB, 662x434, lettercounter2.jpg [View same] [iqdb] [saucenao] [google]
[ERROR]

>>3768147
Fuck it, let me just show you it. Damn abnormal reply error.

>> No.3768207

das sum bump

>> No.3768209

So I think first you should park yourself in spot 0 in the array and run through your string checking how many of each letter belongs in that spot, then once you go through the string, move on up to the next spot in the array.

The way you would compare would probably be the ascii value of each letter. So, for spot 0, a, do an int comparison (assuming the ascii value of a is 10) 10 == a? if yes, increment your counter.


I dont know Java, just C++ and even then Id say Im only okay, so I only gave a rough outline of how to do it but I dont know what it would look like in Java. If anyone else finds flaws with this method or has a better method, by all means, post it.

>> No.3768223

>>3768209

Forgot to mention that ascii values for letters are all sequential. What I mean is the number for a is immediately before the number for b, etc. So increment that as you move up on the array.

>> No.3768224

>>3768209
NO

>> No.3768246

>>3768209
Wouldn't he have to do every letter?

>> No.3768261

what the fuck is abnormal reply

>> No.3768262

>>3768246

Loop

Captcha:
uplifted bteamer

>> No.3768260

>>3768224
Why not?

>> No.3768272

>>3768224


What do you mean no?

>> No.3768275
File: 7 KB, 360x203, progforhim.png [View same] [iqdb] [saucenao] [google]
[ERROR]

df;lgk;'fklg

>> No.3768276

>>3768261
4chan doesn't let you post some replies when they are too long or have weird characters some shit

>> No.3768282

>>3768272
Only run one cycle through string and iterate by index.

posted it here

>>3768275

>> No.3768315

If(letter==a) array[0]++
if(letter==b) array[1]++
if(letter==c) array[2]++

you got alot of typing ahead of you OP
have fun

>> No.3768332

>>3768315
lol, this seems like something I would do back then

>> No.3768333
File: 9 KB, 330x236, 1262320699168.jpg [View same] [iqdb] [saucenao] [google]
[ERROR]

>>3768275
>int result[]
>char c
>result[c]

>i am very drunk

>> No.3768340

>>3768275
Character.toUpperCase...

>cannot convert int to char

the whole line

>> No.3768360

>>3768333
how to fix

>> No.3768385
File: 26 KB, 646x617, 1299625843994.jpg [View same] [iqdb] [saucenao] [google]
[ERROR]

>CS
>hard

>> No.3768399

>>3768360
Try putting this
char c = (char)(andthethingthatwastherbefore)

>> No.3768398

>>3768385
>not hard as hell

>> No.3768411

>>3768399
Fixes the error, but the array entries still return 0.

>> No.3768420

>>3768411
debug it and see what happens

>> No.3768489

>>3768420
Nothing, this is strange, that code seems perfectly fine and its along the lines of what the instructor told us.

>> No.3768501
File: 131 KB, 349x349, 66-jojan.png [View same] [iqdb] [saucenao] [google]
[ERROR]

>>3768315

>> No.3768542

>>3768489
Set a breakpoint on if statement a see what is in variable c. Might be something wrong there.

>> No.3768639

Well, if someone else hasn't already said this, you convert a character to an int by subtracting '0' from the character, ergo:

char temp='A' ;
cout<<temp-'0'

That will print 17, which is the numeric representation of 'A'. 'B' will be 18, 'C' will be 19, etc. So if the 0th element of the array stands for 'A', do these things for each letter in the relevant string:

-Convert the letter to its corresponding integer
-Subtract 17 from that integer (let's call this new number 'Temp')
-Increment array[Temp]

Of course, before you do all of this, you should convert the entire string you're running your function on into capital letters, since 'a' and 'A' have different corresponding integers.

Make any sense?

>> No.3768654

>>3768639

I probably should have stuck a semicolon at the end of cout, but I digress.

>> No.3768747
File: 38 KB, 536x343, asdf.jpg [View same] [iqdb] [saucenao] [google]
[ERROR]

Damn abnormal replies.

Anyways, here you go.

>> No.3768789

>>3768747
this will certainly end in arrayindexoutofbounds

>> No.3768904 [DELETED] 

>>3768789
26 sry

>> No.3768950
File: 23 KB, 494x262, fdsadf.jpg [View same] [iqdb] [saucenao] [google]
[ERROR]

>>3768789
Shit, times three.
Heres a fix.

>> No.3770026

OP here, you guys are fucking amazing. And it's not just that you guys gave me the answers, I actually understand it now that so many people helped me and gave their perspective. Thanks so much.

>> No.3770035

>>3768037
>1 based indexing
Ewww. Man, the more I see, the more I hate the new way CS students are taught. 1-based indexing? Really now?

Also, to answer you question:
Create an array of size 27. In Java, it starts 0 initialize IIRC - if you want to, explicitly initialize each element to 0. Go through the string character by character. For each character, determine the corresponding position in your return array, eg: 'a' goes to 1, 'b' goes to 2, 'c' goes to 3, and so on. (At least, that's how I understand the problem). Increment that position in the return array. After looping through the string, return the array.

>> No.3770086

>>3768223
>>3768639
Also, for a real program, I would immediately remind you that making the assumption that the program uses ASCII encoding is a bug, not a feature.

>> No.3771626

OP here, got 100% on the in class write up thanks to all of you. /sci/ confirmed for broest board.
>>3770035
Yep, that's how we're taught. What I have on the OP what the TA basically told us to write.

>> No.3771637

There's nothing wrong with 1-based indexing but if you use a language that expects zero-based indexing, it is kind of dumb.

>> No.3771660

>>3771637

1 based is very clumsy if you use pointers a lot... and I do.

>> No.3771663

>>3771660
> if you use a language that expects zero-based indexing, it is kind of dumb
got your back

>> No.3771696

>>3768747
>internet explora

>> No.3771706

>>3771663

noooooo...... I was explaining why it is more than an arbitrary chose and/or esthetic choice.

>> No.3771736

>>3770086

How many other encodings are there? And do they all have the capital letters in a row from A-Z?

>> No.3771774

>>3771736
In common use at the language level? None, for certain values of "common use". Then again, it's an easily fixed problem: just encode a character using the integer 'the position of your character in the string "ABCDEFGHIJKLMNOPQRSTUVWXYZ"'.

>> No.3771789

if an alphabet etc etc..
array[ASCII_OF_A - charInString]++;

>> No.3771804

> /sci/
> computer programming

right...

>> No.3771814

>>3771789
sorry, it was array[charInString - ASCII_VALUE_OF_SMALL_A];

>> No.3771819

>>3771814
>>3771814
http://pastebin.com/yzgAg7EY

>> No.3771827

>>3771819
samefag again

it only handles small letters and doesn't check for other characters
it's up to you to figure those out
good luck