You are on page 1of 14

Question 1 A.

void main() { int j = 17; int a[4] = { 1 , 2, 3 , 4 }; int k = 18; printArray(a,4); a[3] = j; printArray(a,4); a[-1] = 98; a[4] = 101; printArray(a,4); cout << "j = " << j << "\n"; cout << "k = " << k << "\n"; printArray(a,5); printArray(a-1,6); }

( . "a[4] = 101;"± "a[-1] = 98" ±

. ). .

, . ±

. . .

/*
{ 1 , 2 , 3 , 4 ,} { 1 , 2 , 3 , 17 ,} { 1 , 2 , 3 , 17 ,} j = 17 k = 18 { 1 , 2 , 3 , 17 , 101 ,} { 98 , 1 , 2 , 3 , 17 , 101 ,} Press any key to continue . . . */

i++) // loop to fill the set. { cout<<"Enter number? \n ". i. { if(arr[i]==minimum) cout<<i<<" . { if (arr[i]<minimum) minimum=arr[i].i++) // printing the places in the set of minimum value. () arr[NUMBER]. i<NUMBER . int const void main { int int NUMBER=20. cin>>arr[i] . minimum.Question 2 #include<iostream> using namespace std. for (i=0 . } /**********OUTPUT*************** Enter number? 1 Enter number? 2 Enter number? 3 Enter number? 4 Enter number? 5 Enter number? 6 Enter number? 7 Enter number? 8 Enter number? 8 Enter number? 9 Enter number? 9 Enter number? 8 . } minimum=arr[0]. i++) // loop to find the minimum number. " . for(i=0. i<NUMBER . for (i=0. i<NUMBER. } cout<<endl<<"The places in the set where the minimum number "<<minimum<<" appear are : " . } cout<<endl.

. ************************/ . .Enter 7 Enter 6 Enter 5 Enter 4 Enter 3 Enter 1 Enter 0 Enter 6 number? number? number? number? number? number? number? number? The places in the set where the minimum number 0 appear are : 18 . Press any key to continue .

) \n" . { cout<<"Enter number? \n ". adowncounter=1.Question 3 #include<iostream> using namespace std. // stop the loop if the input is -1. { if (arr[i]>=arr[i+1]) { adowncounter++. i<NUMBER . } } cout<<"Starting from index "<<AindexOfMin<<" the biggest serial which goes down (contains of "<<temp<<" values. if (arr[i]==-1) { size=i . } /*************OUTPUT************** Enter number? 5 Enter number? 3 Enter number? 1 Enter number? 4 Enter number? 6 Enter number? 4 Enter number? 3 . } } for(i=0. } } else { adowncounter=0. int i. // calculate the members in down serial of input numbers. int const NUMBER=50. i++) // loop to fill the set. temp=0. AindexOfMin. i<size . AindexOfMin= i-temp+1 . and check out what is the index before the serial. void main() { int arr[NUMBER]. if (temp<adowncounter) { temp=adowncounter. i++) // loop to compare the numbers . cin>>arr[i] . size. found out what lenth of the biggest serial which goes down . for(i=0. break.

) Press any key to continue .Enter number? 2 Enter number? -1 Starting from index 4 the biggest serial which goes down (contains of 4 values. **************/ . . .

in addition will sort the numbers by odd numbers after th even numbers.4}. temp . function will copy all the Odd numbers from A-array to B-array. int size) { int i. res=NumberOfPositiveValues(arr. i<size . // C.2.int size) // A { int i.0. i++) { if (arr[i]>0) { index++.1. i2.size ) . }//if }//for return index. will return the number of even numbers in A. size). // example int B[8]={0}.3. int size) // B { int res. //D. for (i=0. i++) { if (A[i]%2!=0) { B[i]=A[i]. } } OutputNumOfPosValues(B . // B. int size). // calling for the previouse function } void removeOdd (int A[].array. i<sizeA. index=0. void copyOdd (int A[]. void main() { int A[8]={0.Question 4 #include<iostream> using namespace std. int B[]. int size). E. int size=8. } void copyOdd (int A[]. int NumberOfPositiveValues (int arr[]. int B[].2. for (i=0. for(i=0. function receive array. int sizeA. int sizeA. // returns the count of numbers bigger than 0 void OutputNumOfPosValues (int arr[]. } void OutputNumOfPosValues (int arr[]. int& sizeB) // C { int i. // A .i++) { .8). removeOdd(A . cout<<res<<endl . int size). int& sizeB) . } int NumberOfPositiveValues (int arr[]. void removeOdd (int A[]. i<size.1.

0 . i2++) { if (A[i2]%2==0) { temp=A[i2]. " . 2 . 3 . for(i=0. new size is: 5 Press any key to continue . break. 1 . } } cout<<"new size is: " <<size<<endl . } /* 0 . */ . i<size. 1 . i<size. A[i]=temp. i2<size. i++) { if (A[i]%2!=0) { size=i.i++) { cout<<A[i]<<" . . . 2 . } } } } for(i=0. } cout<<endl. A[i2]=A[i]. break.if (A[i]%2!=0) { for (i2=i+1. 4 .

} case '}' : { i++. //prototype declaration bool goodBracket(char []). break. break. } case '\n' : . } case ']' : { i++. break. break. int i=0. else cout << "Bracket value is bad\n".Question 6 #include <iostream > using namespace std. bool enterKey=false. if (getData(brackets)) if (goodBracket(brackets)) cout << "Bracket value is good\n". } case ')' : { i++. } case '[' : { i++. while ((good) && (i<=SIZE-1) && !enterKey) { brackets[i]=cin. //Global constants const int SIZE= 60. break. const int DEPTH=30. } bool getData(char brackets[]) { bool good=true. switch (brackets[i]) { case '(' : { i++. break. } case '{' : { i++. else cout << "Invalid input!\n". bool getData(char[]). terminated by enter:\n".get(). cout << "Please enter a series of brackets. void main() { char brackets[SIZE].

break. break. lastBracketIndex++. lastBracketIndex++. char lastBracket=' '. } case '(' : { openArr[lastBracketIndex]=brackets[i]. i++. } bool goodBracket(char brackets[]) { char openArr[DEPTH]. break.numOfClosed=0. break. i++. i++. } default: { good=false. numOfOpen++. int i=0. } case '[' : { openArr[lastBracketIndex]=brackets[i]. return false. numOfOpen++. break. numOfOpen++. int numOfOpen=0. lastBracketIndex++. int lastBracketIndex=0. bool good=true. } case ')' : . while ((i<=SIZE-1) && (good)) { switch (brackets[i]) { case '{' : { openArr[lastBracketIndex]=brackets[i].{ enterKey=true. } } } return good.

. numOfClosed++. terminated by enter: (()) Bracket value is good Press any key to continue . /*******************OUTPUTS************** 1)Please enter a series of brackets.{ if (openArr[lastBracketIndex-1]=='(') { lastBracketIndex--. break. numOfClosed++. } default: { } } } if (numOfOpen==numOfClosed) return good. } i=SIZE. break. } else good=false. i++. i++. break. . } case '}' : { if (openArr[lastBracketIndex-1]=='{') { lastBracketIndex--. numOfClosed++. } case ']' : { if (openArr[lastBracketIndex-1]=='[') { lastBracketIndex--. break. else return false. . } else good=false. i++. } else good=false.

. . ********************************************/ . terminated by enter: ([])(){} Bracket value is good Press any key to continue . 6)Please enter a series of brackets. terminated by enter: ({)} Bracket value is bad Press any key to continue . . terminated by enter: ([]{} Bracket value is bad Press any key to continue . . 5)Please enter a series of brackets. terminated by enter: ([{()[]}]){()} Bracket value is good Press any key to continue . 7)Please enter a series of brackets. . . . terminated by enter: ([()]) Bracket value is good Press any key to continue . 3)Please enter a series of brackets. 4)Please enter a series of brackets. . . . .2)Please enter a series of brackets. . terminated by enter: ({[])} Bracket value is bad Press any key to continue .

int). printGrades(grades.int). i++) { while (!goodInput) { cout << "Please enter a grade between 0 to 100:". grades[j]=grades[j+1]. //prototype declarations void getGrades(int[].SIZE). } goodInput=false. i<=length-1.j>=0. sortGrades(grades.Question 8 #include <iostream> using namespace std. } void getGrades(int grades[].j--) { if(grades[j]<grades[j+1]) { temp=grades[j]. else cout<< "Invalid input\n". cout << "The sorted array:\n". } } void sortGrades(int grades[]. grades[j+1]=temp. getGrades(grades. void printGrades(int[]. int length) { int temp.int length) { bool goodInput=false. for (int i=0.int).SIZE). for(int i=0. SIZE). void main() { int grades[SIZE]. void sortGrades(int[]. //Global constants const int SIZE=20.i++) { for(int j=i-1. cin >> grades[i]. } } .i<length. if ((grades[i]>0 ) && (grades[i]<101)) goodInput=true.

" << grades[i] << '\n'. 78 10. 99 4. 56 20. 90 6. .} } void printGrades(int grades[]. ************/ . i<=length-1. 89 7. 67 14. 99 3. 77 12. 89 8. 67 15. i++) cout << i+1 <<". 78 11. 98 5. 72 13. } /* Complexity remark: Since the sorting function require a total n-1 iteration per object for an array the size of n I conclude that the complexity of this program is O(n^2) */ /**********output Please enter a grade between 0 to 100:33 Please enter a grade between 0 to 100:227 Invalid input Please enter a grade between 0 to 100:89 Please enter a grade between 0 to 100:77 Please enter a grade between 0 to 100:56 Please enter a grade between 0 to 100:89 Please enter a grade between 0 to 100:99 Please enter a grade between 0 to 100:100 Please enter a grade between 0 to 100:78 Please enter a grade between 0 to 100:67 Please enter a grade between 0 to 100:56 Please enter a grade between 0 to 100:58 Please enter a grade between 0 to 100:99 Please enter a grade between 0 to 100:98 Please enter a grade between 0 to 100:72 Please enter a grade between 0 to 100:65 Please enter a grade between 0 to 100:66 Please enter a grade between 0 to 100:78 Please enter a grade between 0 to 100:90 Please enter a grade between 0 to 100:78 Please enter a grade between 0 to 100:67 The sorted array: The grades are: 1. 100 2. 33 Press any key to continue . 66 16. 58 18. for (int i=0. 78 9. int length) { cout << "The grades are:\n" . . 56 19. 65 17.

. void main() { int arr[SIZE]={2.SIZE). int n) { int i=0. while (!found && (i<=n-1)) { if (arr[i] % 2 == 0) if (arr[i+1] % 2 != 0) { found = true. //prototype declaration: int FindChange(int arr[]. return i. } int FindChange(int arr[]. int changeIndex=FindChange(arr. } /****************output******** 2 Press any key to continue .7. cout << changeIndex <<"\n".8. **************/ . } else i++.5.4. bool found=false. } if (!found) return -1.3}. . const int SIZE=10.Question 9 #include <iostream> using namespace std.4. int n).