Extraction of Numbers from array

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

I really do not know how to go about this.
There must be some rules about this, otherwise your program is just:

1
2
3
4
5
6
#include <iostream>

int main()
{
  std::cout << "76 89 23 1";
}


What are the rules? How do you know that the output numbers are 76, 89, 23 and 1?
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.

i hope you understand my question.
1. "I have 12 digits. I have to display them in three rows, four digits per row."

That should be relatively easy.


2. What does "extract digits from array" mean? What does "form an number from digits" mean?

Do you actually have 12 digits and 4 numbers? Is it possible that all numbers cannot be formed from the available digits?
something like this?
1
2
3
4
5
6
7
8
9
10
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...
Last edited on
FlintMT wrote:
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?
Yes icy1
That's exactly what the question asks to do.
FlintMT wrote:
That's exactly what the question asks to do.

Then why didn't you say that in the first place instead of the braindead "extract nos"?
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

// Parses stream of characters into rows of strings
vector<string> ParseRowStrings(istream& is)
{
    vector<string> rows;
    ostringstream oss;
    string line;
    istringstream iss;
    char c;
    while (getline(is, line))
    {
        if (line.empty())
            continue;
        iss.clear();
        iss.str(line);
        oss.str(string());
        while (iss >> c)
            oss << c;
        rows.emplace_back(oss.str());
    }
    return rows;
}

void Show(vector<string>& rows)
{
    for (auto& row : rows)
        cout << row << endl;
    cout << endl;
}

// Given a bunch of strings, try to find some substrings
//   Currently, searches just Forwards (left->right).
//   Prints the ones found and not found.
void Search(const vector<string>& rows, const vector<string>& words)
{
    int count = 0;                     // Total words found
    vector<bool> found(words.size());  // Marking found word indices
    size_t pos;                        // Position in row when found

    for(int r=0; r<rows.size(); ++r)
    {
        const string& row(rows[r]);
        for(int i=0; i<words.size(); ++i)
        {
            if (found[i])
                continue;

            const string& word(words[i]);
            pos = row.find(word);
            if (pos != string::npos)
            {
                cout << "Row #" << r << ": found word '" << word << "' at position "<< pos << endl;
                count += 1;                
                if (count == words.size())
                    return;
                found[i] = true;
            }
        }
    }

    cout << "\nCould not find: ";
    for(int i=0; i<found.size(); ++i)
        if (!found[i])
            cout << words[i] << ' ';
    cout << endl;
}

int main()
{
    const char* data_text = R"(5 7 8 9
1 7 6 4
0 2 3 5
)";

    istringstream iss(data_text);
    auto rows = ParseRowStrings(iss);
    Show(rows);

    vector<string> words { "76", "89", "23", "1", "77", "67" };
    Search(rows, words);

    return 0;
}


Running at https://repl.it/@icy_1/ThunderousUnwelcomeModem

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 
Last edited on
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.
Topic archived. No new replies allowed.