Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
There's an N number of people entering in the theater and for each of them we know the exact time of entering and exiting.
Print the largest number of people that were in the theater at the same time and in which period.
Since you don't have any code yet, I'm going to assume that you are stuck on the algorithm. Here is the obvious way to solve this problem.
Create a list of all events, entry and exit.
Order that list by time.
Walk over the list maintaining a count of how many people are in the theater.
When you encounter an enter event, increment the count. When you encounter an exit event, decrement the count.
Keep track, whilst walking the list, of the largest value of the count that you have observed.
When you have walked the entire list, output the largest value that you observed.
It should be obvious to you how to extend this approach to keep track also of the times at which the count was at its peak value.
I'm not going to write the code for you since that would get in the way of your learning.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am working on CS50's Runoff problem in Problem Set 3, and the program is working when I run it. When I run the check program, however, it notes one particular function as being wrong entirely despite it working when I run the program manually. When I run the style check program to make sure there is no excess whitespace, etc; the results show extra text where there is none in the program.
In both of the screenshots I have the function returning the error in the top window.
Any ideas what is causing the mystery text, or the 4 errors? If it s more helpful, I can paste the entirety of the code here.
Thank you!
Do not be distracted by style50. Any style issues should never change the results of the program. The program is failing check50 because of functional deficiencies. The spec for print_winner says:
If any candidate has more than half of the vote, their name should be printed to stdout and the function should return true.
"More than half the vote" depends on the number of voters not the number of candidates. Try an election with 3 candidates a, b, c with 7 voters who vote b,b,b,b,a,a,c. Who wins? (b). What result does program return?
Deal with style issues after program passes check50 and before submit50. (But it's good practice to double check check50 results after cleaning up style, lest a bug creep in :)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm new to C and I have an assignment where I have to build a dictionary (Linked List in a way). Basically the user inputs several words,year and their definition this:
Example:
love_#_2004_#_LOVING
trade_#_2001_#_INVEST
etc...
And basically I need a function to scan the definition (Ex: INVEST)
and gives me the word trade.
If the definition is related to more than only one word to give me back all the words it relates to.
What sort of a function do I need to scan these strings?
If the word you search is always the last one and the formatting is always the same, then use strtok with _ and copy the last entry, which holds the string you are looking for.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to make a C program to arrange a jumble word into meaningful English word.There is a file "dictionary.txt" which contains lots of jumbled words. So, i have to write a program which read the jumble word from this file and convert it into a meaningful word.
For example:-
dictionary.txt file exits a "epemaxl" word
when we provide this input, the output should come "example".
I have searched alot over internet but didnot get a suitable example according to this.
Please help me.
Thanks in advance.
One strategy can be to calculate the levenshtein distance and select the word that has the closest levenshtein distance to your jumble word.
If you are on Linux (say Debian or Ubuntu, can't tell about the rest of flavors), you could skip the making of real dictionary and just check with /usr/share/dict/ wordlist.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
How can I delete an integer from a list initialized with int list[9999]?
I know how to remove a specified integer from that list by specify a key of list, but I will need to shift other elements to the left. What is the alternative?, shifting all elements is a highly cost CPU operation, should I use a Linked List and delete from memory that entity from the list, the other elements to be untouched?
Thanks!
If you want constant time insertion/deletion, a linked list is pretty much required - but iterating to the desired element is still going to be linear time. However, there may be a better way to optimize your program. Are you performing this operation very frequently? Can you maybe perform this operation less frequently by changing the structure of your program? A CPU can shift 39K (worst-case scenario with 10000 elements) of data pretty darn quickly. Are you sure this is your bottleneck?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have the following recursion method. I get an error stack overflow. it stops at -9352. My questions, is stack overflow the same as an infinite loop? Because this will keep calling itself.
But if I do a infinite loop with while, until, do, etc it doesn't give me the same stack overflow error. It just keeps going until my system runs out of memory.
This is using Ruby
def recursion(n)
print n
recursion(n-1)
end
recursion(3)
output:
3
2
1
0
.
.
.
-9352 stack overflow stops
Recursion and looping are techniques that can solve similar problems in different ways (as mentioned in the comments, they are Turing equivalent, but this is not my field).
Each function call adds a frame to the call stack. This requires additional memory and as your call chain goes deeper, it requires more memory until a certain limit is crossed, which makes your stack overflow and your program to crash.
Your recursive code adds more and more frames to the call stack and, given finite amount of memory, will cause it to overflow. You need some way to tell the recursion when to stop, and do so before the memory is exhausted. Such condition is equivalent to the base case in the mathematical induction, therefore it is usually referred to as such.
Another option, pointed in the comments, is utilizing Tail call optimizations, which replace the current frame in the stack and therefore may prevent the stack from overflowing.
Your iterative solution only requires a fixed amount of memory.
Only the value of a counter or other predefined variables is changed, therefore it does not incur any memory overhead.
If you do not limit the output, it could theoretically go on indefinitely, but some other exhaustion or error will most likely kill it. However, this will not be the memory consumed by the variables used in the loop itself.