Ask your own question, for FREE!
Computer Science 11 Online
OpenStudy (christos):

C++, a simple question. Can you please tell me how I can create an array that will return a value like this? A[i] = Variable(space)Variable 2 variables is the catch

OpenStudy (e.mccormick):

Well, what are the variables? Is any other method of parring them allowed?

OpenStudy (christos):

Yes many methods would be welcome. The variables with be Char(space)Int

OpenStudy (e.mccormick):

OK, so they are different data types. Does it have to be in an array, or are you allowed to use a structure?

OpenStudy (christos):

I am allowed to use structure

OpenStudy (e.mccormick):

Is this for that count the number of letters problem? I did it an interesting way. See, the ASCII value for 'a' is 97. 'b' us 98 and so on. So, if I do this: ``` int letter_count[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // Empty integer array with 26 spots. ``` Now, whenever I detect an a, I do `letter_count[0]++` and for a c it is `letter_count[2]++` and so on. When I print it out, I just need to print the character based on the iterator plus 97. ``` for(int i = 0 ; i<26 ; i++){ cout << (char)(i+97) << ":" << letter_count[i]; } ```

OpenStudy (christos):

But how can you erase non zero values ?

OpenStudy (christos):

sorry I ment *zero values

OpenStudy (christos):

yes its for that problem

OpenStudy (e.mccormick):

With an if statement.

OpenStudy (christos):

but if I erase a zero value with an if statement , how can I move the first non zero value from the next line to the last position of the previews line ?

OpenStudy (christos):

Do you understand what I mean ?

OpenStudy (christos):

remember 4 non zeros each row

OpenStudy (e.mccormick):

You do not need to. As you cycle through the array, only print and advance a counter for number of items printed inside the if statement. When the counter reaches %4==0 also print an endl. So it will have a nested if statement for that part.

OpenStudy (christos):

I understand !

OpenStudy (e.mccormick):

That was what the `int newline = 0` in my example the other day was about. =)

OpenStudy (christos):

ohh I see now

OpenStudy (christos):

Btw did you understand the other way I was thinking of doing it ? Would it be easy for you to tell me a way of creating an array with 2 separate variables ?

OpenStudy (christos):

2 seperate variables for each position

OpenStudy (christos):

of the array

OpenStudy (e.mccormick):

The problem is that an array can only be of one type. Hmmm... but you could cheat that a few different ways. In fact, two arrays would be not hard and at your skill level.

OpenStudy (e.mccormick):

This shows the key to my crafty method: ``` #include <iostream> #include <stdio.h> #include <stdlib.h> #include <cstring> using namespace std; int main() { int newline = 0; for (int i = 64 ; i < 127 ; i++) { cout << i << ":" << (char)i << " " ; newline++; if (newline%10==0) cout << endl; } cout << "\n\n"; char test[]="abcxyzABCXYZ"; for (int i = 0 ; i < 12 ; i++) { cout << "i=" << i; cout << " t[" << i << "]=" << test[i]; cout << " (int)t[" << i << "]=" << (int)test[i] << " "; cout << endl; } return 0; } ``` But that is probably overkill in your case. Yah, 2 arrays using the same iterator is simple!

OpenStudy (christos):

so with this code you just showed me, you used an array with 2 variables of a different type on each position ?

OpenStudy (e.mccormick):

I do it in one array with that. But for you, try this: ``` int letter_count[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; char letter_letter[]="abcdefghijklmnopqrstuvwxyz"; letter_count[5]=3 cout << letter_letter[5] << letter_count[5]; ``` See what that gets you. I think you can work with it from there.

OpenStudy (christos):

ok and something last. Would it be possible for me to do it with structure and/or the function strcat() ?

OpenStudy (e.mccormick):

A structure would let you organize things better from a traditional c++ point of view. ``` struct letterCount { char letter; int count; }; ``` Then there is an array of 26 for the alphabet, or 26 individually declared ones... all depends on how you wanted to work with it.

OpenStudy (christos):

ok and with strcat() ? I have tried using strcat() to solve my problem but to no avail. Maybe it's because of my lack of experience , maybe it's just not even possible ?

OpenStudy (e.mccormick):

That is for copying a string... I don't know what it would be used for in there.

OpenStudy (christos):

nono for copying it's strcpy() , strcat() is for connecting 2 strings together

OpenStudy (e.mccormick):

Well, it is to copy it onto the tail. That is what I meant. Concatination is what they call it.

OpenStudy (christos):

for example strcat(p[i],u[i]) p[i] = letter u[i] = count

OpenStudy (christos):

or strcat(p[i],times)

OpenStudy (e.mccormick):

OH! Well, the count is an integer, not a character.

OpenStudy (e.mccormick):

strcat only works wih strings, not integers.

OpenStudy (christos):

This? *strcat(&r[i],&times)

OpenStudy (christos):

I don't even know what it does , my compiler fixed it for me

OpenStudy (e.mccormick):

Pointers. Which compiler?

OpenStudy (christos):

It's Xcode , I am using mac

OpenStudy (christos):

So can I use pointers for this thing ?

OpenStudy (e.mccormick):

I don't think it wll work right because it is still incorrect types. Sort of like the problem with doing `"1"+1` in a computer. The quotes make the first into a string but the second is an integer. They can not be added.

OpenStudy (christos):

I see man, well thank for very much for taking the time to help me address my problems, I will take some time to use the methods we discussed above in order to find a way to solve it !

OpenStudy (e.mccormick):

Work with the two arrays. That is easist and will work with what you know. And remember, you can reuse an iterator! ``` for(int i = 0 ; i < 5 ; i++){ array_1[i] array_2[i] } ```

OpenStudy (e.mccormick):

That code will not run... but it shows how two arrays can be cycled through at the same time.

OpenStudy (christos):

but what is an iterator ? Also the current lesson is teaching us structures !

OpenStudy (e.mccormick):

Aaaah... if learning structures, then doing it in a structure might be best.

OpenStudy (e.mccormick):

To iterate... : to say or do again or again and again : reiterate http://www.merriam-webster.com/dictionary/iterate In computers, an iterator is the counter in a loop. So as you loop (do it again and again) the iterator keeps track of where you are at the moment.

OpenStudy (christos):

oh I see !

OpenStudy (e.mccormick):

Yah, they tak a lot from English... then morph it into computer lingo.

OpenStudy (e.mccormick):

Have you learned dot notation for structures?

OpenStudy (christos):

if you mean things like that: http://screencast.com/t/bmbqwdUoQg Yes but I don't understand it :(

OpenStudy (christos):

@e.mccormick

OpenStudy (christos):

Do I need to use this for this problem?

OpenStudy (e.mccormick):

If you want them in a struct, yah.

OpenStudy (christos):

I see :/

OpenStudy (e.mccormick):

Play with this code some: ``` struct letterCount { char letter; int count; }; int main() { letterCount counter[] = {{'a',0},{'b',0},{'c',0}}; counter[2].count += 2; counter[2].count++; counter[0].count += 99; for (int i=0; i < 3; i++) { cout << counter[i].letter << ':' << counter[i].count << endl; } ```

OpenStudy (e.mccormick):

Note that whan you fill an array of structures that way, ORDER MATTERS! Because my letter is declaied first in the structure, I have to add the character first when I populate the array.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!