0

I am not allowed to use arrays nor anything else I have not yet studied in the course, e.g: pointers, dynamic memory allocations, etc. What I have studied so far is: Functions and loops. I need to list all of the course codes, days, and times for as many courses as the user has specified in the first scanf_s call. I have no idea how to proceed without using arrays. Any hints/help would be appreciated.

printf("Please enter the number of courses you'd like to take: ");
int numOfCourses;
scanf_s("%d", &numOfCourses);
int courseCode;
int courseDay;
int courseTime;

int i = 0;
while (i < numOfCourses) {
  printf("Please enter the code of the course: ");
  scanf_s("%d", &courseCode);
  printf("Please enter the day of the course: ");
  scanf_s("%d", &courseDay);
  printf("Please enter the time of the course: ");
  scanf_s("%d", &courseTime);
  i++;
}
12
  • what does list of all course codes mean? BTW, you can start with using some good indentation in your code.
    – Serge
    Oct 27, 2019 at 18:46
  • Yeah the formatting got all messed up when I copied it to here. Basically, the user must specify how many courses he has, and must enter the code for all the courses alongside their times and days using only integers. The program must create a table from all the user inputs; I don't know how to save the user's inputs without using an array, though. Oct 27, 2019 at 18:50
  • OT: when calling any of the 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. Oct 27, 2019 at 19:16
  • what is the max number of courses that a student can take? Oct 27, 2019 at 19:20
  • @BallenAbdullah you can fix indentation in your question then. As for the table, you still need to explain a bit more. What is this table? in a file? on the screen? on a web page? You cannot have it on the screen from which you get inputs without disallowed data struct or intermediate saving it into a file.
    – Serge
    Oct 27, 2019 at 19:32

2 Answers 2

0

Suggest:

1) create a struct that holds all the info for an individual student 2) create a file, perhaps in the local directory or in /tmp 3) for each student, after collecting the information, write the struct to the file

--or--

given the MAX number of courses that a student can take, then code similar to:

int   course1day;
int   course1time;
int   course1code;
(repeat for MAX number of courses, increment the digit for each course)

a consideration is: what will the code be doing with this information after it is collected?

2
  • The max number of courses is unknown. How can I incorporate that into my code? Oct 27, 2019 at 23:28
  • for max number of courses, Suggest some large number that is beyond what a student could actually take, For instance 20 Oct 28, 2019 at 6:51
0

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.