the list of arrays are Project Requirements Overview This is

the list of arrays are

Project Requirements (Overview) This is a high level outline of what needs to be accomplished in this assignment l) Initialize an int array of size 150 and load it with the data from eLearning (HW 3 Array Input). For purposes of discussion, call this array the \"Data Array 2) Create an array of int pointers of the same size. For purposes of discussion, call this array the \"Pointer Array.\" Initialize it to point to the Data Array in such a way that, after being initialized, each element of the Pointer Array should point to the element in Data Array that has the same index. This is illustrated in the first picture above.) 3) Sort the Pointer Array by using a modified version of the Bubble Sort algorithm provided. After sorting, pointerArrl0] should point to the smallest element in Data Array pointerArrlijshould point to the second smallest element, and so forth 4) Your program should print out the data set three different times. First, print out the data set in its original order (by traversing the Data Array). Second, print it in sorted order (by S1325-Introduction to Programming page: 5 traversing the Pointer Array). And finally, print the Data Array one more time to demonstrate the original order Functional Requirements You will need to write the following functions besides main 1) A swap o function When called on two pointer arguments, this function should swap the values of the pointer arguments. 2) A sorting function. This function should implement the Bubble S (or some other common sorting algorithm) on the Pointer Array. Note that we are not sorting the Data Array in this problem, only the Pointer Array. Therefore, the pseudocode for the Bubble Sort given above will have to be modified to work on pointers. Furthermore, the sorting will be done according to the values the pointers are pointing to Referring back to Bubble Sort, if we are comparing the values pointed to in one adjacent pointer element to another, and the value pointed to is smaller in the right hand element than the left, then we swap the pointers, not the values in the Data Array

Solution

#include<stdio.h>

int main()
{
   //initialize array with 150 elements
  
   int data_array[150] = { 71, 1899, 272, 1694, 1697, 296, 722, 12, 2726, 1899,
       1374, 1541, 1923, 1904, 1083, 1462, 2981, 1929, 304, 2550,
       1059, 1860, 1963, 516, 647, 1607, 590, 157, 2351, 753,
       2455, 349, 79, 1634, 368, 1992, 2401, 357, 1478, 1601,
       239, 365, 2453, 2283, 2432, 1223, 2739, 2487, 2714, 1391,
       1972, 2805, 1504, 413, 1647, 2750, 44, 64, 934, 1008,
       1429, 1427, 315, 2499, 1620, 1816, 2441, 2557, 2188, 531,
       1514, 2825, 449, 265, 2064, 1022, 34, 1864, 1861, 1516,
       1465, 2327, 398, 2769, 563, 194, 429, 942, 1795, 223,
       2406, 780, 780, 61, 133, 195, 495, 1774, 1934, 2171,
       433, 1417, 292, 324, 2929, 1597, 1470, 764, 593, 891,
       679, 47, 1778, 2532, 1862, 2636, 549, 2923, 2270, 1101,
       1607, 2395, 726, 1111, 892, 1988, 555, 379, 224, 298,
       1660, 2203, 2385, 2159, 2574, 705, 2513, 1755, 313, 173,
       148, 2449, 259, 1006, 1221, 2259, 2020, 1484, 2717, 2400 };
   int *ptr[150];
   // function declarations
   void swap(int *, int *);
   void buble_sort(int *[], int size);
   void print_data_array(int arr[],int size);
   void print_pointer_array(int *arr[],int size);
   int i;
   //initialize array of pointers with data_array
   for (i = 0; i < 150; i++)
   {
       ptr[i] = &data_array[i];
   }
   //display data array
   print_data_array(data_array, 150);
   //call sort on array of pointers
   buble_sort(ptr, 150);
   //display array of pointers
   print_pointer_array(ptr, 150);
}

void swap(int *a, int *b)
{
   int tmp = *a;
   *a = *b;
   *b = tmp;
}
void buble_sort(int *data_array[], int size)
{
   int i, j;
   for (i = 0; i < size; i++)
   {
       for (j = 0; j < (size - i - 1); j++)
       {
           if (*data_array[j] > *data_array[j + 1])
           {
               swap(data_array[j], data_array[j + 1]);
           }
       }
   }
}

void print_data_array(int arr[],int size)
{
   int i;
   printf(\"\ Data Array: \");
   for (i = 0; i < size; i++)
   {
       if (!(i % 11 == 0))
           printf(\"%d \", arr[i]);
       else
           printf(\"\ \");
   }
}
void print_pointer_array(int *arr[],int size)
{
   int i;
   printf(\"\ Pointer Array: \");
   for (i = 0; i < size; i++)
   {
       if (!(i % 11 == 0))
           printf(\"%d \", *arr[i]);
       else
           printf(\"\ \");
   }
}

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

//output for data array with 150 elements

Data Array:
1899 272 1694 1697 296 722 12 2726 1899 1374
1923 1904 1083 1462 2981 1929 304 2550 1059 1860
516 647 1607 590 157 2351 753 2455 349 79
368 1992 2401 357 1478 1601 239 365 2453 2283
1223 2739 2487 2714 1391 1972 2805 1504 413 1647
44 64 934 1008 1429 1427 315 2499 1620 1816
2557 2188 531 1514 2825 449 265 2064 1022 34
1861 1516 1465 2327 398 2769 563 194 429 942
223 2406 780 780 61 133 195 495 1774 1934
433 1417 292 324 2929 1597 1470 764 593 891
47 1778 2532 1862 2636 549 2923 2270 1101 1607
726 1111 892 1988 555 379 224 298 1660 2203
2159 2574 705 2513 1755 313 173 148 2449 259
1221 2259 2020 1484 2717 2400
Pointer Array:
34 44 47 61 64 71 79 133 148 157
194 195 223 224 239 259 265 272 292 296
304 313 315 324 349 357 365 368 379 398
429 433 449 495 516 531 549 555 563 590
647 679 705 722 726 753 764 780 780 891
934 942 1006 1008 1022 1059 1083 1101 1111 1221
1374 1391 1417 1427 1429 1462 1465 1470 1478 1484
1514 1516 1541 1597 1601 1607 1607 1620 1634 1647
1694 1697 1755 1774 1778 1795 1816 1860 1861 1862
1899 1899 1904 1923 1929 1934 1963 1972 1988 1992
2064 2159 2171 2188 2203 2259 2270 2283 2327 2351
2395 2400 2401 2406 2432 2441 2449 2453 2455 2487
2513 2532 2550 2557 2574 2636 2714 2717 2726 2739
2769 2805 2825 2923 2929 2981

the list of arrays are Project Requirements (Overview) This is a high level outline of what needs to be accomplished in this assignment l) Initialize an int arr
the list of arrays are Project Requirements (Overview) This is a high level outline of what needs to be accomplished in this assignment l) Initialize an int arr
the list of arrays are Project Requirements (Overview) This is a high level outline of what needs to be accomplished in this assignment l) Initialize an int arr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site