Assuming that courseCode
is a single digit number representing the course you could do something like this:
printf("Please enter the number of courses you'd like to take: ");
int numOfCourses;
scanf_s("%d", &numOfCourses);
int courseCode;
int courseDay;
int courseTime;
// unsigned long to have the most possible space
unsigned long courseCodeArray = 0;
unsigned long courseDayArray = 0;
unsigned long courseTimeArray = 0;
int i = 0;
while (i < numOfCourses) {
// single digit code
printf("Please enter the code of the course: ");
scanf_s("%d", &courseCode);
// 1 = Monday, 2 = Tuesday, 3 = Wednesday , etc.
printf("Please enter the day of the course: ");
scanf_s("%d", &courseDay);
// 1 = 8:00, 2 = 10:00, 3 = 12:00, etc.
printf("Please enter the time of the course: ");
scanf_s("%d", &courseTime);
courseCodeArray += courseCode;
courseDayArray += courseDay;
courseTimeArray += courseTime;
courseCodeArray = courseCodeArray * 10;
courseDayArray = courseDayArray * 10;
courseTimeArray = courseTimeArray * 10;
i++;
}
This way you would have an "array" made with a single int that could work as long as the code of the course is an integer between 1 and 9.
E.G. the numbers: 435
, 132
and 431
Would mean: 4-1-4
Course 4 Monday at 14:00
, 3-3-3
Course 3 Wednesday at 12:00
and 5-2-1
Course 5 Tuesday at 8:00
respectively.
Of course you then need to write some checks to ensure that the user will enter only valid numbers, that you don't go past the maximum unsigned long
value and make better design choices but i think this is what your professor meant by
Make your code robust and easy to read
So I won't do absolutely everything for you ;)
With this you essentially store multiple values in a single unsigned long
or int
as long as you know how to "decode" the single digits to their representation. Just divide the arrays by 10 to remove the last 0 and then read digit by digit of the resulting "array" numbers and convert them back.
scanf()
family of functions, always check the return value (not the parameter values) to assure the operation was successful. Note: Those functions return the number of successful 'input format conversion specifiers. or EOF. Any returned value other than the number of expected conversion specifiers indicates an error occurred.