Store a matrix of letters delimited by spaces - c++

I'm trying to create a program which can automatically solve word search puzzles.
I'm having some trouble storing the inputted word search puzzle. The example word search text file I'm supplied with contains letters separated by two spaces. There is a blank line between each populated line.
This is the code that I have right now. It seems to only store and output the left most column of the puzzle. What am I doing incorrectly? Is there a cleaner way of doing this?
vector< vector <string> > puzzle;
ifstream myPuzzle ("puzzle.txt");
if (myPuzzle.is_open())
{
while (myPuzzle)
{
string temp;
if (!getline(myPuzzle, temp))
break;
istringstream ss(temp);
vector <string> record;
while (ss)
{
string temp2;
if (!getline(ss, temp2, ' '))
break;
record.push_back(temp2);
}
puzzle.push_back(record);
}
}
else
cout << "Failed to open puzzle file!" << endl;
cout << "Puzzle succesfully inputted" << endl;
for (vector< vector<string> >::size_type i = 0; i < puzzle.size(); ++i)
{
for (vector<string>::size_type it = 0; it < puzzle[i].size(); i++)
cout << puzzle[i][it];
cout << endl;
}
Example matrix of characters (puzzle.txt):
U T Y Y A L P S G I B S K I P Q H S K T
A T L T I R U N E Z I M O D N A R S F P
D J I P R H H V E U E W U K F D L T A V
R T N Q N O E Y U O L H D S P Q C A F H
A A E U O O S A J F B O I Z S C C C V L
T T A I D P I B P L B L C E T H P K T I
I I R C U P Q S A W U B T Y N P E R T Y
C S T K U M S C R O B I I K N I L L H R
D E N B N A K I R U O N O H L I Q Y L A
F L A M I Z M M E S C S N S B B H O H N
U E T W O F O H D E M E A G E T E S X I
R C S X N I V T A I E R R Z Y G K V M B
C T N F I V C I G O R T Y X E H X G W D
D I O T W D Y R O I G I Z L V L P V Q N
J O C S E W L A U R E O U Q W O S A T I
I N O I U M F G B D L N C C Y O N M R F
L K P L E D U O I A M O R T I Z E J B G
M S Y Y U R V L Q D F P T E E R T K C W

You use i++ in both for loops. The second one should use it++.

Related

Output certain string in C++

I'm doing this exercise:
Write a program to get a number N and a sequence of numbers and characters. As a result the program must output first all numbers and then the symbols. All members must be separated by space.
So I wrote this code:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cctype>
using namespace std;
int main()
{
const size_t N = 1000;
char a[N];
size_t n = 0;
cin >> n;
if ( N < n ) n = N;
for ( size_t i = 0; i < n; i++ )
{
cin >> a[i];
}
auto it = stable_partition( a, a + n, []( unsigned char c ){ return ::isdigit( c ); } );
stable_partition( it, a + n, []( unsigned char c ){ return ::isalpha( c ); } );
for ( size_t i = 0; i < n; i++ )
{
cout << a[i] << ' ';
}
return 0;
}
Now when the program gets following input:
1000 < h X " O d ? 3 s P ? ^ ; p l | ^ ^ ) I & B " 2 h h v a a r M N 2 Q 2 a ! a ) N T s $ J [ Y X , 7 | = + m - f $ ] h c 0 x A 8 l d x m + C + y Z 9 U d E Y b = 6 ^ n ] y P 1 # Y ^ . U 1 e 1 3 w U d y E / M i L 1 T B Y . 7 t ' . o g g . ` ' & k # [ R % t O - \ # Y [ T k V ' H t L H I C _ t . K S l t y # B + - " U Z 4 B M l C 0 L ^ ] v c 0 B z K P . ~ i } y ^ q C V [ c 5 Y U L % N V - 8 < a [ y [ P J ) k Q I ` ~ q z d ) Y / Q b M [ h E 7 f { V S ( F K ` ? 4 ? _ C - + O X 6 n V a & $ p w C C D U 3 B y w : s M < z A D | 7 Q ? i & 4 9 > ! + e E [ # ; i b } $ # E L d ` w B 1 ~ N ; s x B 6 p W 7 g m o = _ 7 B C ' i 3 & Y r G 3 j g a Z j . ^ z ^ U . 1 7 > t [ o - G " b ~ ` ( h : R ~ R ~ F E l Q S 0 J j F ^ x 4 u K y 4 F | X C ~ B ? O r + } | " j A C % w ? v w | S E = c 4 6 . # H e q D a & 1 c % P | B " } g 0 9 | 7 n . # B 7 7 ` H v Q I i B g r r Q ; Q 5 c , ` C y x ( J m 1 W h # 6 I a X _ g l 9 j l # 8 > d < E ^ O { 2 _ l 8 u f L _ _ P F w > i ( x 5 4 f X 1 b 9 v S p I < V q 0 [ E I P j > ' $ h Q , I 5 7 R 5 I T T ; I R 4 V E ~ . Q " # n s u & 0 8 f 6 G U n ] 8 q u 6 7 j 2 T ! p ' " J ! t 5 E - E c ] r 2 : T ^ T j X V k 2 k [ z P R / m , / M ; I 5 d ( & b ^ Z m Z T " Q m t A a E d _ } j R H S [ o g | - ! F " $ J L E } J C b Z C a # & & r R T . " R o e V h / 9 t T < S ; l U 8 O q S _ Y t # L l 9 Y { { . o I Y G Y [ f Y \ K j S F ; r , O | 9 E 6 9 C T X p v G " ! ; M _ 3 K $ o $ . ] N q L > n = * M c a i x s J S v = # l D : ' 0 { f e d 7 1 E J G \ K L { v n C T / h y G c Q N s C C \ = & ` , 5 6 g G . P 6 - r > F z L s b ( c [ J P { . o T a 7 a / ? h 4 6 ; W $ h c i m f x , D l _ 6 9 C % l a L r q ' F 1 m } 5 l c 0 ( V I [ V Y . 0 G H 1 e j K = n p p b 9 v h V ; x - / o z R 3 n ' ; L P W } 3 > y Z = 8 9 ' - | x u , 2 l z t } | > w k 8 | P 8 , g k U ` 8 F , < 8 g & z c ^ Z 0 s < \ ^ D { 3 | ! C H R y b 3 ` C q I g p 0 $ e S * k e < 8 < q & - 1 A X < b / a K q x q w ) b I ? T h - e ! A . z J p U l } * P G H ! m - F ' = a : B ~ ! . k 4 A " U / Y c 4 b t u } < U K X y 1 H A # { E 3
It forgets to output symbols like < ` ".
As result I get false output with "eaten" symbols.
My program executes:
3 2 2 2 7 0 8 9 6 1 1 1 3 1 7 4 0 0 5 8 7 4 6 3 7 4 9 1 6 7 7 3 3 1 7 0 4 4 4 6 1 0 9 7 7 7 5 1 6 9 8 2 8 5 4 1 9 0 5 7 5 4 0 8 6 8 6 7 2 5 2 2 5 9 8 9 9 6 9 3 0 7 1 5 6 6 7 4 6 6 9 1 5 0 0 1 9 3 3 8 9 2 8 8 8 8 0 3 3 0 8 1 4 4 1 3 h X O d s P p l I B h h v a a r M N Q a a N T s J Y X m f h c x A l d x m C y Z U d E Y b n y P Y U e w U d y E M i L T B Y t o g g k R t O Y T k V H t L H I C t K S l t y B U Z B M l C L v c B z K P i y q C V c Y U L N V a y P J k Q I q z d Y Q b M h E f V S F K C O X n V a p w C C D U B y w s M z A D Q i e E i b E L d w B N s x B p W g m o B C i Y r G j g a Z j z U t o G b h R R F E l Q S J j F x u K y F X C B O r j A C w v w S E c H e q D a c P B g n B H v Q I i B g r r Q Q c C y x J m W h I a X g l j l d E O l u f L P F w i x f X b v S p I V q E I P j h Q I R I T T I R V E Q n s u f G U n q u j T p J t E E c r T T j X V k k z P R m M I d b Z m Z T Q m t A a E d j R H S o g F J L E J C b Z C a r R T R o e V h t T S l U O q S Y t L l Y o I Y G Y f Y K j S F r O E C T X p v G M K o N q L n M c a i x s J S v l D f e d E J G K L v n C T h y G c Q N s C C g G P r F z L s b c J P o T a a h W h c i m f x D l C l a L r q F m l c V I V Y G H e j K n p p b v h V x o z R n L P W y Z x u l z t w k P g k U F g z c Z s D C H R y b C q I g p e S k e q A X b a K q x q w b I T h e A z J p U l P G H m F a B k A U Y c b t u U K X y H A E < " ? ? ^ ; | ^ ^ ) & " ! ) $ [ , | = + - $ ] + + = ^ ] # ^ . / . ' . . ` ' & # [ % - \ # [ ' _ . # + - " ^ ] . ~ } ^ [ % - < [ [ ) ` ~ ) / [ { ( ` ? ? _ - + & $ : < | ? & > ! + [ # ; } $ # ` ~ ; = _ ' & . ^ ^ . > [ - " ~ ` ( : ~ ~ ^ | ~ ? + } | " % ? | = . # & % | " } | . # ` ; , ` ( # _ # > < ^ { _ _ _ > ( < [ > ' $ , ; ~ . " # & ] ! ' " ! - ] : ^ [ / , / ; ( & ^ " _ } [ | - ! " $ } # & & . " / < ; _ # { { . [ \ ; , | " ! ; _ $ $ . ] > = * = # : ' { \ { / \ = & ` , . - > ( [ { . / ? ; $ , _ % ' } ( [ . = ; - / ' ; } > = ' - | , } | > | , ` , < & ^ < \ ^ { | ! ` $ * < < & - < / ) ? - ! . } * ! - ' = : ~ ! . " / } < # {
Whereas the right answer is:
3 2 2 2 7 0 8 9 6 1 1 1 3 1 7 4 0 0 5 8 7 4 6 3 7 4 9 1 6 7 7 3 3 1 7 0 4 4 4 6 1 0 9 7 7 7 5 1 6 9 8 2 8 5 4 1 9 0 5 7 5 4 0 8 6 8 6 7 2 5 2 2 5 9 8 9 9 6 9 3 0 7 1 5 6 6 7 4 6 6 9 1 5 0 0 1 9 3 3 8 9 2 8 8 8 8 0 3 3 0 8 1 4 4 1 3 < h X " O d ? s P ? ^ ; p l | ^ ^ ) I & B " h h v a a r M N Q a ! a ) N T s $ J [ Y X , | = + m - f $ ] h c x A l d x m + C + y Z U d E Y b = ^ n ] y P # Y ^ . U e w U d y E / M i L T B Y . t ' . o g g . ` ' & k # [ R % t O - \ # Y [ T k V ' H t L H I C _ t . K S l t y # B + - " U Z B M l C L ^ ] v c B z K P . ~ i } y ^ q C V [ c Y U L % N V - < a [ y [ P J ) k Q I ` ~ q z d ) Y / Q b M [ h E f { V S ( F K ` ? ? _ C - + O X n V a & $ p w C C D U B y w : s M < z A D | Q ? i & > ! + e E [ # ; i b } $ # E L d ` w B ~ N ; s x B p W g m o = _ B C ' i & Y r G j g a Z j . ^ z ^ U . > t [ o - G " b ~ ` ( h : R ~ R ~ F E l Q S J j F ^ x u K y F | X C ~ B ? O r + } | " j A C % w ? v w | S E = c . # H e q D a & c % P | B " } g | n . # B ` H v Q I i B g r r Q ; Q c , ` C y x ( J m W h # I a X _ g l j l # > d < E ^ O { _ l u f L _ _ P F w > i ( x f X b v S p I < V q [ E I P j > ' $ h Q , I R I T T ; I R V E ~ . Q " # n s u & f G U n ] q u j T ! p ' " J ! t E - E c ] r : T ^ T j X V k k [ z P R / m , / M ; I d ( & b ^ Z m Z T " Q m t A a E d _ } j R H S [ o g | - ! F " $ J L E } J C b Z C a # & & r R T . " R o e V h / t T < S ; l U O q S _ Y t # L l Y { { . o I Y G Y [ f Y \ K j S F ; r , O | E C T X p v G " ! ; M _ K $ o $ . ] N q L > n = * M c a i x s J S v = # l D : ' { f e d E J G \ K L { v n C T / h y G c Q N s C C \ = & ` , g G . P - r > F z L s b ( c [ J P { . o T a a / ? h ; W $ h c i m f x , D l _ C % l a L r q ' F m } l c ( V I [ V Y . G H e j K = n p p b v h V ; x - / o z R n ' ; L P W } > y Z = ' - | x u , l z t } | > w k | P , g k U ` F , < g & z c ^ Z s < \ ^ D { | ! C H R y b ` C q I g p $ e S * k e < < q & - A X < b / a K q x q w ) b I ? T h - e ! A . z J p U l } * P G H ! m - F ' = a : B ~ ! . k A " U / Y c b t u } < U K X y H A # { E
How can I fix it?

c++ 2 dimension array rotation

I am writing a program in C++ to display a 2 dimensional array and then rotate it 90, 180, and 270 degrees. The array is a capital 'E' made of E's and blanks. I have written the program but when I run it it does print the E's rotated but vertically (top to bottom), my professor wants them rotated but to print horizontally (left to right).
By my logic I would have to edit my code to do:
-> print array row 1
-> rotate90
-> print array row 1
-> rotate90 (to revert to original)
-> rotate180
-> print array row 1
-> rotate180 (original)
-> rotate270
-> print array row 1
-> rotate 270 (original) ->
...continue 7 times until all rows are printed.
I know there is an easier way but I can't wrap my head around how to do it.
#include <iostream>
using namespace std;
char eArray[][7]{
{'E','E','E','E','E','E','E'},
{'E',' ',' ',' ',' ',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E','E','E','E','E',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E','E','E','E','E','E','E'},
};
int n = 7;
void rotateArray90(char a[][7]){
for (int i = 0; i < n; i += 1) {
for (int j = i+1; j < n; j += 1) {
swap(eArray[i][j], eArray[j][i]);
}}}
void flipArray180(char a[][7]){
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < n/2; j += 1) {
swap(eArray[i][j], eArray[i][n-1-j]);
}}}
void rotateArray270(char a[][7]){
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < n/2; j += 1) {
swap(eArray[i][j], eArray[n-1-i][j]);
}}}
void printArray(char a[][7]){
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
cout << eArray[i][j] <<" ";
}cout << endl;
}}
int main(){
printArray(eArray);
cout <<"\n";
rotateArray90(eArray);
printArray(eArray);
cout <<"\n";
rotateArray90(eArray);
flipArray180(eArray);
printArray(eArray);
cout <<"\n";
flipArray180(eArray);
rotateArray270(eArray);
printArray(eArray);
cout <<"\n";
rotateArray270(eArray);
}
Thank you for the help.
I am not sure I got you, but what you doing here is not a rotation (in the sense of matrix rotations). When you apply rotateArray90(eArray) two times you should get your array rotated 180 degrees, but instead it turns back to original array.
Here is my rotation method, take a look whether it helps. When you use it, you do not need separate methods for rotating at 180, 270 etc degrees, you just applies it 2, 3, ... times. After 4th rotation array returns back to original. It works with any rectangle array, not only n x n. And you do not have to use raw pointers, use instead containers.
template<typename T>
void printArray(const vector<vector<T>> &vch) {
for (int i = 0; i < vch.size(); ++i) {
for (int j = 0; j < vch[0].size(); ++j) {
cout << vch[i][j] << " ";
}
cout << endl;
}
}
template<typename T>
void rotateArray(vector<vector<T>> &v)
{
vector<vector<T>>rotated;
for (size_t i = 0; i < v[0].size(); i++) {
vector<T> newRow;
for (int j = v.size() - 1; j >=0; j--) {
newRow.push_back(v[j][i]);
}
rotated.push_back(newRow);
}
v = rotated;
}
Here is an example how to use:
int main() {
vector<vector<char>> vch = {
{'E', 'E', 'E', 'E', 'E', 'E', 'E'},
{ 'E',' ',' ',' ',' ',' ',' ' },
{ 'E',' ',' ',' ',' ',' ',' ' },
{ 'E','E','E','E','E',' ',' ' },
{ 'E',' ',' ',' ',' ',' ',' ' },
{ 'E',' ',' ',' ',' ',' ',' ' },
{ 'E','E','E','E','E','E','E' },
};
printArray<char>(vch);
cout << "\n";
rotateArray<char>(vch);
printArray<char>(vch);
cout << "\n";
rotateArray<char>(vch);
printArray<char>(vch);
cout << "\n";
rotateArray<char>(vch);
printArray<char>(vch);
cout << "\n";
return 0;
}
//orignal
E E E E E E E
E
E
E E E E E
E
E
E E E E E E E
// rotated 90
E E E E E E E
E E E
E E E
E E E
E E E
E E
E E
// rotated 180
E E E E E E E
E
E
E E E E E
E
E
E E E E E E E
// rotated 270
E E
E E
E E E
E E E
E E E
E E E
E E E E E E E
// rotated 360
E E E E E E E
E
E
E E E E E
E
E
E E E E E E E

Randoming symbols from a-Z

I need to generate a string from random characters in range of a-Z. How can i do it?
Atm, i can only suggest randoming numbers in range of 1-52 and then reinitialize numbers with charecters. But that sounds awful.
CALL RANPERM seems well suited for this task.
25 data shuffle(keep=a: drop=addr);
26 array a[52] $1;
27 addr = addrlong(a1);
28 call pokelong(cat(collate(65,90),collate(97,122)),addr,52);
29 put #20 a[*];
30 seed=1234567890123;
31 do n=1 to 10;
32 call ranperm(seed,of a[*]);
33 put seed= #20 a[*];
34 output;
35 end;
36 stop;
37 run;
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z
seed=295748501 i U I K e d H R b E D k u N c v B M T Z G V n A s W h r q Q j F J o x X O p g t Y w z P C m S l L f a y
seed=766887342 z F v U N G M q u H P Q Z d y b X l a D f p Y w m o r V A B n J I j e g c S x R T K O L i s k C h E W t
seed=618158873 L v x k T r c m S e F w n Z K G P t q E W C h M z X p d N o H Y V i s Q u y f B J O g l U R D b I a j A
seed=632090736 J l H Y T R A p N x C v U b w K V W d X t L y E G o Q B M z I S n q k f s O F P D j a r m i c g Z h e u
seed=1911942895 t G j Z e K q H x m L F A g T h v W a y V C B M k I r J S R P s Y Q p w O D E l X U o z u N d f n i b c
seed=479260872 Y Q i d P R z S G B a D K Z X j M E b h T m c e C I n x y w o A k r U f H u l N W s v J p L q O t g V F
seed=1143672881 L X i f a u m K T g x p E M o B q Z s c t A Q I P G D z U d H S y V J F j h r e l v C b k Y W R O w N n
seed=869134054 F D U g G w A Y J o e p r b H z q d l I L M v R W h y m t S n V x T P C K B k O Q i j s c N f Z a X E u
seed=481233318 n h P j V M y S W s m F a G l q f d o O A u T I E p k z b e C X t v Q H i w c U g Z K B R N L x r D J Y
seed=1445828380 d P t o N h c R r M l n E y g F x z S a W L G p w T Q k B H e v I K i b f q X C m Y D J Z V j A U u O s
You can use the byte function, along with a random number, to generate a random character. Just loop the desired number of times and combine the results with call cats. The byte function returns the relevant character from the ASCII table. For your example you need ASCII numbers 65-90 (A-Z) and 97-122 (a-z). The following code will generate a random sequence of 6 characters.
data _null_;
length z $6 y $1;
do x=1 to 6;
do until (rank(y) in (65:90,97:122)); /* ASCII characters A-Z, a-z */
y=byte(65+floor((1+122-65)*rand("uniform"))); /* Generate random integer between 65 and 122 */
end;
call cats(z,y); /* concatenate values */
end;
putlog z;
run;

How to determine which items are on the critical path in GAMS?

This is my current code on GAMS. Yes, I know it can be more concise, but bear with me here:
set activity /a*q/;
parameter duration (activity) "in days"
/a 15, b 4, c 5, d 10, e 4, f 15, g 15, h 5, i 5, j 10, k 4, l 3, m 4, n 20, o 5,
p 2, q 2/;
alias (activity, x, y);
set prec(x,y) "Precedence Order"
/A.(B,C,D)
(B,C,D).E
E.(F,G,H)
F.N
G.(K,I)
H.I
K.M
I.J
J.L
L.M
M.N
N.O
O.P
P.Q/ ;
Free Variable
T Completion Time;
Nonnegative Variables
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q;
equations
t_A, t_B, t_C, t_D, t_E, t_F, t_G, t_H, t_I, t_J, t_K, t_L, t_M, t_N, t_O, t_P,
t_Q, s_AB, s_AC, s_AD, s_BE, s_CE, s_DE, s_EF, s_EG, s_EH, s_FN, s_GK, s_GI,
s_HI, s_KM, s_IJ, s_JL, s_LM, s_MN, s_NO, s_OP, s_PQ;
t_A.. T =G= A + 15;
t_B.. T =G= B + 4;
t_C.. T =G= C + 5;
t_D.. T =G= D + 10;
t_E.. T =G= E + 4;
t_F.. T =G= F + 15;
t_G.. T =G= G + 15;
t_H.. T =G= H + 5;
t_I.. T =G= I + 5;
t_J.. T =G= J + 10;
t_K.. T =G= K + 4;
t_L.. T =G= L + 3;
t_M.. T =G= M + 4;
t_N.. T =G= N + 20;
t_O.. T =G= O + 5;
t_P.. T =G= P + 2;
t_Q.. T =G= Q + 10;
s_AB.. A + 15 =L= B;
s_AC.. A + 15 =L= C;
s_AD.. A + 15 =L= D;
s_BE.. B + 4 =L= E;
s_CE.. C + 5 =L= E;
s_DE.. D + 10 =L= E;
s_EF.. E + 4 =L= F;
s_EG.. E + 4 =L= G;
s_EH.. E + 4 =L= H;
s_FN.. F + 15 =L= N;
s_GK.. G + 15 =L= K;
s_GI.. G + 15 =L= I;
s_HI.. H + 5 =L= I;
s_KM.. K + 4 =L= M;
s_IJ.. I + 5 =L= J;
s_JL.. J + 10 =L= L;
s_LM.. L + 3 =L= M;
s_MN.. M + 4 =L= N;
s_NO.. N + 20 =L= O;
s_OP.. O + 5 =L= P;
s_PQ.. P + 2 =L= Q;
model thesis /all/;
solve thesis using lp minimizing t;
display t.l;
This code gives me a ton of details, such as what days I should start each activity. However, it doesn't tell me (as far as I can see), which activities are on the critical path. How can I determine this using GAMS?
A dependency lies on the critical path if the marginal on the dependency equation (s_AB etc. in your formulation) is -1. You can check this in the listing (.lst) file or by using the code below:
set activity /a*q/;
parameter duration (activity) "in days"
/a 15, b 4, c 5, d 10, e 4, f 15, g 15, h 5, i 5, j 10, k 4, l 3, m 4, n 20, o 5,
p 2, q 2/;
alias (activity, x, y);
set prec(x,y) "dependency Order"
/A.(B,C,D)
(B,C,D).E
E.(F,G,H)
F.N
G.(K,I)
H.I
K.M
I.J
J.L
L.M
M.N
N.O
O.P
P.Q/;
Free Variable
T Completion Time;
Nonnegative Variables
start(x);
equations
t_bound(x)
dependency(x,y);
t_bound(x).. T =G= start(x) + duration(x);
dependency(x,y)$prec(x,y).. start(x) + duration(x) =l= start(y);
model thesis /all/;
solve thesis using lp minimizing t;
set criticalpath(x,y);
criticalpath(x,y) = 1$(dependency.M(x,y) < 0);
display t.l, criticalpath;

Crossword game: read 2d given array and search diagonally c++

This is a crossword game. I wanna read an array diagonally.
I should find some word in all over the 2d given array
this array read from a given file
and it is n*m size; m not always = n
How can I read 2d given diagonally like this:
Example:
m = 4
n = 4
b o o k
z a k o
s l l e
x y z l
ball: found
[b] o o k
z [a] k o
s l [l] e
x y z [l]
foo: not found
Here is the code:
char ReadArray(char* array, int r, int c, int n, int m)
{
return (r > 0 && r <= n && c > 0 && c <= m) ?
array[n * (r - 1) + (c - 1)] : '\0';
}
char readrc(char* array, int r, int c, int n, int m)
{
return (r > 0 && r <= n && c > 0 && c <= m) ?
array[n * (r - 1) + (c - 1)] : '\0';
}
void read_down_right(char* array, int n, int m, vector<string>& list)
{
for (int sc = 2 - n; sc <= m - 1; sc++)
{
string str = "";
for (int r = 1, c = sc; r <= n; r++, c++)
{
char chr = readrc(array, r, c, n, m);
if (chr != '\0')
str += chr;
}
list.push_back(str);
}
}
void read_down_left(char* array, int n, int m, vector<string>& list)
{
for (int sc = 2; sc <= m + n - 2; sc--)
{
string str = "";
for (int r = 1, c = sc; r <= n; r++, c--)
{
char chr = readrc(array, r, c, n, m);
if (chr != '\0')
str += chr;
}
list.push_back(str);
}
}
pass a reference to a blank list each time. list contains all possible strings afterwards, do a linear search.