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

Objective: In this lab you will implement two functions using header files and a

ID: 3888810 • Letter: O

Question



Objective: In this lab you will implement two functions using header files and a main program. Functions: 1) float swapAtindex(int index, List uList); I/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); I/ 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), deleteltem(int index, List* ulist), printList etc.

Explanation / Answer

//File Name: Lab4.h
#ifndef _Lab4_h
#define _Lab4_h
//Structure definition
typedef struct listnode
{
int maxSizeOfList, tail;
float *array;
}List;
//Prototype of functions
//Function to create a list of specified size
void createList(int size, List *uList);
//Function to delete a item at specified index
void deleteItem(int index, List *uList);
//Function to display the items
void printList(List *uList);
//Function to accept data to the list
void acceptData(List *uList);
//Function to return the original item swapped
float swapAtIndex(int index, List *uList);
//Function to return the maximum value in the list and create a new list to store the maximum
float copyMax(List orList, List *newList);
#endif // _Lab4_h

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

//File Name: Lab4.c
#include <stdio.h>
#include <malloc.h>
#include "Lab4.h"

//Function to create a list of specified size
void createList(int size, List *uList)
{
//Assigns the maximum size
uList->maxSizeOfList = size;
//Allocate memory
uList->array = (float *) malloc(sizeof(float) * size);
//Initializes tail to zero
uList->tail = 0;
}//End of function

//Function to accept data to the list
void acceptData(List *uList)
{
//c for counter variable, n for number of items to insert
int c, n;
//Accept the number
printf(" How many numbers you want to insert: ");
scanf("%d", &n);
//Checks whether the number exceeds maximum size allocated to the list
if(n > uList->maxSizeOfList)
{
//Displays error message
printf(" Exceeding maximum size");
//Exit program
exit(0);
}//End of if condition
//Accept data for the list
printf(" Enter %d data: ", n);
//Loops up to n
for(c = 0; c < n; c++)
{
//Accepts data
printf(" Enter %d data: ", (c+1));
scanf("%f", &uList->array[c]);
}//End of for loop
//Tail is updated to n
uList->tail = n;
}//End of function

//Function to delete a item at specified index
void deleteItem(int index, List *uList)
{
//Counter variable
int c;
//Checks whether the index position to delete exceeds the number of elements in the list
if(index > uList->tail)
//Display error message
printf(" Invalid index");
//Other valid index position
else
{
//Loops till number of elements in the list
for(c = index; c < uList->tail; c++)
//Moves the array elements one position left
uList->array[c] = uList->array[c+1];
//Decrease the tail counter
uList->tail--;
}//End of else
}//End of function

//Function to display the items
void printList(List *uList)
{
//Loop counter variable
int c;
//Loops till end of list
for(c = 0; c < uList->tail; c++)
//Display the item
printf("%f | ", uList->array[c]);
printf(" ");
}//End of function

//Function to return the original item swapped
float swapAtIndex(int index, List *uList)
{
//Temporary variable assigned starting position value of list
float temp = uList->array[0];
//Checks whether the index position to delete exceeds the number of elements in the list
if(index > uList->tail)
//Displays error message
printf(" Invalid index");
//Other valid index position
else
{
//Swapping process
uList->array[0] = uList->array[index];
uList->array[index] = temp;
}//End of else
//Return the original value swapped
return temp;
}//End of function

//Function to return the maximum value in the list and create a new list to store the maximum
float copyMax(List orList, List *newList)
{
//Loop counter variable
int c;
//Stores the starting index position value of the list as maximum value
float max = orList.array[0];
//Loops from one position to end of the list, because zero position is already considered as maximum
for(c = 1; c < orList.tail; c++)
{
//Checks the current position whether it is greater than the max
if(orList.array[c] > max)
{
//If current position value is greater than max value then update the max with the current index position value
max = orList.array[c];
}//End of if condition
}//End of for loop
//Assign the value max to new list tail position
newList->array[newList->tail] = max;
//Increase the tail counter for the new list
newList->tail++;
//Return maximum value
return max;
}//End of function

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

//File Name: main.c
#include <stdio.h>
#include "Lab4.h"
#include "Lab4.c"
//Main method definition
int main()
{
//Creates two list
List l, nl;
createList(5, &l);
createList(5, &nl);
//Accept data for list
acceptData(&l);
//Display list after accept data
printList(&l);
//Delete an item from the list at specified index position
deleteItem(1, &l);
//Display list after deleting
printList(&l);
//Displays the original item swapped
printf(" The original item swapped = %f ", swapAtIndex(3, &l));
//Display list after swapping
printList(&l);
//Displays the maximum value in the list and store the maximum value in the new list
printf(" The Maximum value in the list = %f ", copyMax(l, &nl));
//Display new list after copy
printList(&nl);
//Delete an item from the list at specified index position
deleteItem(0, &l);
//Display list after deletion
printList(&l);
//Displays the maximum value in the list and store the maximum value in the new list
printf(" The Maximum value in the list = %f ", copyMax(l, &nl));
//Display new list after copy
printList(&nl);
}//End of main function

Sample Run:


How many numbers you want to insert: 5

Enter 5 data:
Enter 1 data: 10

Enter 2 data: 20

Enter 3 data: 30

Enter 4 data: 40

Enter 5 data: 50
10.000000 | 20.000000 | 30.000000 | 40.000000 | 50.000000 |
10.000000 | 30.000000 | 40.000000 | 50.000000 |

The original item swapped = 10.000000
50.000000 | 30.000000 | 40.000000 | 10.000000 |

The Maximum value in the list = 50.000000
50.000000 |
30.000000 | 40.000000 | 10.000000 |

The Maximum value in the list = 40.000000
50.000000 | 40.000000 |