Desperate need of help with arrays...

If you could send me msg I'd be more than happy to provide you with all information needed . I've been working on this assignment for almost 24 hours straight now and it's due in about 36 hours or so and I'm stumped.

Alright so Description:
The program asks the user for a seed number. This number is then used to generate an
array of random characters of a-z and A-Z. When testing your program, use the same seed so you can
test with the same sequence of characters. The program then calculates the number of occurrences of
each character in the string. Finally, a summary is printed out back to the user.

Code deleted

I know it is very messy and bad atm but those are probably the easiest parts to fix later on. The thing I need help with is Line 55-65 I don't know how to print out a counter of all my Characters . I actually don't even know how to count all characters . If someone could please explain this part then I will be very happy as this is the only part left that I'm having trouble with ;-;.


Here's an example output:
Enter the seed number: 55
Number of characters: 25
Generated characters:
ptefFfdUAq
fATdgYbRYy
ZKyMC
Summary for the characters:
a = 2
b = 1
c = 1
d = 2
e = 1
f = 4
g = 1
h = 0
i = 0
j = 0
k = 1
l = 0
m = 1
n = 0
o = 0
p = 1
q = 1
r = 1
s = 0
t = 2
u = 1
v = 0
w = 0
x = 0
y = 4
z = 1
Last edited on
Hi

1
2
3
4
for (int i = 1;i<size;i++){
    if (character[i-1] == 'A' || character [i-1] == 'a')
    counter[0] = counter[0]+1;    
    }


Index should start at 0!;)

This means you've got to write an if...else with 26 'branches' (or whatever they're called).
Not very efficient.
Why not:
1) convert each character to lower case first (http://cplusplus.com/reference/clibrary/cctype/tolower/)
2) look at its ASCII code (http://www.asciitable.com/)
3) Use some very simple maths to convert that ASCII code into a slot in your counter array
?


Hello! Finally, thank you for responding, I have been trying all day for someone to respond to my threads ;-;!
1. If I convert to lowercase isn't that the same as saying a = A? or is it saying that I can convert it and it won't change the output of
AaBrfrA
? If so, then I will try to use that.
Other than that, it is part of my required assignment to use both A and a
2. I know the ASCII code for 65-90 , 97-122 A-Z and a-z (:
3. I converted everything to ASCII code basically already but I'm pretty sure I'm doing it wrong because I'm just confusing myself even more but here's my already messed up code; -;



Code deleted.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (char i = 97; i<=122 ; i++){
    int x = 0;
    cout<< i<<" = "<<counter[x]<<endl;
    x++;
    
}
for (int p = 0;p<size;p++){
    char AZ = 65;
    char atoz = 97;
    if (character[p] == AZ || character [p] == atoz)
    counter[0] = counter[0]+1; 
    AZ++;
    atoz++;
   
    }

I don't see what the use of that first loop is. Could you explain it?
Second loop is wrong - you're still setting yourself up for 26 separate if statements whereas if you use my solution you don't need any.
If you can follow these steps (- I don't want to write it out for you - that's cheating ;):
1
2
3
4
5
for (int p = 0;p<size;p++){
    convert character[p] to lowercase and store a copy (tolower doesn't modify its argument)
    it will have an ASCII code between 97 and 122 inclusive
    increment the relevant slot in your counter array (hint: 97 -> 0, 98 -> 1, 99 -> 2)
} 


*EDIT*
Actually I've just noticed you're not going to need 26 if statements but the code's still wrong - it's not going to do what you want it to.
Last edited on
Yeah, I know, I was just messing around attempting to find a way to make it do something I wanted..

1
2
3
4
5
6
for (char i = 97; i<=122 ; i++){
    int x = 0;
    cout<< i<<" = "<<counter[x]<<endl;
    x++;
    
}
is supposed to show a-z the lower case characters which in ASCII is 97-122
I have to have those as my required output and yeah the 2nd loops I'm just stuck .
I'm currently looking at your hint so I'll get backto you with an"Edit" soon (: Ty !
Uh I'm pretty sure that loop is messed up too. I'm just so confused about "design" of a specific code



Dang ;-; Seems like I had to Edit sooner than I thought

I tried doing tolower earlier but I didn't know where in my function to put it. and I already stored characters into character[100] array if you look before

char character[100];
and I stored the random characters in there with whatever output x numbers I put

K, I'll check back at this in a bit , I'll just be doodling with my code and try to put your words into sense in my mind at 6 AM ^-^ . Thanks for the help though . I appreciate it :D .

Another edit:
1
2
if (character[p] == AZ || character [p] == atoz)
    tolower(character[p]);

Is this what you meant? I don't know how to check if it changed everything correctly X_O .

OMG. Edit again: I'm sorry! I just noticed you used the for loop let just try that ... I'm slow, sorry, please bear/bare? with me D:!

Also, would it help if I gave you the link of the full requirements and such? o-o
Last edited on
K, I'll check back at this in a bit , I'll just be doodling with my code and try to put your words into sense in my mind at 6 AM ^-^ . Thanks for the help though . I appreciate it :D .

OK, I just managed to fix it (with my copy of your code) and it works and it takes TWO lines inside that for loop. So the rest of your code, messy though it is ;), is obviously doing the right thing.
Remember to #include <cctype> to get the tolower function.
Good luck!
Did you understand the loop I used before?(the one you asked about) and I'm assuming your hint means
97 = counter[0] right? o-o

If so... aWEKFMAKELRGM
1
2
3
4
5
6
7
8
9
for (char i = 97; i<=122 ; i++){
    int x = 0;
    cout<< i<<" = "<<counter[x]<<endl;
    x++;}
    for (int p = 0;p<size;p++){
    tolower(character[p]);
    counter......
}
   

I'm not sure what I'm doing anymore but I'm not quitting ! D:<!!!!
For sure: I don't know what to do with
1
2
3
4
for (char i = 97; i<=122 ; i++){
    int x = 0;
    cout<< i<<" = "<<counter[x]<<endl;
    x++;}


Or do I not need it anymore considering I will be able to figure out how to do the loop you did?

Sigh. Edit:

1
2
3
4
5
6
for (char i = 97; i<=122 ; i++){
    cout<< i<<" = "<<counter[i]<<endl;    

for (int p = 0;p<size;p++){
    tolower(character[p]);    
}    

Gives me a weird output of a bunch of numbers like 3459305 or something ;-;

At 7:48 AM I have given up for the time being and I hope tobe able to figure t his out when I wake up in 2-3 hours... Gl to me...
Last edited on
1
2
3
for (int p = 0;p<size;p++){
    tolower(character[p]);    
}    


Close :) To quote myself:
convert character[p] to lowercase and store a copy [of the return value] (tolower doesn't modify its argument)

That's a local copy that you'll throw away after each iteration of the loop.

and I'm assuming your hint means
97 = counter[0] right? o-o

Correct. This is more or less the next line/statement in your for loop. The ASCII code (i.e. the return value of tolower, of which you should have a local copy) is the index into your counter array (after you've done a simple subtraction on it).
Gives me a weird output of a bunch of numbers like 3459305 or something ;-;

Make sure your counter array has all elements set to 0 before you start.
All this
int counter[26];
does is allocate space for 26 ints, filled with whatever random junk was there already.

*Edit*
Your first loop's so you can output something like:
a: 3
b: 7
c: 2

or whatever, isn't it? So that's just the print-out - leave it till after you've filled your counter array, then do the whole output thing in one go. In fact, that's the only way it'll work.




Last edited on
EDIT: Solved. I ended up using strings instead . The original way I was trying to do was too difficult for me to comprehend . Or maybe I could comprehend but I'm just too tired, not sure.
Anywho thanks for your time (:!

for 97 = counter[0]
since counter is an integer array and to get 'a' it'd have to be a character then isn't that just setting counter[0] to the number 97? Or is my whole concept of ASCII wrong?


Still working on it I guess. .

Edit: int counter[26] = {0};
Doesn't this set the all the elements to 0? I changed it to this but I still get the random numbers if I used the code :
1
2
3
4
5
6
for (char i = 97; i<=122 ; i++){
    cout<< i<<" = "<<counter[i]<<endl;    

for (int p = 0;p<size;p++){
    tolower(character[p]);    
}   
Last edited on
1
2
3
for (int p = 0;p<size;p++){
    tolower(character[p]);    
}  


OK, I'll try a third time just for the hell of it:

convert character[p] to lowercase and STORE A COPY OF THE RETURN VALUE (tolower doesn't modify its argument)


It returns a char, by the way ;) And if you stuff that char into an array subscript operator (that's [] to you and me), it'll work exactly the same way as an int. And if you subtract 97 from its value...

Lolol. I got it (: . Thank you very much :D.

When testing your program, use the same seed so you can test with the same sequence of characters.


Unless I'm misunderstanding the question, is the person that assigned this to you expecting the same numbers to be generated every time you use the same seed?
Topic archived. No new replies allowed.