Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. THIS IS MY MAIN.C FILE!!!!!!!!!!!!!
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "solution.h"
  6.  
  7. #define DEBUG 1
  8.  
  9. int main (void)
  10. {
  11.     FILE *fp = NULL;
  12.     int *allNum;
  13.     int numLength, i, num;
  14.  
  15.     fp = fopen ("testcases.txt", "r");
  16.    
  17.     if (fp == NULL)
  18.     {
  19.         printf ("File is empty!\n");
  20.         exit (1);
  21.     }
  22.    
  23.     fscanf(fp, "%d", &numLength);
  24.     #if DEBUG
  25.     printf ("Numlength: %d\n", numLength);
  26.     #endif
  27.     allNum = malloc(sizeof(int)*numLength);
  28.    
  29.     for (i = 0; i < numLength; i++)
  30.     {
  31.         allNum[i] = readNextNum(fp);
  32.         #if DEBUG
  33.         printf ("Number %d is: %d\n", i, allNum[i]);
  34.         #endif
  35.     }
  36.    
  37.     printf ("Unsorted Array: ");
  38.     printArray (allNum, numLength);
  39.    
  40.     #if DEBUG
  41.     printf ("\nPrintArray is not a problem\n");
  42.     #endif
  43.    
  44.     printf ("\nSorted Array: ");
  45.     mySort (numLength, allNum);
  46.    
  47.     if (fscanf(fp, "%d", &num) != EOF)
  48.     {
  49.         numLength = moreNum(fp, allNum, numLength, num);
  50.        
  51.         printf ("\nNEW array: ");
  52.         mySort (numLength, allNum);
  53.     }
  54.     else
  55.     {
  56.         printf ("\nThere are no other values in the file.\n");
  57.     }
  58.    
  59.     fclose(fp);
  60.     free (allNum);
  61.    
  62.     return 0;
  63. }
  64.  
  65.  
  66. THIS IS MY FUNCTIONS.C FILE ----------------------------------------------------------------------!!!!!!!!!!!!!!!!!!!!!!!!!!!
  67.  
  68. /* Patrick Bryan
  69.  * 0703010 */
  70.  
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #include "solution.h"
  74.  
  75. /* Code taken from Judi lab2 */
  76. int readNextNum(FILE *fp)
  77. {
  78.     int num;
  79.     if (fp == NULL)
  80.     {
  81.         printf ("You are supposed to test the file pointer for null!\n");
  82.         exit(1);
  83.     }
  84.     if (fscanf(fp, "%d", &num) != EOF)
  85.         return (num);
  86.     else
  87.     {
  88.         printf ("Scanned past the end of the file. Keep track of the length!\n");
  89.         exit (1);
  90.     }
  91. }
  92.  
  93. void printArray (int *array, int length)
  94. {
  95.     int i;
  96.    
  97.     for (i = 0; i < length; i++)
  98.         printf ("%d ", array[i]);
  99. }
  100.  
  101. int moreNum (FILE *fp, int *allNum, int numLength, int num)
  102. {
  103.     numLength++;
  104.     fp = realloc (fp, sizeof(int)*numLength);
  105.     allNum[numLength-1] = num;
  106.    
  107.     if (fscanf(fp, "%d", &num) != EOF)
  108.     {
  109.         numLength = moreNum (fp, allNum, numLength, num);
  110.     }
  111.    
  112.     return numLength;
  113. }
  114.  
  115. void mySort (int numLength, int *allNum)
  116. {
  117.     int biggestNum;
  118.     int swapSpace;
  119.     int j, i;
  120.    
  121.     #if DEBUG
  122.     printf ("\nProgram has successfully entered mySort function\n");
  123.     #endif
  124.    
  125.     /* Code taken from Quiz 1 */
  126.     for(i=0; i<numLength; i++)
  127.     {
  128.         biggestNum = allNum[i];
  129.         for(j=i; j<numLength; j++)
  130.         {
  131.             if (allNum[j] > biggestNum)
  132.                 biggestNum = allNum[j];
  133.         }
  134.         if (biggestNum != allNum[i])
  135.         {
  136.             swapSpace = biggestNum;
  137.             biggestNum = allNum[i];
  138.             allNum[i] = swapSpace;
  139.         }
  140.     }
  141.    
  142.     printArray (allNum, numLength);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement