Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program in C++ that reads a 2D integer array from a file "data.txt". File contains row and column size and the data of array. Now read an integer array from user. Your task is to find the ID array in 2D array if ID array is present in 2D array return true else false.
Example 1
data.txt
4 4
11 12 13 14
25 26 27 28
39 30 31 32
43 44 45 46
Array: 25 26 27 28
True
Example 2
data.txt
4 4
11 12 13 14
25 26 27 28
39 30 31 32
43 44 45 46
Array: 13 14 25 26
True
Example 3
data.txt
4 4
11 12 13 14
25 26 27 28
39 30 31 32
43 44 45 46
Array: 13 15 64 45
False

What I have tried:

#include<iostream>
#include<fstream>
using namespace std;

bool Find_1D_into_2D(int **p, int q, int r)
{
	int *Arr = new int[q], i = 0;
	bool Found = true;
	cout << "Enter Elenments of 1D array to find:\n";
	for(; i < q; i++)
	{
		cin >> Arr[i];
	}
	for(int j = 0,l=0; j < q; j++)
	{
		for(int k = 0; k < r; k++)
		{
			Found = false;
			if(p[j][k]==Arr[l])
			{
				Found = true; l++;
			}
			else
			{
				Found = false; l = 0;
			}
		}
		if(Found&&l == i)
		{
			break;
		}
	}
	return Found;
}
int main()
{
	int Row = 0, Column = 0;
	ifstream Read("Sample.txt");
	Read >> Row >> Column;
	Read.close();
	int **arr = new int *[Row] {};
	for(int i = 0; i < Row; i++)
	{
		arr[i] = new int[Column] {};
	}
	cout << "Enter Elements of 2D array: " << endl;
	for(int i = 0; i < Row; i++)
	{
		for(int j = 0; j < Column; j++)
		{
			cin >> arr[i][j];
		}
	}
	if(Find_1D_into_2D(arr, Row, Column))
	{
		cout << "True";
	}
	else
	{
		cout << "False";
	}
	cout << endl;
	return 0;
}
Posted
Comments
Richard MacCutchan 24-Jun-20 7:21am    
You need to tell us what the problem is (again). If you do not know how to create a 2D array there are many examples that Google will find for you. If it is something else then we need the details. Incidentally, I don't quite understand why you have started a new question when you still haven't finished work on Auction management system | C++ | functions, pointers, dynammically or memory leakage free[^].
NaumanMalik 24-Jun-20 7:41am    
it should read row from only forward direction only not column... like
11 12 13 14
15 16 17 18
19 20 21 22
23 24 25 26
1D ARR: 11 12 13 14 TRUE
if i input 1D array: 13 14 from first row and 15 16 from second row but they are each after one another in line like 13 14 15 16 in 2D array: then it is true

Solution for above problem
#include<iostream>
#include<fstream>
using namespace std;

bool Find_1D_into_2D(int **p, int q, int r)
{
	int *Arr = new int[q], i = 0;
	bool Found = true;
	cout << "Enter Elenments of 1D array to find:\n";
	for(; i < q; i++)
	{
		cin >> Arr[i];
	}
	for(int j = 0,l=0; j < q; j++)
	{
		for(int k = 0; k < r; k++)
		{
			if(Found&&l == i)
			{
				break;
			}
			Found = false;
			if(p[j][k]==Arr[l])
			{
				Found = true; l++;
			}
			else
			{
				Found = false; l = 0;
			}
		}
		
	}
	return Found;
}
int main()
{
	int Row = 0, Column = 0;
	ifstream Read("Sample.txt");
	Read >> Row >> Column;
	Read.close();
	int **arr = new int *[Row] {};
	for(int i = 0; i < Row; i++)
	{
		arr[i] = new int[Column] {};
	}
	cout << "Enter Elements of 2D array: " << endl;
	for(int i = 0; i < Row; i++)
	{
		for(int j = 0; j < Column; j++)
		{
			cin >> arr[i][j];
		}
	}
	if(Find_1D_into_2D(arr, Row, Column))
	{
		cout << "True";
	}
	else
	{
		cout << "False";
	}
	cout << endl;
	return 0;
}
 
Share this answer
 
v2
Comments
Patrice T 24-Jun-20 8:35am    
Is it a solution or your new code with problems ?
NaumanMalik 24-Jun-20 9:07am    
solution for my own problem
in your
Find_1D_into_2D
don't forget to:

where:
C++
int *Arr = new int[q];


is followed by

C++
delete [] Arr;
 
Share this answer
 
Comments
NaumanMalik 24-Jun-20 7:50am    
okay
First you have to decide what "is present in" means.
Does it mean that a row contains it:
123 

in

456
123
789
Or a column?
123

in
417
528
639
Or both? What about diagonals? Forward only, or does backward count? What if it spans two rows?
123

in

451
236
789


When you have decides that, you can start looking for it.

Looking for a sequence in a longer sequence is pretty easy:
1) Set an variable to the start of the sequence you are searching for: call it seeker
2) Loop through the long sequence, value by value.
3) Does the value match the current element of seeker?
3.1) If it does, advance seeker. Is seeker at the end?
3.1.1) If it is, return true - you found it.
3.1.2) If not, continue the loop.
3.2) If not, set seeker back to the start of the search sequence.
4) It's not there, return false.


Try it manually, and you'll see what I mean - you should then be able to implement it.
 
Share this answer
 
Comments
NaumanMalik 24-Jun-20 7:40am    
Yes it should read row from only forward direction only not column... like
11 12 13 14
15 16 17 18
19 20 21 22
23 24 25 26
1D ARR: 11 12 13 14 TRUE
if i input 1D array: 13 14 from first row and 15 16 from second row but they are each after one another in line like 13 14 15 16 in 2D array: then it is true
NaumanMalik 24-Jun-20 7:40am    
im trying on paper what you suggested

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900