Program is not accessing correct index of array.. Why? - c++

I come to this site in need of help, after struggling with this problem for a few days now. I am trying to program a poem that accepts some data from standard input and then outputs a poem based on that data.
The code seems to be working, but it is not correct! It is giving me the wrong index of the array I am using. I would love extra eyes to help me with my code and let me know what I am doing wrong.
ALSO! For some reason, I am not able to access the third array of the char array... I tried to place "SIZE - 1" in there but it prints nothing... Would love to understand why this is. Does this look right?
// Program that accepts some data from standard input,
#include <iostream>
#include <cstring>
//here... extracted.
for (int sign = 0; sign < poem[line]; sign++)
{
if (line > word_count)
{
std::cout << " ";
print_poem(seed);
}
else
{
print_poem(seed);
}

You haven't mentioned what exactly the task is but I can at least explain to you parts of the problem.
Are the correct syllables being printed?
Let's assess if the correct syllables are being printed. I ran your code on my machine (with the input you provided that is "100 3 1 5 7 5") and got:
nahoewachi
tetsunnunoyasa
munahohuke
The syllable count of each line is fine (5,7,5) so that's not a problem.
The first syllable you have a problem with is chi in nahoewachi. I'm only illustrating why this syllable is being printed. You can apply the same logic to the rest.
Initially, the seed is 100. Before processing the first row, you apply generate_prnd, which gives 223. Before calculating chi, you print 4 other syllables (na, ho, e and wa). This means that you have applied generate_prnd 8 times before calculating the fifth syllable.
Applying generate_prnd 8 times on 223 gives 711. Applying one more time (to get row) gives 822.
822%9 = 3rd row (0 indexed).
Applying one more time (to get column) gives 361. 361%5 = 1st column.
Therefore the index for the fifth syllable is (3,1). The string at the (3,1)th index is "chi". Therefore, the correct syllable is being printed. The indexing is correct. There's a problem with your logic if you want a different syllable to be printed.
Now, let's assess why there aren't any spaces in your output.
In the example you provided, num_lines=3. The word_counts (actually syllable counts) are 5, 7 and 5. You are applying a space when line (which is always less than num_lines) is greater than word_count.
However, line is always less than word_count since the maximum value of line is 2 (num_lines - 1). Therefore, a space will never be printed.
P.S. If you are allocating memory using new, don't forget to deallocate using delete later.

Related

If two digits are the same 0s then make 0 condition in C++ and Root

In my code, legends are running within a loop, and I am trying to show a graph with
0-10%
10-20%
and so on. The problem is when I write this code
legend->AddEntry(gr[i], Form("%d0-%d0 %%",i+0,i+1), "lep");
It shows
00-10%
10-20% etc
So how to not show 00, but 0 in the first line?
A small adaptation of the shown statement should be enough; use:
legend->AddEntry(gr[i], Form("%d-%d %%", i*10 , (i+1)*10), "lep");
Explanation:
Form("%d0-%d0 %%",i+0,i+1) seems to be some kind of string formatting, and i your loop variable which runs from 0 to 9, right? The shown Form statement just appends "0" hard-coded to the single digit in i; instead, you can multiply i by 10, resulting in the actual numbers you want printed; and since 10*0 is still 0, this will be a single digit still; so, replace the previous Form(...) call with Form("%d-%d %%", i*10, (i+1)*10) and you should have the result you want!
In case you're worrying that printing i*10 is "less efficient" than printing i with "0" suffix - don't. The formatting and output of the string is most probably orders of magnitude slower than the multiplication anyway, so any overhead of doing multiple multiplications is negligible.

What's the difference between LEN and <>"" when searching for values?

I initially learned that if I want to see if a cell has any contents to use if(A1<>"",.... But as I receive more and more assistance on SO, it seems most people use if(LEN(A1),... Is there a difference? Did I learn the wrong information? Should I ever opt for one over the other or just always use LEN from now on?
pretty much the same result. difference is:
LEN(A1) - checks if A1 has a length
A1<>"" - checks if A1 is not equal to "empty"
then there is a length of the formula itself (some prefer to save 1 extra character space):
A1<>"" has 6 characters compared to LEN(A1) 7 characters
the superiority of LEN comes when you need to check for character count like:
=IF(LEN(A1)=4, TRUE, FALSE)
eg. output TRUE only if A1 value has exactly 4 characters

How to generate all possible 4 letter words in C++

I want to write a program that can give me all 4 letter words (from the dictionary or outside the dictionary). I code in C++. And by far, I've reached nowhere.
I'm simply a beginner in C++, I can apply the logic but I'm not introduced to advanced features in C++. It doesn't matter if it takes a long time for this program to end execution, I just want the solution.
For example:
abcd
king
ngik
cbda
play
lpay
payl
and so on (just a few of the millions of outputs I hope this program to output).
NOTE: The words generated need not make sense and I do not want to discard any combinations, I want it all.
I suggest looping say i from 0 to 26^4 - 1, each time outputting 'A' + i / (26*26*26), 'A' + i / (26*26) % 26, 'A' + i / 26 % 26, and 'A' + i % 26, then a newline.
Make a array that has all the possible letters in it(add numbers and symbols if you want).
Then use four nested for loops that loop a number from 0 to the length of the array.
Lets say that the loop number variables are a,b,c,d.
In the inner loop(the last one) you can output it as array[a] + array[b] + array[c] + array[d]
This gives all the possible combinations where you can add numbers and symbols aswell.
use recursion
explore DFS a 26-ary tree and output the letter every time you go down on the corresponding branch. depth = 4.
P.S. recursive algorithms eat stack memory like hell... so be sure your machine let programs have enough stack mem. You don't wanna run this on a pic micro with only 3-level call stack :-)

Generating letters of the english alphabet

I have homework due that states that I need to write a program that generates the first 15 letters of the english alphabet. I can't delcare and set 15 different variables or constants. The letters must be displayed in a number of columns initially set by the user. the numbers have to be aligned in columns. Can anyone help? Maximum number of columns is 7 and the minimum is 1.
Here's some pseudo-code to get you started. Read it, understand it, then try to implement it.
get numcols from user
if numcols < 1 or numcols > 7:
print error and exit
ch = 'a'
for count = 1 to 15:
output ch followed by space
add 1 to ch
if count is an integral multiplier of numcols:
output newline
endif
endfor
if numcols is not equal to 3 or 5:
output newline
endif
It's pitched at about the level of your homework (no fancy stuff and the smallest hint of awkwardness) and should map reasonably well into C code.
As part of this implementation, you should research:
the fact that character constants like 'a' are really integers in disguise.
remainder or modulus (%) operators and how/why they are useful here.
getting user input with scanf.
putchar for outputting characters.
why you have that final if statement :-)
Here is a hint:
ASCII code of A is 65, B is 66, C is 67 and so on. You can do it in a loop starting from 65 and going on for 15 iterations.
This can be done with two nested loops, one for the vertical and one for the horizontal. since the numbers are in sequence in value you can increment the variable for the character each time.
I don't want to give away more than that unless another user says I should. I've already given a lot of help and I'm sure you can figure out the rest.
If you feel you need more help I'll try to not give too much but explain more.

C++ cοde related to matrix,n*n grid etc

Buttons
Each cell of an N x N grid is either a 0 or a 1. You are given two such N x N grids, the initial grid and the final grid. There is a button against each row and each column of the initial N x N grid. Pressing a row-button toggles the values of all the cells in that row, and pressing a column-button toggles the values of all the cells in that column.
You are required to find the minimum number of button presses required to transform the grid from the initial configuration to the final configuration, and the buttons that must be pressed in order to make this transformation.
When the initial and the final configurations are the same, print "0".
Input
The first line contains t, the number of test cases (about 10). Then t test cases follow.
Each test case has the following form:
The first line contains n, the size of the board (1 ≤ n ≤ 1000).
n lines follow. The ith line contains n space separated integers representing the ith row of the initial grid. Each integer is either a 0 or a 1.
n lines follow, representing the final grid, in the same format as above.
Output
For each test case, output the number of row-button presses, followed by the row buttons that must be pressed. Print the number of column-button presses next, followed by 0-indexed indices of the column buttons that must be pressed. The total number of button presses must be minimized.
Output "-1" if it is impossible to achieve the final configuration from the initial configuration. If there is more than one solution, print any one of them.
Input:
1
3
0 0 0
1 1 0
1 1 0
1 1 0
1 1 1
1 1 1
Output:
1
0
1
2
Though it works absolutely fine on my machine,it doesnt accept a solution at codechef and gives me a wrong answer.Can anyone guide me what to do pls pls pls??
Code has been written in C++ and compiled using g++ compiler.
In the code posted, I would revise the code after you calculate "matrixc". I find it very difficult to follow beyond that point, so I'm going to stop looking at the code and talk about the problem. For those without the code, matrix C = initial matrix - final matrix. The matrices are over the binary field.
In problems like these, look at the symmetries in the solution. There are three symmetries. One is the order of the buttons does not matter. If you take a valid solution and rearrange it, you get another valid solution. Another symmetry is that pressing a button twice is the same as not pressing it at all. The last symmetry is that if you take the complement of a valid solution, you get another valid solution. For example, in a 3x3 grid, if S = { row1, row3, col1 } is a solution, then S' = { row2, col2, col3 } is also a solution.
So all you need to do is find one solution, then exploit the symmetry. Since you only need to find one, just do the easiest thing you can think of. I would just look at column 1 and row 1 to construct the solution, then check the solution against the whole matrix. If this solution gives you more than N buttons to press for an NxN grid, then take the solution's complement and you'll end up with a smaller one.
Symmetry is a very important concept in computer science and it comes up almost everywhere. Understanding the symmetries of this problem is what allows you to solve it without checking every possible solution.
P.S. You say this code is C++, but it is also perfectly valid C if you remove #include <iostream> from the top. It might take a lot less time to compile if you compile it as C.