3

I have a perfect running code, BUT one criteria for this homework is that it has atleast two different functions. How can i devide this code into one more functions?

I want to sort the alarmClock() function into more functions. There's alot going on in there. Maybe a updateTime() function. I tried something like this but this doesn't work:

#include <stdio.h>

void alarmClock(int, int);
void updateTime(int, int, int);

int main() {
  int present_time;
  int time_for_alarm;

  printf("Time indicates in HHMMSS! \nPresent time: ");
  scanf("%d", &present_time);

  printf("Time for alarm: ");
  scanf("%d", &time_for_alarm);

  if (present_time == time_for_alarm)
    printf("ALARM!");
  else
    alarmClock(present_time, time_for_alarm);

  return 0;
}

void alarmClock(int presT, int alarmT) {
  int presentHH = presT / 10000;
  int presentMM = (presT / 100) % 100;
  int presentSS = presT % 100;

  int combineTime;

  while (presT != alarmT) {
    printf("%02d:%02d:%02d \n", presentHH, presentMM, presentSS);
    presentSS++;
    updateTime(presentHH, presentMM, presentSS);
    combineTime = presentHH * 100 + presentMM;
    presT = combineTime * 100 + presentSS;
  }
  printf("ALARM!");
}

void updateTime(int presentHH, int presentMM, int presentSS) {
  if (presentSS > 59) {
    presentSS = 0;
    presentMM++;
    if (presentMM > 59) {
      presentMM = 0;
      presentHH++;
      if (presentHH > 24) {
        presentHH = 1;
      }
    }
  }
}

My teacher hinted me saying "you could make on printTime() function and one updateTime() function sending present_time as arguments". But i dont know how...

This is my working code that needs atleast one more function.

#include <stdio.h>

void alarmClock(int, int);

int main() {
  int present_time;
  int time_for_alarm;

  printf("Time indicates in HHMMSS! \nPresent time: ");
  / scanf("%d", &present_time);
  /

      printf("Time for alarm: ");
  scanf("%d", &time_for_alarm);

  if (present_time == time_for_alarm)
    printf("ALARM!");
  else
    alarmClock(present_time, time_for_alarm);

  return 0;
}

void alarmClock(int presT, int alarmT) {
  int presentHH = presT / 10000;
  int presentMM = (presT / 100) % 100;
  int presentSS = presT % 100;

  int combineTime;

  while (presT != alarmT) {
    printf("%02d:%02d:%02d \n", presentHH, presentMM, presentSS);
    presentSS++;

    if (presentSS > 59) {
      presentSS = 0;
      presentMM++;
      if (presentMM > 59) {
        presentMM = 0;
        presentHH++;
        if (presentHH > 24) {
          presentHH = 1;
        }
      }
    }
    combineTime = presentHH * 100 + presentMM;
    presT = combineTime * 100 + presentSS;
  }
  printf("ALARM!");
}

The working code gives this output (correct output);

if present_time = 115957
and time_for_alarm = 120001

output is 11:59:57 11:59:58 11:59:59 12:00:00 ALARM

but when i created the updateTime() function the code keeps running forever if i have these values:

if present_time = 115957
and time_for_alarm = 120001

output is 11:59:57 11:59:58 11:59:59 11:59:60 11:59:61 11:59:62 11:59:63 ... and so on and on (presentSS keeps going +=1 forever)

Kami Kaze
  • 2,069
  • 15
  • 27
  • 2
    i haven't looked at all the code (and the forarmatting really makes it difficult). But keep in mind that if you change values of `presentXX` in `updateTime()` that happens only to the internal values witthin the function and has no effects to the calling function – Ingo Leonhardt Sep 05 '19 at 14:32
  • I would just like to say, that even as this is a homework question, I gave an upvote. The question shows a clear question with OPs current work and what OP tried to solve the problem. For the first post and especially a homework question this much more than what I am used to.... Good job OP – Kami Kaze Sep 05 '19 at 14:53
  • You should however take some time to format your code better as it will make it more readable for users here, but also yourself. – Kami Kaze Sep 05 '19 at 15:00
  • Related: https://stackoverflow.com/questions/2229498/passing-by-reference-in-c/30519731#30519731 – S.S. Anne Sep 05 '19 at 21:55

2 Answers2

5

The variables presentHH, presentMM, and presentSS in the function updateTime are distinct from the ones in alarmClock, so changes to those variables in updateTime are not visible in the calling function.

You need to pass the address of each of these variables and dereference those pointers in updateTime. Then you're actually changing the variables defined in alarmClock.

So change the definition of updateTime to:

void updateTime(int *presentHH, int *presentMM, int *presentSS);

And call it like this:

updateTime(&presentHH, &presentMM, &presentSS);

You'll also need to change the body of updateTime to reflect that the parameters are now pointers and to dereference them. I'll leave that as an exercise to the reader.

dbush
  • 205,898
  • 23
  • 218
  • 273
-1

dbush shows a good way to solve the problem with not many changes to the general structure. I would suggest a different approach to the task. Instead of passing the addresses of these values, you could restructure your code to pass the composite time and return the updated time. Similiar to this

int updateTime(int presT){

  //seperate, update and combine the time value

  return updatedTime;
}

this would reduce the interface and would also enable you to write even more functions that can be reused. As you now will have to seperate and combine the time in updateTimeand alarmClock you can write functions that do this instead of writing the same code twice.

example:

int getHours(int time){
  return time / 10000;
}

and:

int combineTime(int hours, int minutes, int seconds){
  combinedTime = (presentHH * 100 + presentMM)* 100 + presentSS;
  return combinedTime;
}

This will help you get more expierience with functions and also helps to let you see one big benefit of functions (having reusable code).

Kami Kaze
  • 2,069
  • 15
  • 27