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
Well, what are the variables? Is any other method of parring them allowed?
Yes many methods would be welcome. The variables with be Char(space)Int
OK, so they are different data types. Does it have to be in an array, or are you allowed to use a structure?
I am allowed to use structure
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]; } ```
But how can you erase non zero values ?
sorry I ment *zero values
yes its for that problem
With an if statement.
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 ?
Do you understand what I mean ?
remember 4 non zeros each row
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.
I understand !
That was what the `int newline = 0` in my example the other day was about. =)
ohh I see now
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 ?
2 seperate variables for each position
of the array
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.
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!
so with this code you just showed me, you used an array with 2 variables of a different type on each position ?
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.
ok and something last. Would it be possible for me to do it with structure and/or the function strcat() ?
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.
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 ?
That is for copying a string... I don't know what it would be used for in there.
nono for copying it's strcpy() , strcat() is for connecting 2 strings together
Well, it is to copy it onto the tail. That is what I meant. Concatination is what they call it.
for example strcat(p[i],u[i]) p[i] = letter u[i] = count
or strcat(p[i],times)
OH! Well, the count is an integer, not a character.
strcat only works wih strings, not integers.
This? *strcat(&r[i],×)
I don't even know what it does , my compiler fixed it for me
Pointers. Which compiler?
It's Xcode , I am using mac
So can I use pointers for this thing ?
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.
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 !
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] } ```
That code will not run... but it shows how two arrays can be cycled through at the same time.
but what is an iterator ? Also the current lesson is teaching us structures !
Aaaah... if learning structures, then doing it in a structure might be best.
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.
oh I see !
Yah, they tak a lot from English... then morph it into computer lingo.
Have you learned dot notation for structures?
if you mean things like that: http://screencast.com/t/bmbqwdUoQg Yes but I don't understand it :(
@e.mccormick
Do I need to use this for this problem?
If you want them in a struct, yah.
I see :/
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; } ```
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.
Join our real-time social learning platform and learn together with your friends!