Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. // Please write your code, within the "BEGIN-END" blocks given below.
  5. // A "BEGIN-END" block is identified as follows :
  6. //
  7. // "//// BEGIN: Some string DONT_ERASE_xx_yy"
  8. //
  9. //
  10. // "//// END: Some other string DONT_ERASE_xx_yy"
  11. //
  12. // where "xx" is the block number and "yy" is the
  13. // marks allocated for the block
  14. //
  15. // The FIRST block (BLOCK 1 i.e. DONT_ERASE_01_0) carries 0 marks and
  16. // is a placeholder for your personal information, to be written as a comment.
  17. //
  18. // WARNING :
  19. // (1) Do NOT write any cout or cin statements
  20. // (2) Do NOT delete or modify the existing code i.e. main function, comments, blocks, etc.
  21. // (3) Write your code in between BEGIN and END of the respective blocks only
  22. // (4) Do NOT rename the .cpp file
  23.  
  24. //// ---------------------------------------------------------------------------
  25. //// BEGIN: Fill your details as comments below DONT_ERASE_01_0
  26. //// Name: Puru Sharma
  27. ////
  28. //// END: Fill your details as comments above DONT_ERASE_01_0
  29. //// ---------------------------------------------------------------------------
  30.  
  31.  
  32. int comparator(int audience[][2],int index1, int index2)
  33. {
  34. ////-----------------------------------------------------------------------
  35. //// BEGIN: write your code to compare the ecodes of index1 and index2 DONT_ERASE_02_02
  36.  
  37. int ecode1=0,ecode2=0;
  38. int a=audience[index1][0],b=audience[index1][1];
  39. if(b!=0)
  40. {
  41. ecode1=a;
  42. }
  43. else
  44. {
  45. ecode1=0;
  46. }
  47. while (b>1)
  48. {
  49. ecode1=ecode1*a;
  50. b--;
  51. }
  52. a=audience[index2][0],b=audience[index2][1];
  53. if(b!=0)
  54. ecode2=a;
  55. else
  56. ecode2=0;
  57. while (b>1)
  58. {
  59. ecode2=ecode2*a;
  60. b--;
  61. }
  62. ecode1=ecode1%100;
  63. ecode2=ecode2%100;
  64. if(ecode1>ecode2)
  65. return -1;
  66. else if(ecode1==ecode2)
  67. return 0;
  68. else if(ecode1<ecode2)
  69. return 1;
  70.  
  71. ////END: end of your code DONT_ERASE_02_02
  72. ////------------------------------------------------------------------------
  73. }
  74.  
  75. void sorting(int audience[][2],int N, int &i_index, int &j_index)
  76. {
  77. ////-----------------------------------------------------------------------
  78. //// BEGIN: write your code to find index i and index j DONT_ERASE_03_04
  79. //// Hint: Use the comparator function
  80. int p=0,q=1,r=0;
  81. while(comparator(audience,p,q)==1)
  82. {
  83. r++;
  84. p++;
  85. q++;
  86. }
  87. i_index=r;
  88. r=N-1;p=N-1;q=N-2;
  89. while(comparator(audience,p,q)==-1)
  90. {
  91. r--;
  92. p--;
  93. q--;
  94. }
  95. j_index=r;
  96.  
  97. int x,y,z[N][2];
  98. x=i_index;y=j_index;
  99. for(x=i_index;x<=y;x++)
  100. {
  101. if(comparator(audience,x,x+1)==-1)
  102. {
  103. z[x]=audience[x];
  104. audience[x]=audience[x+1];
  105. audience[x+1]=z[x];
  106. }
  107. }
  108.  
  109.  
  110. ////END: end of your code DONT_ERASE_03_04
  111. ////------------------------------------------------------------------------
  112. }
  113.  
  114. void merge(int audience[][2], int mergedarray[][2],int N, int i_index , int j_index)
  115. {
  116. ////----------------------------------------------------------------------------
  117. //// BEGIN: write your code to merge DONT_ERASE_04_04
  118. int i=0;
  119. while(i<=i_index)
  120. {
  121. mergedarray[i]=audience[i];
  122. i++;
  123. }
  124. int j=j_index;
  125. while(j<N)
  126. {
  127. mergedarray[i]=audience[j];
  128. i++;
  129. j++;
  130. }
  131.  
  132. int r,t[N][2];
  133. for(r=0;r<i;r++)
  134. {
  135. if(comparator(mergedarray,r,r+1)==-1)
  136. {
  137. t=mergedarray[r];
  138. mergedarray[r]=mergedarray[r+1];
  139. mergedarray[r+1]=t;
  140. }
  141. }
  142. int s=i_index+1;
  143. while(s<j_index)
  144. {
  145. mergedarray[i]=audience[s];
  146. i++;
  147. s++;
  148. }
  149.  
  150. ////END: end of your code DONT_ERASE_04_04
  151. ////---------------------------------------------------------------------------------
  152. }
  153.  
  154. int main()
  155. {
  156. int audience[100][2],mergedmarks[100][2];
  157. int i,N;
  158. int index1=0,index2=0;
  159. int comp_result;
  160. cout<<"Enter the value of N : ";
  161. cin >> N; // Enter size of the table
  162. cout<<"Enter the base and exponent for " << N << "rows " << endl;
  163.  
  164. for(i=0;i<N;i++)
  165. cin >> audience[i][0] >> audience[i][1]; //Enter numbers in the table
  166.  
  167. cout << endl << "Checking Function 1: Compare ecodes for 5 index pairs" << endl;
  168. for(i = 0;i<5;i++)
  169. {
  170. cout <<"\nEnter indices of row1 and row2 that you want to compare: ";
  171. cin >> index1 >> index2;
  172. if (index1 < 0 || index2 <0 || index1 >=N || index2 >=N)
  173. continue;
  174. comp_result = comparator(audience,index1,index2);
  175.  
  176. if(comp_result==-1)
  177. cout << "ecode of index 1 is greater than ecode of index2" <<endl;
  178. else if(comp_result==1)
  179. cout << "ecode of index 1 is less than ecode of index2" <<endl;
  180. else if(comp_result==0)
  181. cout << "ecode of index 1 is equal to ecode of index2" <<endl;
  182. }
  183. cout << endl;
  184. int index_i=0, index_j=N;
  185. sorting(audience, N, index_i, index_j);
  186.  
  187. cout << "Checking Function 2: Printing sorted array "<<endl;
  188. for(i=0; i<N; i++)
  189. cout << audience[i][0] << " "<<audience[i][1]<<endl;
  190. cout << endl;
  191.  
  192. cout << "index i: "<< index_i <<"\nindex j: " << index_j<<endl;
  193.  
  194.  
  195. cout << endl << "Checking Function 3: Printing Merged Array "<<endl;
  196. merge(audience , mergedmarks ,N, index_i, index_j);
  197. int merge_array_size = index_i + (N-(index_j+1));
  198. for(i=0; i<N; i++)
  199. cout << mergedmarks[i][0] << " "<<mergedmarks[i][1]<<endl;
  200. cout << endl;
  201. return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement