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?
Related
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
Right now I'm trying to write a program, and one part of the it involves expanding an existing array by copying values from a previous array into an array with a larger size.
The way I'm doing this by using an int variable which is defined in a previous point int the program by user input.
int[x] array;
int[x + 1] array2;
Will this work, or do I have to initialize a separate int variable with value of x + 1?
The correct syntax is:
int array[x];
int array2[x+1];
C 1999 and later supports this (with the value of x determined at run time), although it is optional in C 2011. Some compilers (of questionable quality) do not support it.
The space available for objects of this sort is typically limited to one to eight mebibytes or so, and that space must also serve for other program needs, so it should be used only for small arrays.
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.
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.
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 found this line in Linux Audio drivers soc-core.c inside sound folder:
int regsize = codec->driver->reg_word_size * 2;
Can anybody please explain the meaning of * 2?
Multiply the contents of codec->driver->reg_word_size by 2. I guess this is a translation between size in words to size in bytes.
Multiplies that value by 2. That's all it does
Well, I can just guess, but it looks like this:
codec is a pointer to a structure, which has a pointer to another structure in driver, which has a member variable reg_word_size (which it seems is, like the name says, the size of a register word). This value gets doubled (*2).
This could be, like the other answer says, a conversion between bytes and words. However, it could probably also just mean that this regsize should be twice as big as the reg_word_size.