Sorry, but no. C++ does not support variable length arrays (VLA). Statically allocated array's size has to be known when you compile the binary.
Therefore, the int newNums[individualNumbers]; is not legal.
There are at least three approaches.
* Have a fixed-size array that is large enough and an integer that shows how many elements of the array you actually do use.
* Allocate array dynamically. The size of those arrays does not need to be known before you run the program. However, management of dynamic memory manually is something you don't want to do unless you have to. (You have to understand/learn it at some point.)
#include <iostream>
#include <string>
usingnamespace std;
int countLet(string p, int q){ // A function to count consecutive identical letters
int x = 1; // starts at one to accommodate it not counting the last in the consec order
while (p[q] == p[q+1]){
x++;
q++;
}
return x;
}
int main(){
int tot = 1; // currently unused
char x; //this will be an if check later. Currently unused.
string y; // y is the original string entered. e.g. hhelllooo!!
cin >> x;
cin.ignore();
cin >> y;
int num[y.length()] {0}; // num is an array initialized with zeroes
int individualNumbers = 0;
for (int i = 0; i < y.length(); i++){
if (y[i] != y[i-1]){ // this if calls the function on just the different letters
num[i] = countLet(y, i); // then sticks them into the num array
} // the plan is to cout the letter then it's corresponding number of appearances
}
for (int i = 0; i < y.length(); i++){
if (num[i] != 0){
individualNumbers++;
}
}
int newNums[individualNumbers];
}
**This is self learning challenge, not homework or anything like that**
Thanks for that documentation about dynamic arrays. I went ahead and decided to push myself and try to solve it using dynamic arrays. I was successful.
I'm not saying it is the cleanest solution, but it met the challenge's criteria and passed all tests.
I'll post how I did it, not only on the off chance there are future https://open.kattis.com users or beginners with the same question, but also in hopes that someone more seasoned than me could point out things like potential problems, sketchy code, better ways, etc. since this is long forgotten ground for me.
#include <iostream>
#include <string>
usingnamespace std;
int countLet(string p, int q){ // takes in the entire string plus the iteration of the loop
int x = 1; // Can't start at 0 because no repeats is still a count of 1
while (p[q] == p[q+1]){
x++;
q++;
}
return x;
}
void decode(char r, char s){ // passes in the string to decode char by char, plus the number as a char
string xx(1, s); // converts char to string first
int x = stoi(xx); // then string to int. Because I don't know a better way
for (int i = 0; i < x; i++){ // a loop to print H4 as HHHH
cout << r;
}
}
int main(){
char x; // indicator of E for encode or D for decode
string y; // e.g. HHHeellooo!! or H3e2l2o3!2
cin >> x;
cin.ignore();
cin >> y;
int num[100] {0}; // 100 is the max the string can be
int counts[100] {0}; // initialized with zeroes to do an easy check to grab anything not zero
if (x == 'E'){
num[0] = countLet(y, 0); // I had to call the function on element at zero outside the loop
for (int i = 1; i < y.length(); i++){ // adjusted the bounds accordingly
if (y[i] != y[i-1]){ // if a consecutive char is found, it calls the function to count occurences
num[i] = countLet(y, i);
}
}
int tot = 1;
int *totarr; // dynamic array
for (int i = 1; i < y.length(); i++){
if (num[i] != 0){
counts[i]= num[i]; // the array isn't exactly necessary here, but it helped me organize
tot++; // in my mind. tot is accumulated to set the bound of dynamic array
}
}
int place = 1; // the place holder for where to stick the elements into the dynamic array
totarr = newint[tot];
totarr[0] = num[0]; // again, I found it less troublesome to go ahead and set the 0th element
for (int i = 1; i < y.length(); i++){
if (counts[i] != 0){
totarr[place] = counts[i]; // fills dynamic array with all the counts in order
place++;
}
}
int splace = 1; // this block of code does the same thing, but condenses the consec chars to a single char
char *indiv;
indiv = newchar[place];
indiv[0] = y[0];
for(int i = 1; i < y.length(); i++){
if (y[i] != y[i - 1]){
indiv[splace] = y[i];
splace++;
}
}
for (int i = 0; i < tot; i++){ // couts them in order
cout << indiv[i] << totarr[i];
}
} // end of encoding
if (x == 'D'){ // the decoding was much simpler after getting encoding down
for (int i = 0; i < y.length(); i++){ //input can only be either a letter
if (isalpha(y[i])){
decode(y[i], y[i+1]);
}
if (ispunct(y[i])){ // or a punctuation character followed by number
decode(y[i], y[i+1]);
}
}
}
}
Recursion! Nice! And is that tertiary code too by chance?
Recursion was going to be my first attempt, but I didn't quite know the right way to stop it going to infinity.
I can follow along with your decode function, but not your encode. Will you break it down? How does it move past str[0]?
I think it's actually called the "conditional operator" (and it's the one and only "ternary" operator that I know in c++).
For encode:int p = str.find_first_not_of( str[0] );
looks for the first item that isn't equal to the first character, str[0]. There are two possibilities - it doesn't find one (in which case if ( p == string::npos ) p = str.size(); forces it to be the length of the string), or p is the start of the next distinct character. Either way, p will end up being the number of repeats of the character.
str[0] + to_string( p ) then gives the letter and the number of times it occurs.
p < str.size() ? determines if there are any more characters left, and if so recurses from p characters on (encode( str.substr( p ) )); if there aren't any characters left it just stops the recursion by adding an empty string instead.
Actually, I thought decode was harder (because there may be more than one digit after the letter - "12" in the example). However, @Keskiverto explains most of it below.
Okay, so it chips away at the string from the beginning by removing length of p, and stops when it can't find anymore repeats.
Oh, about it being harder than you thought, it definitely could have been, but one of the conditions of the problem stated that the repeat, r, was 0 < r <= 9.