help with program

Hi im a beginner in c++
there is this example that i am supposed to do and i dont really know how. iw ould appreciate some help.
the example is:
write a program that uses a two- dimensional array and checks if one of the rows contains an element from all the other rows . for example :
{1, 2, 3, 4 ,5 ,6} contains {1, 2, 3, 1, 2, 3}, {3, 3, 3, 3, 1, 1} but not
{1, 2, 3, 4, 5, 7}.
Do you know how to check whether array contains a specific value?

When you have a list of values, you can repeat the above with each of them. If any says "no", then all of list is not "contained".

When you have several lists, you can repeat the above for each list.
i dont really understand how i can check if all the values of each row are in another
Can you check whether one value is in that specific row?
Don't understand.
Did you understand what needs to be done?
Did you understand what needs to be done?

Yes.

In programming we usually don't solve complex problems in one go. We break the problem into smaller, simpler tasks first. Then we create solutions for the simple problems. Finally, we use those solutions to handle the original problem.

Now, write an implementation for this function:
bool contains( int * foo, int N, int bar );
The foo points to an integer array that has N elements.
The function returns true, if at least one element in that array has value bar.

http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/arrays/
What is the *
Within that context it means that the 'foo' is a pointer to type int object.

http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/doc/tutorial/control/
I haven't learned pointers yet. Is there a way to do it without pointers
For now, pretend that it is an array.
What is ? The pointer?
Yes, the pointer. Pretend that the foo is an array of N integers within function
'contains' and implement the function so that it returns true if value 'bar' is in the array. (Obviously, if bar is not in the array, then the function must return false.)
closed account (L1vUM4Gy)
Can you show us what you have done?

What is the *

If you are afraid of using new concepts you haven't learned yet, you can also write it like this :
bool contains( int foo[], int N, int bar );

This time, foo is really an array. And now you can use it with ease.
Itago wrote:
This time, foo is really an array.

No. You can write foo[] there, but the compiler automatically converts it into *foo.
See, for example: http://stackoverflow.com/questions/33291624/why-do-arrays-in-c-decay-to-pointers
How can I show you my code
How can I show you my code
Paste it here, between code tags like this:
[code] your code here [/code]
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
90
91
92
93
94
95
96
 #include <iostream>  //  includes...
#include <cstdlib>   //  ...........

//=============================================================================

using std :: cin;    //  usings.....
using std :: cout;   // ............
using std :: endl;   // ............

//=============================================================================

const int COLS_SIZE = 5;  // defining const for size of columns
const int ROW_SIZE = 4;   // defining const for size of rows

//=============================================================================


int main()
{
	int array [ROW_SIZE][COLS_SIZE];

	int i =0,j=0,x=0,y=0,count=0,index=-1;
	bool check = true;

	cout << "please enter 20 numbers (five each time)" <<endl;
	for (i=0;i < ROW_SIZE; i++)

	{
		for (j =0; j<COLS_SIZE; j++)
			cin >> array[i][j];
	}









	for (i =0 ; i < ROW_SIZE; i++)
	{

        check = true;
		for (x=0; x < ROW_SIZE && check ; x++)
		{


			for (y=0; y< COLS_SIZE && check;y++)
			{


				for (j=0;j<COLS_SIZE && check ;j++)
				{


					if (array[i][j] != array [x][y])
					{
						count ++;

					}

					if (count == COLS_SIZE)
					{

						check =false;

					}

				}

			}
		}


		if(check)
		{

			index = i;
			break;

	    }


        
	}
    cout <<index;







	return EXIT_SUCCESS;
} 
Nice start. (Too many unnecessary empty rows though.)

Lets get back to:
checks if one of the rows contains an element from all the other rows

What does that mean, actually?

"one of the rows" Which row?

"contains an element from other row" Does that mean "at least one element of row X appears also in some other row"?

"from all the other rows" Does that mean that at least one element of each row can also be found from row X?

In the example
{1, 2, 3, 4 ,5 ,6}
{1, 2, 3, 1, 2, 3}
{3, 3, 3, 3, 1, 1}
{1, 2, 3, 4, 5, 7}

all rows contain 1 and 3. Therefore, you can look at any of those rows, see that it has 1 and 3 and then see that all the other rows have them too.

However, was that the real question?


Edit:
The example's "answer" would fit a different question: Which rows are such that all of their elements can be found from the first row?
Last edited on
is there a way to send a private message?
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
#include <iostream>  //  includes...
#include <cstdlib>   //  ...........


using std :: cin;    //  usings.....
using std :: cout;   // ............
using std :: endl;   // ............



const int COLS_SIZE = 4;  // defining const for size of columns
const int ROW_SIZE = 5;   // defining const for size of rows




int main()
{
	int array [ROW_SIZE][COLS_SIZE],
		i=0,j=-1,check2=0 ;
	bool check = false;


	for (i=0;i < ROW_SIZE; i++)
		for (j =0; j<COLS_SIZE; j++)
			cin >> array[i][j];
	i=j=0;

	if (array[i][j]<=array[i][j+1]&& array[i][j]<=array[i+1][j])
	{
		check =true;
		check2 =1;
	}
	else
		check = false;

	if(array[i][j+1]<=array[i][j+2]&& array[i][j+1]<=array[i+1][j+1]&&array[i][j+1]<=array[i+2][j]&&check&&array[1][0]<=array[0][2]&&array[1][0]<=array[1][1]&&array[1][0]<=array[2][0])
	{
		check2=2;
		check=true;
	}
	else
		check=false;

	if (array[0][2]<=array[0][3]&& array[0][2]<=array[1][2]&&array[0][2]<=array[2][1]&&array[0][2]<=array[3][0]
	&&  array[2][0]<=array[0][3]&&array[1][1]<=array[1][2]&&array[1][1]<=array[2][1]&&array[1][1]<=array[3][0]
	&&  array[1][1]<=array[0][3]&&array[2][0]<=array[1][2]&&array[2][0]<=array[2][1]&&array[2][0]<=array[3][0]
			&&check)
		check2 =3;
	else
		check = false;

	switch(check2)
	{

	case 1:
		cout <<"2\n";
		break;
	case 2:
		cout <<"3\n";
		break;
	case 3:
		cout <<"4\n";
		break;
	default:
		cout<<"1\n";

	}

	return EXIT_SUCCESS;
}

how can i make this code better?
can i use for's?
i didnt really know how to play around with the indexes to get it to do what i wanted so i used ifs.
this program is supposed to check the three left sided diagonals of a two dimensional array,and check witch diagonal has each number that is greater than or equal to the diagaonal before it
Topic archived. No new replies allowed.