Question

In: Computer Science

I've written three functions to increase the value of a counter in an increment of one,...

I've written three functions to increase the value of a counter in an increment of one, but when I call the functions and then try to print out their values (which should have been updated) I get the values they were originally intialized to which was 0. Below is my code for the 3 functions and where I call them in my main. Can someone please tell me where I'm going wrong?

void addsec(int hr, int min, int sec){

               ++sec;

               if(sec==60){

                              ++min;

                              sec=0;

                              if(min==60){

                                             ++hr;

                                             min=0;

                                             if(hr==24){

                                                           

                                                            hr=0;

                                             }

                              }

               }

               return;

}

void addmin(int hr, int min){

               ++min;

               if(min==60){

                              ++hr;

                              min=0;

                              if(hr==24){

                                                           

                                             hr=0;

                              }

               }

return;

}

void addhour(int hr){

               ++hr;

               if(hr==24){

                              hr=0;

               }

return;

}

int main(int argc, char* argv[]){

int sec=0;
int min=0;
int hr=0;

printf("the current value of hour is %d\n", hr);
printf("\n");
printf("the current value of minute is %d\n", min);
printf("\n");
printf("the current value of second is %d\n", sec);
printf("\n");

/*test that add one to hr, min, sec works, then print military and standard time*/
   addsec(hr, min, sec);
   printf("the updated value of second is %d\n", sec);
   printf("\n");
   addmin(hr, min);
   printf("the updated value of minute is %d\n", min);
   printf("\n");
   addhour(hr);
   printf("the updated value of hour is %d\n", hr);
   printf("\n");
...

Solutions

Expert Solution


#include <stdio.h>

void addsec(int hr, int min, int *sec){

++(*sec);

if(*sec==60){

++min;

sec=0;

if(min==60){

++hr;

min=0;

if(hr==24){

hr=0;

}

}

}

return;

}

void addmin(int hr, int *min){

++(*min);

if(*min==60){

++hr;

min=0;

if(hr==24){

hr=0;

}

}

return;

}

void addhour(int *hr){

++(*hr);

if(*hr==24){

hr=0;

}

return;

}

int main(int argc, char* argv[]){

int sec=0;
int min=0;
int hr=0;

printf("the current value of hour is %d\n", hr);
printf("\n");
printf("the current value of minute is %d\n", min);
printf("\n");
printf("the current value of second is %d\n", sec);
printf("\n");

addsec(hr, min, &sec);
printf("the updated value of second is %d\n", sec);
printf("\n");
addmin(hr, &min);
printf("the updated value of minute is %d\n", min);
printf("\n");
addhour(&hr);
printf("the updated value of hour is %d\n", hr);
printf("\n");
}

// The issue we need pass use call by reference to effect changes

// so you have used call by value because of that changes did't had any impact

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

If the value of semaphore is negative, ___? A. an increment operation can be perform on...
If the value of semaphore is negative, ___? A. an increment operation can be perform on it. B. a process is waiting forever. C. there will be no process waiting on that semaphore. D. a decrement can be perform on it. ((( ( can negative semaphore decreases in case of more waiting processes? Explain in details. )))
A) What are three major functions of money?      B) How does the FED increase the amount...
A) What are three major functions of money?      B) How does the FED increase the amount of money in the banking system?      C) What are the three main parts of the FED?  What does each do?      D) Explain the meaning of central bank independence
Provide three materials on how to speak with confidence, one from a written material, one from...
Provide three materials on how to speak with confidence, one from a written material, one from a video (could be youtube, ted talk or other videos), one from the people you observed and talked to. Briefly discribe what they talk about. Explain how these materials help you speak with confidence more.
In a grocery store, there is one cashier counter. Customers arrive at the cashier counter according...
In a grocery store, there is one cashier counter. Customers arrive at the cashier counter according to a Poisson process. The arrival rate is 30 customers per hour. The service time is exponentially distributed. The mean service time is 1 minute 30 seconds. What is the expected waiting time?
QUESTION ONE Operations Management is one of the three major functions of any organisation. No organisation...
QUESTION ONE Operations Management is one of the three major functions of any organisation. No organisation can do without the Operations function. It exists for manufacturing and service organisations as well as to profit and non-profit making organisations. It is such a costly part of an organisation but contribute to a large extent to the success of the organisation. However, organisations are impacted continuously by business pressures and these provide them with the biggest challenges. Organisations often look to the...
write three functions in C++: one that declares a large array statically, one that declares the...
write three functions in C++: one that declares a large array statically, one that declares the same array on the stack, and one that creates the same array on the heap. Call each of them a number of times (at least 100,000) and output the time required by each. Explain the results.
Question on Database Systems - Concurrency Control A popular update operation is increment/decrement the numeric value...
Question on Database Systems - Concurrency Control A popular update operation is increment/decrement the numeric value of data object by 1. Suppose we are now to create a lock mode I, along with usual shared and exclusive lock modes. Consider a locking protocol that will require a request for a lock with mode I on a data object Q (with numeric value) before proceeding with the increment/decrement operation on Q. (i) Provide a lock compatibility matrix with these three modes....
36) Construct BCD counter and draw block diagram of a three-decade decimal BCD counter. The following...
36) Construct BCD counter and draw block diagram of a three-decade decimal BCD counter. The following must be included (5points) : - a) The truth table of BCD counter b) The implementation of BCD counter c) The three-decades BCD counter
Discuss three functions of the money market. Describe three of functions of mutual funds.
Discuss three functions of the money market. Describe three of functions of mutual funds.
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT