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}.
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.
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.)
#include <iostream> // includes...
#include <cstdlib> // ...........
using std :: cin; // usings.....
using std :: cout; // ............
using std :: endl; // ............
constint COLS_SIZE = 4; // defining const for size of columns
constint 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