Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Open Notes, Pair Exercise:
- The following program is intended to continuously get
- an input integer from the user until the sentinel value -999.
- Other features intended to manipulate the data (like
- determining the largest and the smallest values, reversing the
- contents, and rotating the contents of the dynamic list) are also
- included in the features of this program.
- Your task is to complete the program given the skeleton and
- the comments.
- Indicate your section: S17
- Indicate below your names:
- Zach MARASIGAN
- Kyle SANTIAGO
- */
- /* include the necessary libraries */
- #include <stdio.h>
- #include <stdlib.h>
- /* This function will copy nElem number of entries starting from the
- given starting address stored in pSource to an destination dynamic
- list with the starting address stored in pDest.
- Pre-condition:
- 1.) pSource is already pointing to a dynamic list containing
- nElem integer entries
- 2.) there is enough space allocated to store nElem
- entries at the address referred to by pDest
- @param pSource starting address of the source dynamic list
- @param pDest starting address of the destination dynamic list
- @param nElem number of elements to copy from addresses pointed to
- by pSource to pDest
- */
- void
- copy (int *pSource, int *pDest, int nElem)
- { /* complete the requirements for this function */
- int i;
- for (i=0;i<nElem;i++)
- *(pDest+i)=*(pSource+i);
- }
- /* This function will get input from the user as long as the
- input is not the sentinel value -999. Note that a larger
- capacity should be allocated for the list. You are required to
- meaningfully call and use the function copy() as part of your
- solution this function. Do not use realloc() for your
- solution. This function will also update the number of
- elements pointed to by pElem.
- Pre-condition: *pList contains a valid address (i.e., NULL if
- not yet allocated any space and memory
- address of location allocated and stored
- with *pElem number of elements)
- @param pList address where the pointer variable is stored
- @param pElem address where the number of elements have to be updated in
- */
- void
- getInputsV1(int **pList, int *pElem)
- {
- int *pTemp;
- int nInput;
- do {
- printf("Enter number: ");
- scanf("%d", &nInput);
- if (nInput != -999) {
- /* write your statement to allocate 1 space more than
- the current number of elements to be pointed to by
- pTemp */
- pTemp=malloc(sizeof(int)*((*pElem)+1));
- if (pTemp != NULL) {
- if (*pList != NULL) {
- /* call function copy() to copy all previous contents
- of *pList to pTemp */
- copy (*pList,pTemp,*pElem);
- }
- /* add / include the newest input nInput at the end
- of the list pointed to by pTemp */
- *(pTemp+*pElem)=nInput;
- /* increment the number of elements */
- (*pElem)++;
- /* deallocate the old space pointed to by *pList
- if *pList is previously pointing to a list*/
- if (*pList != NULL)
- free(*pList);
- /* have *pList point to the same list as pTemp */
- *pList=pTemp;
- }
- }
- } while (nInput != -999);
- }
- /* This function displays the contents of pList,
- given nElem number of elements. */
- void
- displayList (int *pList, int nElem)
- { /* complete the requirements of this function */
- int i;
- for (i=0;i<nElem;i++)
- printf("Value of %d is %d \n", i, *(pList+i));
- }
- /* This function copies the contents of the list
- pointed to by pSource to pDest. */
- void
- reverseListV1 (int *pSource, int **pDest, int nElem)
- { /* complete the requirements of this function */
- *pDest = malloc(sizeof(int)*nElem);
- int i=0, temp=nElem-1;
- while(i<nElem){
- *(*pDest+i)=*(pSource+temp);
- temp--;
- i++;
- }
- }
- /* This function updates via addresses in the parameter the
- largest and smallest values in the list pointed to
- by pList,given nElem number of elements. */
- void
- hiLow ( int *nHigh, int *nLow, int *pList, int nElem)
- { /* complete the requirements of this function */
- int i;
- *nHigh = *pList;
- *nLow = *pList;
- for (i=0; i<nElem; i++){
- if (*(pList+i)>*nHigh)
- *nHigh=*(pList+i);
- else if (*(pList+i)<*nLow)
- *nLow=*(pList+i);
- }
- }
- /* Bonus 1: Create another version of a function that
- will get input from the user as long as the
- input is not the sentinel value -999. The difference of
- getInputsV1() with this is that, here, you are required
- to use realloc() for your solution. This function will still
- also update the number of elements pointed to by pElem.
- Pre-condition: *pList contains a valid address (i.e., NULL if
- not yet allocated any space and memory
- address of location allocated and stored
- with *pElem number of elements)
- @param pList address where the pointer variable is stored
- @param pElem address where the number of elements have to be updated in
- */
- void
- getInputsV2(int **pList, int *pElem)
- {
- int *pTemp;
- int nInput;
- do {
- printf("Enter number: ");
- scanf("%d", &nInput);
- if (nInput != -999){
- /* write your statement to allocate 1 space more than
- the current number of elements to be pointed to by
- pTemp */
- pTemp=malloc(sizeof(int)*((*pElem)+1));
- if (pTemp != NULL) {
- /* write statements to store the new nInput as part
- of the dynamic list
- */
- if (*pList != NULL) {
- /* call function copy() to copy all previous contents
- of *pList to pTemp */
- copy (*pList,pTemp,*pElem);
- }
- *pList = realloc(pTemp, sizeof(int)*((*pElem)+2));
- *(*pList+*pElem)=nInput;
- (*pElem)++;
- }
- }
- } while (nInput != -999);
- }
- /* Bonus 2: This RECURSIVE function copies the contents of the list
- pointed to by pSource to pDest in reverse order. */
- void
- reverseListV2 (int *pSource, int **pDest, int nElem,int i)
- { /* complete the requirements of this function */
- if (nElem>0){
- *(*pDest+(nElem-1))=*(pSource+i);
- reverseListV2 (pSource,pDest,nElem-1,i+1);
- }
- }
- int main()
- {
- int *pList, *pReverse;
- int nOpt, nElem = 0;
- float fAve;
- int nHigh, nLow, i=0;
- pList = NULL;
- do {
- printf("Menu: \n");
- printf("1 - Get Inputs\n");
- printf("2 - Display Contents of List\n");
- printf("3 - Get Highest and Lowest Values in List\n");
- printf("4 - Reverse Contents of List\n");
- printf("5 - Get Inputs, Realloc'd \n");
- printf("6 - Reverses, Recursively \n");
- /* add printf()'s for option 5 and 6 if the Bonus 1 and
- Bonus 2 are implemented.
- */
- printf("0 - Exit\n");
- printf("\nEnter option: ");
- scanf("%d", &nOpt);
- switch (nOpt) {
- case 1 : getInputsV1 (&pList, &nElem); break;
- case 2 : displayList (pList,nElem); break;
- case 3 : hiLow (&nHigh, &nLow, pList, nElem);
- printf("Highest number is %d\n", nHigh);
- printf("Lowest number is %d\n", nLow); break;
- case 4 : reverseListV1 (pList, &pReverse, nElem);
- copy(pReverse, pList, nElem); break;
- case 5 : getInputsV2 (&pList, &nElem); break;
- case 6 : pReverse = malloc(sizeof(int)*nElem);
- reverseListV2 (pList, &pReverse, nElem, i);
- copy(pReverse, pList, nElem);
- break;
- default : exit(0);
- }
- /* provide the necessary statements to call the appropriate
- functions and to display the result depending on the
- option chosen. */
- } while (nOpt != 0);
- /* deallocate any dynamically allocated memory */
- free(pList); free(pReverse);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement