Hello guys, i'm a newbie here.
I have an assignment to write a C++ program to extract nos 76,89, 23 & 1 given the table below
5--7--8--9
1--7--6--4
0--2--3--5
There is a given table. It is an array of numbers;
5, 7, 8, 9
1, 7, 6, 4
0, 2, 3, 5
I am required to first display these array of numbers in the output in form of a matrix. And then from the array of numbers, i have to extract numbers to form 76, 89, 23 and 1.
int array1{5,7,8,9};
int array2{1,7,6,4};
int array3{0,2,3,5};
cout<<array2[1]<<array2[2]<<", ";
cout<<array1[2]<<array1[3]<<", ";
cout<<array3[1]<<array1[2]<<", ";
cout<<array2[0];
}
if you wanted to show off you could generate all the possible values from 0 to 100 that can be formed by going left to right in the provided digit-matrix.
eg
bool inthere[100] = {false};
//some logic to set values true, eg it would set
5, 57, 7, 78,8,89, 9 for the first row, do you see a pattern there? Loop that pattern.
then you check it..
if inthere[76] then print out that 76 is a possible value.
this really would not be a lot of trouble to code...
I am required to first display these array of numbers in the output in form of a matrix. And then from the array of numbers, i have to extract numbers to form 76, 89, 23 and 1.
Sorta like a word search?
H Z Z Z Z
Z E Z Z Z
Z Z L Z Z
Z Z Z L Z
Z Z Z Z O
For example, if you had this and user asked you to find "HELLO", search grid and try to find it...?
So in your case, you have a matrix and have teh search terms "76", "89", "23", "1", yes?
Still needs more rules for a precise implementation.
In your example, it looks like just Forwards (left->right) ? Are you also allowed to search:
- backwards (right->left)
- down (up->down)
- up (down->up)
- diagonally left->right downwards
- diagonally left->right upwards
- diagonally right->left downwards
- diagonally right->left upwards
?
There are so many possibilities and you're just giving us bread crumbs.
If it's just Forwards (left->right) , it's really a variation of "find substring in string". Simplest would be to combine each row into a string and then leverage standard C++ or C functions to search each row-string for each of the substrings. One optimization would be to mark if a substrings was already found previously and to not search it in subsequent rows.
pseudo-code:
for each row-string
for each substring still needed to be found
find()
if found, mark it as found, print "found xxx in row zzz"
return if no more left to be found
print "the following couldn't be found: " with remaining
5789
1764
0235
Row #0: found word '89' at position 2
Row #1: found word '76' at position 1
Row #1: found word '1' at position 0
Row #2: found word '23' at position 1
Could not find: 77 67
There are more untold possibilities. The "extraction" sounds same as "removal". One could have an extra requirement that each element of the array can be used at most once.
For example:
675
724
contains "67" in two possible ways: horizontal and vertical. If we pick the horizontal, that leaves
..5
724
and that has no "75" in it.
If we had preferred vertical, or searched "75" before the "67", then both would be found.
The overall lesson is that details are important. Not only for code that has to be exact, but also when communicating with others. Exact requirements and complete error messages.