# מבוא למדעי המחשב תרגיל 7

מערכים דו ממדיים ב ++C

פתרון שאלה 1

```
/*************************
targil: 7 sheela 1
lecturer: meir komer
discription: birany search
**************************/

#include <iostream>
using namespace std;

void binarSearch(int array[], int len, int n)
{

int  low = 0, high = len - 1, mid, result = -1;

while (low <= high)
{
mid = (low + high) / 2;
if (n == array[mid])
{
result = mid;
cout << "the number  " << n << " was found at index:" << result << endl;
break;

}
else
{
if (n>array[mid])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
}

if (result == -1)
{
}
}

int main()
{
int arr[10], size = 0, number_x, m = 0;

cout << "enter 10 numbers:" << endl;

do                        //input loop
{
m = 0;
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
size++;                  // to know the array size
if (arr[i] <= arr[i - 1])
{
m = 1;
}

}
if (m == 1)
{
cout << "ERROR" << endl;
size = 0;       // becauze in ERROR case we need to do the array input agine

}
} while (m == 1);

cout << "enter 1 number:" << endl;
cin >> number_x;

binarSearch(arr, size, number_x);  // go to the function to do a binary search

//	system("pause");
return 0;

}

/*******OUTPUT*****************

enter 10 numbers:
1 2 3 4 5 6 7 8 9 10
enter 1 number:
4
the number  4 was found at index:3

*****************************/
```

פתרון שאלה 2

```/*************************
targil: 7 sheela 2
lecturer: meir komer
discription: the program  merged 3 arrays to a new array and allso sorted the new array .
**************************/
#include <iostream>
using namespace std;

int cinArray(int array[])
{
int m = 0, i = 0, size ,firstElement=1;

do                      // the input loop
{
i = 0;	m = 0; size = 0;
cin >> array[i];              //   the input first elemant input
size++;                       // 'size' is the numbers of the elements in the array.
if (array[0]==0)
{
size=0;                      // 'size' is 0 becauze: 0 is the sign for STOP , in this case(array[0]=0) the array is completely empty :( .
firstElement=0;
}

for (i = 1; i < 10&&(firstElement); i++)         // the second condition is to make sure that: if the first elemant is 0, the program not ask mor input.
{
cin >> array[i];
size++;
if (array[i] == 0)    //  if the  elemant is 0, STOP!
{
size -= 1;            //0 is the sign for STOP so : 0 not include in the array
break;
}
if (array[i] >= array[i - 1])    //  if the next elemant is bigger or equal from the privies elemant let me know
{
m = 1;     // let me know if is not sorted (to print ERROR and statr the input loop all over again).
}

}
if (m == 1)
{
cout << "ERROR" << endl;
}

} while (m == 1);

return size;            // we need the array size for the merged loops.
}

void marrageArray(int array1[],int array2[],int array3[] ,int newArray[30] ,int size1  ,int size2,int size3) //the merged function.
{
int i=0 ,j,c, k=0 ,b=0;               // indexs

for ( i = 0; i < size1; i++)      // enter the  first array to the new Array
{
newArray[i] = array1[i];
}

for ( k=0, j = i; k < size2; k++,j++)   // enter the second array to the new Array
{
newArray[j] = array2[k];
}
for ( b = 0, c = j; b < size3; c++, b++)      // enter the third array to the new Array
{
newArray = array3[b];
}

bool flag; int temp;     // bubble sort for the new array that contian the 3 arrays.
flag = true;

while (flag)
{
flag = false;
for (j = 0; j < c-1; j++)
{
if (newArray[j] < newArray[j + 1] )     // if elamant is smaller from the next one replace between them.
{
temp = newArray[j];
newArray[j] = newArray[j + 1];
newArray[ j + 1] = temp;
flag = true;
}
}

}

for (int f = 0; f < c; f++)                  // merged vector output
{
cout << newArray[f] << " ";
}

}

int main()
{
int array1[10], array2[10], array3[10],newArray[30];
int size1, size2, size3;
cout << "enter values for the first vector:" << endl;

size1 = cinArray(array1);                                       // go to the function to get numbers for the first array , and return the first array size
cout << "enter values for the second vector:" << endl;

size2 = cinArray(array2);                                         // go to the function to get numbers for the second array , and return the second array size
cout << "enter values for the third vector:" << endl;

size3 = cinArray(array3);                                       // go to the function to get numbers for the third array , and return the third array size

cout <<"merged vector is:"<<endl;
marrageArray( array1, array2, array3 , newArray , size1  , size2, size3);

//	system("pause");
return 0;
}
/********OUTPUT 1**********
enter values for the first vector:
6 4 2 0
enter values for the second vector:
21 19 17 14 13 12 9 6 3 2
enter values for the third vector:
3 2 1 0
merged vector is:
21 19 17 14 13 12 9 6 6 4 3 3 2 2 2 1
**************************/

/********OUTPUT 2**********
enter values for the first vector:
6 5 4 1 2 3 0
ERROR
3 2 1 0
enter values for the second vector:
9 8 6 4 3 2 1 0
enter values for the third vector:
1 2 0
ERROR
6 5 4 3 2 1 0
merged vector is:
9 8 6 6 5 4 4 3 3 3 2 2 2 1 1 1
**************************/

```

פתרון שאלה 3

```
/*************************
targil: 7 sheela 1
lecturer: meir komer
discription: birany search
**************************/
#include <iostream>
using namespace std;

void sorted (int*vec,int size)
{
int first; // (element in index 1 [one after index 0])
int s;
for (first = 1; first < size; first++)
{
int temp = vec[first];
for (s = first; s > 0 && temp< vec[s-1]; s--)
{
vec[s]=vec[s-1];
}

vec[s]=temp;
}

}
int main()
{
cout <<"enter 100 numbers:"<<endl;
int array[10][10];

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

int RightTriangle[45],w=0;

for (int k = 0,step = 1; k < 9; k++,step++)
{
for (int j = step; j < 10; j++)
{
RightTriangle[w]=array[k][j];

w++;
}
}

sorted(RightTriangle,45);

int q=0;
for (int k = 0,step = 1; k < 9; k++,step++)
{
for (int j = step; j < 10; j++)
{
array[k][j]=RightTriangle[q];
q++;
}
}

int leftTriangle[45],g=0;

for (int w = 1,step=1; w < 10; w++,step++)
{
for (int x = 0; x < step; x++)
{
leftTriangle[g]=array[w][x];
g++;
}
}

sorted(leftTriangle,45);

int n=0;
for (int w = 1,step=1; w < 10; w++,step++)
{
for (int x = 0; x < step; x++)
{
array[w][x]=leftTriangle[n];
n++;
}
}
cout<<endl;
cout <<"sorted matrix:"<<endl;

for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout<<array[i][j]<<" ";
}
cout<<endl;
}

//	system ("pause");
return 0;
}
/*************OUTPUT**************\
enter 100 numbers:
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
10 20 30 40 50 60 70 80 90 100

sorted matrix:
1 11 21 22 31 32 33 41 42 43
2 12 44 51 52 53 54 55 61 62
3 4 23 63 64 65 66 71 72 73
5 6 7 34 74 75 76 77 81 82
8 9 10 13 45 83 84 85 86 87
14 15 16 17 18 56 88 91 92 93
19 20 24 25 26 27 67 94 95 96
28 29 30 35 36 37 38 78 97 98
39 40 46 47 48 49 50 57 89 99
58 59 60 68 69 70 79 80 90 100
*************************************/

```

פתרון שאלה 4

```
/*************************
targil: 7 sheela 4
lecturer: meir komer
discription: gets 5 words into a matrix of 10 by 5 and sorts the words According the ABC.
**************************/
#include <iostream>
using namespace std;

void sortedWords(char wordsArray[])
{

char temp[5];
int i;
for (int r = 0; r < 9; r++)
{
for ( i = 0; i < 45; i+=5)
{
if (wordsArray[i]>wordsArray[i+5])
{
for (int j = i,k=0; k < 5; k++,j++)
{
temp[k]=wordsArray[j+5];

}

for (int k = 0,j=i; k < 5; k++,j++)
{
wordsArray[j+5]=wordsArray[j];
}

for (int k = 0 ,j = i;  k < 5;  k++,j++)
{
wordsArray[j]=temp[k];

}

}

}

}

int b;

for (int a = 0; a < 9; a++)
{
for ( b = 0; b < 45; b+=5)
{
if (wordsArray[b]==wordsArray[b+5])
{
if (wordsArray[b+1]==wordsArray[b+6])
{
if (wordsArray[b+2]==wordsArray[b+7])
{
if (wordsArray[b+3]==wordsArray[b+8])
{
if (wordsArray[b+4]==wordsArray[b+9])
{

}
if (wordsArray[b+4]>wordsArray[b+9])
{
for (int j = b,k=0; k < 5; k++,j++)
{
temp[k]=wordsArray[j+5];

}
for (int k = 0,j=b; k < 5; k++,j++)
{
wordsArray[j+5]=wordsArray[j];
}
for (int k = 0 ,j = b;  k < 5;  k++,j++)
{
wordsArray[j]=temp[k];

}
}
}
if (wordsArray[b+3]>wordsArray[b+8])
{
for (int j = b,k=0; k < 5; k++,j++)
{
temp[k]=wordsArray[j+5];

}
for (int k = 0,j=b; k < 5; k++,j++)
{
wordsArray[j+5]=wordsArray[j];
}
for (int k = 0 ,j = b;  k < 5;  k++,j++)
{
wordsArray[j]=temp[k];

}
}
}
if (wordsArray[b+2]>wordsArray[b+7])
{
for (int j = b,k=0; k < 5; k++,j++)
{
temp[k]=wordsArray[j+5];

}
for (int k = 0,j=b; k < 5; k++,j++)
{
wordsArray[j+5]=wordsArray[j];
}
for (int k = 0 ,j = b;  k < 5;  k++,j++)
{
wordsArray[j]=temp[k];

}
}
}
if (wordsArray[b+1]>wordsArray[b+6])
{
for (int j = b,k=0; k < 5; k++,j++)
{
temp[k]=wordsArray[j+5];

}
for (int k = 0,j=b; k < 5; k++,j++)
{
wordsArray[j+5]=wordsArray[j];
}
for (int k = 0 ,j = b;  k < 5;  k++,j++)
{
wordsArray[j]=temp[k];

}
}
}
}
}
}
int main()
{
cout <<"enter 10 words:"<<endl;
char words[10][5];

for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5; j++)
{
cin>>words[i][j];
}
}
cout <<"after sorting:"<<endl;

char wordsArray[50]; int u=0;

for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5; j++)
{
wordsArray[u]=words[i][j];
u++;
}
}

sortedWords(wordsArray);

for (int i = 0,space=1; i < 50; i++,space++)
{
cout<<wordsArray[i];
if (space%5==0)
{
cout<<" ";

}
}

cout<<endl;
//system ("pause");
return 0;
}
/***********output**********
enter 10 words:
house apple teach array teach books point float apply begin
after sorting:
apple apply array begin books float house point teach teach
**********************************/

```

## 2 Comments on “מבוא למדעי המחשב תרגיל 7”

1. אתה צריך להוריד ולהתקין את סביבת העבודה של ויזואל סטודיו