Guest User

Untitled

a guest
Jan 24th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.79 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. #define INPUT_FILE "large.txt"
  8.  
  9. /* The maximum name length and number of people and movies */
  10. #define MAX_NAME_LENGTH 200
  11. #define MAX_PEOPLE 700
  12. #define MAX_MOVIES 200
  13.  
  14.  
  15.  
  16. /* The only global variables permitted in this project are below */
  17. char movies[MAX_MOVIES][MAX_NAME_LENGTH] = {0};
  18. char people[MAX_PEOPLE][MAX_NAME_LENGTH] = {0};
  19. int ratings[MAX_PEOPLE][MAX_MOVIES] = {0};
  20. int numberOfMovies = 0;
  21. int numberOfPeople = 0;
  22.  
  23. //main function
  24. int main(void) {
  25.    
  26.     //declaring variables and variable types
  27.     char movie[MAX_NAME_LENGTH], person[MAX_NAME_LENGTH];
  28.     int rating,  duplicate, i, stringSame;
  29.     int IdSetPep=0;
  30.     int iDSetMov=0;
  31.    
  32.     //the following code is for opening the input file for reading   **********************///
  33.    
  34.     FILE *fp; //identifying pointer fp, this will be where the input file is origonally stored
  35.  
  36.     fp= fopen("input.txt", "r"); //opening the input file
  37.     if (fp == NULL) { //if statement, when true will tell the user there is an error opening the file
  38.         printf("there is an error opening."); //tells the user there is an error
  39.         exit(EXIT_FAILURE);
  40.        
  41.         }
  42.  
  43. //the following code is used to set the input txt file into arrays*************************************////
  44.  
  45. //fscanf function is used to scan through the input file, it will set 'person' and 'movie' as strings and 'rating' will be an integer value
  46.     while (fscanf(fp, "%s %s %d", person, movie, &rating) != EOF){ //while loop will continue to scan through the input file until the End Of File is reached
  47.    
  48.    
  49.     // setting the variable 'duplicate' to zero. this variable will be used to monitor if a string has appeared before 
  50.         duplicate=0;
  51.    
  52.     //for loop will continue to cycle as long as 'i' is less than the variable 'numberOfPeople'. The variable 'numberOfPeople' is used to as an indicator of how many people have been stored in the new array that is being created.
  53.         for (i=0; i < numberOfPeople; i++){
  54.             stringSame= strcmp(people[i], person); //strcmp function tests the new array people[] in slot 'i' against the newly scanned 'person' string from the input file. If the string has appeared it will store a zero in the variable 'stringSame'
  55.             if (stringSame==0){ // if statement will test if variable stringSame is equal to zero
  56.                 IdSetPep=i; // 'IdSetPep' is a variable that acts as an identification for the individual people that are set into the new array 'people'
  57.                 duplicate=1;  // sets 'duplicate' to be true
  58.             }
  59.         }
  60.             if  (duplicate!=1) { //if statement will  works when there is no double up with the strings
  61.                 strcpy(people[i], person); // strcpy function sets the newly read 'person' string and copys it into the people[i] slot
  62.                 IdSetPep =numberOfPeople;
  63.                 numberOfPeople++ ; //increments the variable 'numberOfPeople'
  64.             }
  65.        
  66.            
  67.  
  68.         stringSame =1; //resets stringSame to 1
  69.         duplicate=0; //resets duplicate to zero
  70.  
  71.         for (i=0; i < numberOfMovies; i++){ //for loop will continue to cycle as long as 'i' is less than the variable 'numberOfMovies'. The variable 'numberOfMovies' is used to as an indicator of how many movies have been stored in the new array 'movies' that is being created.
  72.             stringSame=strcmp(movies[i], movie); //strcmp function tests the new array movies[] in slot 'i' against the newly scanned string 'movie' from the input file . If the string has appeared it will store a zero in the variable 'stringSame'
  73.             if (stringSame==0){ // if statement will test if variable stringSame is equal to zero
  74.                 iDSetMov=i; //'iDSetMov' is a variable that acts as an identification for the individual movies that are set into the new array 'movies '  
  75.                 duplicate=1; // sets 'duplicate' to be true
  76.             }
  77.         }
  78.        
  79.             if (duplicate!=1){ //if statement will  works when there is no double up with the strings
  80.                 strcpy(movies[i], movie); // strcpy function sets the newly read 'movie' string and copys it into the movies[i] slot
  81.                 iDSetMov=numberOfMovies;
  82.                 numberOfMovies++;  //increments the variable 'numberOfMovies'
  83.                 }
  84.            
  85.    
  86.    
  87.    
  88.    
  89.    
  90.             ratings[IdSetPep][iDSetMov]=rating; // sets the integer 'rating' values from the txt file into the relevant positions of the new array 'ratings'
  91.     }
  92.     //the following code is used to print out the 'ratings' array*********************************/////
  93.         for (IdSetPep=0; IdSetPep < numberOfPeople; IdSetPep++) { //for loops are used to select and print out every value in the array
  94.             for (iDSetMov=0; iDSetMov < numberOfMovies; iDSetMov++){ //for loops are used to select and print out every value in the array
  95.                 printf(" %d", ratings[IdSetPep][iDSetMov]); //printf function used to print outthe ratings values
  96.     }
  97.                 printf("\n"); //printf function used to put a space after each value
  98.         }
  99. return 0; // end of function
  100. }
Add Comment
Please, Sign In to add comment