24L-0807-Ali-Hassan-PF-assignment-solutions

Q1---


 //#include <iostream>

//using namespace std;

//

//int main()

//{

// // Declaration and initialization

// const int size = 7;

// int arr[size] = { 0 }, eq_ind = -1;

//

// // Input

// cout << "Enter the elements of the array (" << size << ") :\n\n";

// for (int i = 0; i < size; i++)

// {

// cout << "Element " << i + 1 << ": ";

// cin >> arr[i];

// }

//

// for (int i = 0; i < size; i++) // Traverses the array

// {

// int sum1 = 0, sum2 = 0;

// for (int j = 0; j < size; j++) // Calculates the sum of the elements before and after current index

// {

// if (j < i)

// sum1 += arr[j];

// else if (j > i)

// sum2 += arr[j];

// }

// if (sum1 == sum2) // Checks if theese sums are equal, if yes then it is equilibrium index

// {

// eq_ind = i;

// break;

// }

// }

//

// cout << "\nEquilibrium index: " << eq_ind << "\n\n";

//

// system("pause");

// return 0;

//}

----------------------------------------------------------------------------------------------


Q2


//#include <iostream>

//using namespace std;

//

//int main()

//{

// // Declarations

// int a1[15] = { 0 }, a2[15] = { 0 }, a3[15] = { 0 }, r[60] = { 0 };

// int s1, s2, s3;

//

// // Inputs the sizes of the arrays

// cout << "Size Inputs:\n";

// do

// {

// cout << "Enter the size of array 1 (size must not exceed 20): ";

// cin >> s1;

// } while (s1 <= 0 || s1 > 20);

//

// do

// {

// cout << "Enter the size of array 2 (size must not exceed 20): ";

// cin >> s2;

// } while (s2 <= 0 || s2 > 20);

//

// do

// {

// cout << "Enter the size of array 3 (size must not exceed 20): ";

// cin >> s3;

// } while (s3 <= 0 || s3 > 20);

//

// int s4 = s1 + s2 + s3;

//

// // Inputs the elements of the arrays while ensuring sorted elements

// cout << "\nElements Input:\nEnter the sorted elements (" << s1 << ") of array 1\nEnter element 1: ";

// cin >> a1[0];

// for (int i = 1; i < s1; i++)

// {

// do

// {

// cout << "Enter element " << i + 1 << ": ";

// cin >> a1[i];

// } while (a1[i] <= a1[i - 1]);

// }

//

// cout << "\nEnter the sorted elements (" << s2 << ") of array 2\nEnter element 1: ";

// cin >> a2[0];

// for (int i = 1; i < s2; i++)

// {

// do

// {

// cout << "Enter element " << i + 1 << ": ";

// cin >> a2[i];

// } while (a2[i] <= a2[i - 1]);

// }

//

// cout << "\nEnter the sorted elements (" << s3 << ") of array 3\nEnter element 1: ";

// cin >> a3[0];

// for (int i = 1; i < s3; i++)

// {

// do

// {

// cout << "Enter element " << i + 1 << ": ";

// cin >> a3[i];

// } while (a3[i] <= a3[i - 1]);

// }

//

// int i = 0, j = 0, k = 0, l = 0; // i, j, l and k are indices trackers of array 1, 2, 3 and 4 respectively

//

// // One of the arrays get exhausted as its all elements are copied in sorted order

// while (i < s1 && j < s2 && l < s3) 

// {

// if (a1[i] <= a2[j])

// {

// if (a1[i] <= a3[l])

// r[k++] = a1[i++];

// else

// r[k++] = a3[l++];

// }

// else

// {

// if (a2[j] <= a3[l])

// r[k++] = a2[j++];

// else

// r[k++] = a3[l++];

// }

// }

//

// // We check all the remaining possibilites 

// while (i < s1 && j < s2)

// {

// if (a1[i] <= a2[j])

// r[k++] = a1[i++];

// else

// r[k++] = a2[j++];

// }

// while (j < s2 && l < s3)

// {

// if (a2[j] <= a3[l])

// r[k++] = a2[j++];

// else

// r[k++] = a3[l++];

// }

// while (i < s1 && l < s3)

// {

// if (a1[i] <= a3[l])

// r[k++] = a1[i++];

// else

// r[k++] = a3[l++];

// }

//

// // All the remaining elements are just copied as it is, however only one loop runs

// while (i < s1)

// r[k++] = a1[i++];

//

// while (j < s2)

// r[k++] = a2[j++];

//

// while (l < s3)

// r[k++] = a3[l++];

//

// cout << "\nResultant Sorted Array: { ";

// for (int i = 0; i < s4; i++)

// cout << r[i] << ", ";

// cout << "}\n\n";

//

// system("pause");

// return 0;

//}


---------------------------------------------------------------------------------------


Q3


//#include <iostream>

//using namespace std;

//

//int main()

//{

// int size = 18;

// do

// {

// cout << "Enter the size of the array (size must not exceed 20): ";

// cin >> size;

// } while (size <= 0 || size > 20);

// 

// int arr[20] = { 0 }, targetVal = 0;

//

// cout << "\nEnter the " << size << " elements of the array (+ve):\n";

// for (int i = 0; i < size; i++)

// {

// do

// {

// cout << "Enter element " << i + 1 << ": ";

// cin >> arr[i];

// } while (arr[i] <= 0);

// } cout << endl;

//

// do

// {

// cout << "Enter a value whose longest index you want to delete: ";

// cin >> targetVal;

// } while (targetVal <= 0);

//

// cout << "\nOriginal Array: { ";

// for (int j = 0; j < size; j++)

// cout << arr[j] << ", ";

// cout << "}\n\n";

//

// /* if the value matches then running count is updated while the actual count is only updated

// when a mismatch happens and the running count is greater than actual count ensuring that only

// largest occurances are considered and it counts the number of that sequence */

// int count = 0, run_count = 0, end = 0, i;

// for (i = 0; i < size; i++)

// {

// if (arr[i] == targetVal)

// run_count++;

// else

// {

// if (run_count > count)

// {

// count = run_count;

// run_count = 0;

// end = i;

// }

// }

// }

// if (run_count > count)

// {

// count = run_count;

// end = i; 

// }

//

// // Iterates from the start of the longest sequence to the end and replaces them with -1

// for (int j = end - count; j < end; j++)

// arr[j] = -1;

//

// cout << "After deletion: { ";

// for (int j = 0; j < size; j++)

// cout << arr[j] << ", ";

// cout << "}\n\n";

//

// system("pause");

// return 0;

//}


----------------------------------------------------------------------------------------

Q4


//#include <iostream>

//using namespace std;

//

//int main()

//{

// int size;

// do

// {

// cout << "Enter the size of the array (size must not exceed 20): ";

// cin >> size;

// } while (size <= 0 || size > 20);

//

// int arr[20] = { 0 };

//

// cout << "\nEnter the elements of the array (" << size << ") :\n";

// for (int i = 0; i < size; i++)

// {

// cout << "Element " << i + 1 << ": ";

// cin >> arr[i];

// }

//

// int sum = arr[0], st = 0, et = 0, s = 0, e = 0;

// for (int i = 0; i < size; i++)

// {

// for (int j = 0; j < size - i; j++)

// {

// int temp = 0;

// // Iterates over sub arrays and caluclates their sum and starting and ending idices as well

// for (int k = 0; k <= i; k++)

// {

// if (k == 0)

// st = j + k;

// else if (k == i)

// et = j + k;

// temp += arr[j + k];

// }

// if (temp > sum) // Stores the largest subarray sum and its starting and ending index

// {

// s = st;

// e = et;

// sum = temp;

// }

// }

// }

//

// cout << "\nMax subarray: { ";

// for (int i = s; i <= e; i++)

// cout << arr[i] << ", ";

//

// cout << "}\n\nMax subarray sum: " << sum << "\n\n";

// system("pause");

// return 0;

//}

---------------------------------------------------------------------------------------------------


Q5



//#include <iostream>

//using namespace std;

//

//int main()

//{

// int size;

// do

// {

// cout << "Enter the size of the array (size must not exceed 20): ";

// cin >> size;

// } while (size <= 0 || size > 20);

//

// int price[20] = { 0 };

//

// cout << "\nEnter the values (" << size << ") of the stock on each day (+ve):\n";

// for (int i = 0; i < size; i++)

// {

// do

// {

// cout << "Enter price " << i + 1 << ": ";

// cin >> price[i];

// } while (price[i] <= 0);

// } cout << endl;

//

// cout << "Prices: { ";

// for (int i = 0; i < size; i++)

// cout << price[i] << ", ";

// cout << "}\n\n";

//

// // Finds the index of the smallest element

// int cheap = 0;

// for (int i = 1; i < size; i++)

// {

// if (price[i] == 1)

// {

// cheap = i;

// break;

// }

// else if (price[i] < price[cheap])

// cheap = i;

// }

//

// // Finds the index of the largest element

// int exp = cheap;

// for (int i = exp + 1; i < size; i++)

// {

// if (price[i] > price[exp])

// exp = i;

// }

//

// cout << "Maximum Profit: " << price[exp] - price[cheap] << "\n\n";

//

// system("pause");

// return 0;

//}

-------------------------------------------------------------------------------------

Q6


//#include <iostream>

//using namespace std;

//

//int main()

//{

// int size;

// do

// {

// cout << "Enter the size of the array (size must not exceed 20): ";

// cin >> size;

// } while (size <= 0 || size > 20);

//

// int arr[20] = { 0 };

//

// cout << "\nEnter the elements of the array (" << size << ") :\n";

// for (int i = 0; i < size; i++)

// {

// cout << "Element " << i + 1 << ": ";

// cin >> arr[i];

// }

//

// // Checks if the element at certain index is greater than its neighbours and prints them

// cout << "\nPeak indices: ";

// for (int i = 0; i < size; i++)

// {

// if ((i == 1 && arr[i] > arr[i + 1]) || (i == size - 1 && arr[i] > arr[i - 1]) || (arr[i] > arr[i + 1] && arr[i] > arr[i - 1]))

// cout << i << ", ";

// } cout << "\n\n";

//

// system("pause");

// return 0;

//}


---------------------------------------------------------------------------------------

Q7


//#include <iostream>

//using namespace std;

//

//int main()

//{

// int size;

// do

// {

// cout << "Enter the size of the array (size must not exceed 20): ";

// cin >> size;

// } while (size <= 0 || size > 20);

//

// int arr[20] = { 0 }, res[20] = { 0 };

// cout << "\nEnter the elements of the array (" << size << ") :\n";

// for (int i = 0; i < size; i++)

// {

// cout << "Element " << i + 1 << ": ";

// cin >> arr[i];

// }

//

// int f_size;

// do

// {

// cout << "\nEnter the size of the filter (size must be odd and <= half of array size): ";

// cin >> f_size;

// } while (f_size <= 0 || f_size % 2 == 0 || f_size > size / 2);

//

// int fil[10] = { 0 };

// cout << "\nEnter the elements of the filter (" << f_size << ") :\n";

// for (int i = 0; i < f_size; i++)

// {

// cout << "Element " << i + 1 << ": ";

// cin >> fil[i];

// }

//

// int sum, term = (f_size - 1) / 2; // This term is the life saver 

// for (int i = 0; i < size; i++)

// {

// sum = 0;

// int k = i - term;

// for (int j = 0; j < f_size; j++)

// {

// if (k >= 0 && k < size) // Ensures that only valid values are considered

// sum += fil[j] * arr[k];

// k++;

// }

// res[i] = sum;

// }

//

// cout << "\nResultant Array: { ";

// for (int i = 0; i < size; i++)

// cout << res[i] << ", ";

// cout << "}\n\n";

//

// system("pause");

// return 0;

//}

------------------------------------------------------------------------------------------------------------------

bonus question


//#include <iostream>

//using namespace std;

//

//int main()

//{

// int size;

// do

// {

// cout << "Enter the size of the array (size must not exceed 20): ";

// cin >> size;

// } while (size <= 0 || size > 20);

//

// int arr[20] = { 0 };

// cout << "\nEnter the elements of the array (" << size << ") (+ve):\n";

// for (int i = 0; i < size; i++)

// {

// do

// {

// cout << "Element " << i + 1 << ": ";

// cin >> arr[i];

// } while (arr[i] <= 0);

// }

//

// int jumps = 1, max;

// for (int i = 0; i < size; )

// {

// max = i + 1; int j;

// for (j = 1; j <= arr[i]; j++) // We jump from the starting index to that having max element

//

// if (i + j == size - 1) // The loop terminates when it reaches the last index

// break;

// else if (i + j < size - 1) 

// {

// if (arr[i + j] >= arr[max]) 

// max = i + j;

// }

// }

// if (i + j == size - 1)

// break;

// jumps++;

// i = max;

// }

//

// cout << "\nMinimum jumps: " << jumps << "\n\n";

//

// system("pause");

// return 0;

//}

--------------------------------------------------------------------------------------------------------------------

0 Comments