Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Assignment 4 – Strings and Structures

60-141 – Introduction to Programming II Winter, 2017
(Saturday, March 25, 2017: 23:59:59)
Write a C program that uses string processing functions to properly format a Comma Separated Values (csv) file of course Information and store them into an array of structures. Once all data have been formatted and stored, your program should be able to:
a. Add new course
b. Search for course information using either course name or course number as the search key
c. Display all or any course data.
The content of the input file (input.csv) is read by the program using input redirection technique that you already have used in the earlier assignments.
Assume that the CourseInfo structure has been defined as follows:
struct CourseInfo{
int courseID;
char courseName[50];
char courseCode[12];
char Term [6];
};
typedef struct CourseInfo courseInfo; //optional
Example:
Fields in the input file are comma separated as follows: CourseName,Faculty,Subject,Level,Section,Semester,Year
Content of a sample input file:
programming,03,60,141,01,W,2015
programming,03,60,141,30,W,2015
Algebra,03,62,102,02,S,2013
Religion,08,98,938, 20,F,2014
Corresponding field values in the array of structures (after formatting):
assignment 4-1

Hints:
1- For formatting data, the following points are to be considered:
a. Course ID should be unique sequential integers starting from 1 (not from 0).
b. Course name must be all capital letters (upper case).
c. Course Code should also be unique and store in the format as xxxx-xxx-xx. The first xxxx part
indicates faculty code (xx) and subject code (xx). The second xxx part indicates level and the
last xx part indicates section number. For example 0360-141-30
d. Term should be one character indicating semester (‘S’, ‘F’,’W’) following by 4 digits indicating
a year. For example S2010, W2014.
2- Your program should implement at least the following functions:
a) loadCourseInfo() : To read all data from input file and format and store them in an array of
structures.
b) displayCourseInfo(), To print a table indicating the course Information. For example:

assignment 4-2
c) addCourseInfo(): To add a new course. Note that:
– CouseID should be unique and generated automatically by your program (last
courseID+1).
– CourseCode is also unique and you cannot have two courses with the same
courseCode. So, before adding a course, search for the courseCode to be sure that
you have not had it previously.
d) searchCourseInfo(): To search for a course information using courseID or course Name and
print the course information if it exists.
Requirements:
– Write and document a complete C program that is capable of satisfying the requirements of this
assignment problem.

Answer:

/*
Title: Assignment#4: Strings and Structures
Objective: This program uses string processing functions to properly format a comma Separated Values(csv) file
of course information and store them into an array of structures.
*/

//C-Preprocessor Directives
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

struct CourseInfo{
	int CourseID;
	char courseName[50];
	char courseCode[12];
	char Term[6];
};

typedef struct CourseInfo Course;

Course courses[100];
int numCourse = 0;

//Function Prototypes
//To Read all the data from input file and format and store them in an array of structures
void loadCourseInfo();
void displayCourseInfo();//Prints a table indicating the course information
void addCourseInfo(char *, char *, char *);//This function is used to add a new course
//This function is used to search for a course information using courseID
// or course Name and print the course information if it exists
void searchCourseInfo(int);
void addNewCourse();//This function is used to add new course information and store in structure

int main (){
	int select;
	loadCourseInfo();
	int loop=1;
	while (loop) {
	//display the menu options
	printf("Please select an option\n"
		    "1 - Add a new course\n"
		    "2 - Search for a course information using course name\n"
		    "3 - Search for a course information using course number\n"
		    "4 - Display all or any course data\n"
		    "0 - Quit\n");
	scanf("%d", &select);
		switch(select){
			case 0:  loop = 0;
				 break;
			case 1:
				addNewCourse();
				break;
			case 2:
				searchCourseInfo(1);
				break;
			case 3:
				searchCourseInfo(2);
				break;
			case 4:
				displayCourseInfo();
				break;
			default: printf("Invalid selection, please choose a number between 1 and 4, or enter 0 to Quit\n");
		}
	}
return 0;
}

/*
Objective: To Read all the data from input file and format and store them in an array of structures.
Input:There are no parameters
Output: There is no output
*/
void loadCourseInfo(){

	char *stringToken,row[100],courseCode[12],courseName[50],term[6];
	FILE *fPointer;
	int k = 0;

	fPointer = fopen("input.csv","r");

	while (fscanf (fPointer, "%s\n", row) != EOF){

		// Use "," as the delimiter, and store them in an array of structures
		stringToken = strtok(row, ",");

		while(stringToken != NULL){
			switch(k){
				case 0:
					strcpy(courseName, stringToken);
					break;
				case 1:
					strcpy(courseCode,stringToken);
					break;
				case 2:
					strcat(courseCode,stringToken);
					strcat(courseCode,"-");
					break;
				case 3:
					strcat(courseCode,stringToken);
					strcat(courseCode,"-");
					break;
				case 4:
					strcat(courseCode,stringToken);
					break;
				case 5:
					strcpy(term, stringToken);
					break;
				case 6:
					strcat(term, stringToken);
					break;
			}
			k++;
			stringToken = strtok(NULL, ",");
		}
		k=0;

		addCourseInfo(courseName, courseCode, term);
	}
		displayCourseInfo();
	fclose(fPointer);

}

/*
Objective: Prints a table indicating the course information.
Input: no parameters for this function
Output: no return for this function
*/
void displayCourseInfo(){
	int i;
	printf("\n     Content of input.csv file \n\n");
	printf("\nID   Name            Code        Term \n");
	printf("_______________________________________ \n");

	for (i = 0; i < numCourse; i++){
			//Add 1 to make sure we start from 1 not 0
			printf("%-5d", (courses[i].CourseID)+1);
			printf("%-16s", courses[i].courseName);
			printf("%-12s", courses[i].courseCode);
			printf("%-5s", courses[i].Term);
			printf("\n");
	}
	printf("\n\n");
}

/*
Objective: This function is used to add a new course
Input: cName, cCode and term arrays
Output: no return for this function
*/
void addCourseInfo(char cName[50], char cCode[12], char term[6]){
	int j;

	for (j = 0; j < strlen(cName); j++){
			cName[j] = toupper(cName[j]);
	}

	strcpy(courses[numCourse].courseName, cName);
	strcpy(courses[numCourse].courseCode, cCode);
	strcpy(courses[numCourse].Term, term);
	numCourse++;
	courses[numCourse].CourseID = numCourse;

}

/*
Objective: This function is used to search for a course information using 
courseID or course Name and print the course information if it exists.
Input: cName, cCode and term arrays
Output: no return for this function
*/
void searchCourseInfo(int select){
	int i, j, m;
	int searchID;
	char searchName[50];
	int isThere = 0;

// Using the course name to find if this course exists
	if (select == 1) {
		printf("\nPlease enter the course name you want to search for\n  ");
		scanf("%s", searchName);

		for (j = 0; j < strlen(searchName); j++)
			searchName[j] = toupper(searchName[j]);

		printf("\nResults:\n");

		for (i = 0; i < numCourse; i ++){

			if (strcmp(courses[i].courseName, searchName) == 0){
				printf("%-8d", (courses[i].CourseID)+1);

				for (m = 0; m < strlen(courses[m].courseName); m++){
					courses[i].courseName[m] = toupper(courses[i].courseName[m]);
				}

				printf("%-16s", courses[i].courseName);
				printf("%-16s", courses[i].courseCode);
				printf("%s", courses[i].Term);
				printf("\n");

				isThere++;
			}
		}

		if (isThere == 0){
		printf("%s course was not found.\n", searchName);
		}
	}

    // Using the course ID to find if this course exists
	else {
		printf("\nPlease enter the course ID you want to look for\n ");
		scanf("%d", &searchID);
		printf("\nResults:\n");

		if(searchID <= numCourse)
		{
			printf("%-8d", searchID);

			for (j = 0; j < strlen(courses[searchID-1].courseName); j++){
				courses[searchID-1].courseName[j] = toupper(courses[searchID-1].courseName[j]);
			}
			printf("%-16s", courses[searchID-1].courseName);
			printf("%-16s", courses[searchID-1].courseCode);
			printf("%s\n\n", courses[searchID-1].Term);
		}
		else{
			printf("Can not find Course ID %d \n", searchID);
		}
	}
}

/*
Objective: This function is used to add new course information and store in structure
Input: course code,course Name,term,faculty Code,subject Code,level,section Number,semester and year.
Output: no return for this function
*/
void addNewCourse(){

	char courseCode[12],courseName[50],term[6],fCode[2],sCode[2],level[3],sectionNum[2],semester[1],year[4];
	int i, duplicate=0;

// Accept input from user

	printf("\nPlease enter the Course Name: ");
	scanf("%s", courseName);
	getchar();

	printf("Please enter the Course code \"xx\":  ");
	scanf("%s",fCode);
	getchar();

	printf("Please enter the Course Subject code \"xx\":  ");
	scanf("%s",sCode);
	getchar();

	printf("Please enter the Course Level code \"xxx\":  ");
	scanf("%s",level);
	getchar();

	printf("Please enter the Course Section code \"xx\":  ");
	scanf("%s",sectionNum);
	getchar();

// create course code
	strcpy(courseCode,fCode);
	strcat(courseCode,sCode);
	strcat(courseCode,"-");
	strcat(courseCode,level);
	strcat(courseCode,"-");
	strcat(courseCode,sectionNum);

	// Make sure the course code should be unique.
	for (i = 0; i <numCourse; i++){ if (strcmp(courses[i].courseCode, courseCode) == 0) duplicate++; } if (duplicate > 0){
		printf("You have a duplicate Course Code \n");
		return;
	}

	printf("Please enter \"S, F or W\" for the Course Semester : "  );
	scanf("%s", semester);
	getchar();
	semester[0] = toupper(semester[0]);

	printf("Please enter the Course Year \"yyyy\":  ");
	scanf("%s", year);
	getchar();

	strcpy(term, semester);
	strcat(term, year);

	addCourseInfo(courseName,courseCode,term);
}



About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!