Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C Programming: In this lab you will implement two functions using header file

ID: 3884711 • Letter: I

Question

In C Programming:

In this lab you will implement two functions using header files and a main program.

Functions:

1) float swapAtIndex(int index, List uList); //This function swaps the item at location index with the item at the beginning of the list. The original item at index is returned.
2) float copyMax(List oriList, List *newList); // This function searches for the maximum value in oriList array and copies the value into newList array. The maximum value is returned

You should use the same typedef struct from last lab, which is:
typedef struct listnode{
int maxSizeOfList, tail;
float *array;
}List
And you can continue to use functions implemented from last lab, such as createList(int size, List* ulist), deleteItem(int index, List* ulist), printList etc.

Implementing details:
1. Create two header files (Lab4.h and Lab4.c) and a main program, declare your prototypes in Lab4.h file and implement the functions in Lab4.c file.
2. In your main program, initialize a list and set the array in list to [1.11, 2.22, 3.33, 2.22, 1.11].
3. Swap item at index 3 with the first item. Now your array in list should look like this: [2.22, 2.22, 3.33, 1.11, 1.11].
4. Swap item at index 10. Watch out for the array boundary.
5. Then after step 4, create a new List, search for the maximum value in original list, and copy it to this new list. Your new list array should look like this [3.33].

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>

typedef struct listnode {
    int maxSizeOfList, tail;
    float *array;
}List;

float swapAtIndex(int index, List uList){

     float temp;
     temp = uList.array[index];
     uList.array[index] = uList.array[0];
     uList.array[0] = temp;
     return temp;
}


float copyMax(List oriList, List *uList){

     float max = 0;
     int i;

     for(i = 0; i< oriList.tail; i++){
        if (oriList.array[i] > max)
           max = oriList.array[i];
     }
     uList->array[0] = max;
     return max;
}


int main(){

   List l1,*l2;
   int i;

   l1.maxSizeOfList = 10;
   l1.array = (float *)malloc(sizeof(float) * 10);
   *(l1.array) = 1.11;
   *(l1.array+1) = 2.22;
   *(l1.array+2) = 3.33;
   *(l1.array+3) = 2.22;
   *(l1.array+4) = 1.11;
    l1.tail = 5;
   swapAtIndex(3,l1);
  
  
   for (i = 0; i<l1.tail; i++){
       printf("%0.2f ",l1.array[i]);
   }
   printf(" ");
   l2 = (List *)malloc(sizeof(List));
   l2->array = (float *)malloc(sizeof(float));
   copyMax(l1,l2);
   printf("%0.2f ", l2->array[0]);

   return 0;
}