Using one array print frequency of elements in array? [closed] - c

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 8 years ago.
Improve this question
Write an algorithm called occurrences that, given an array of numbers A, prints all the distinct values in A each followed by its number of occurrences.
For example, if A = <28, 1, 0, 1, 0, 3, 4, 0, 0, 3>, the algorithm should output the following five lines (here separated by a semicolon 28 1; 1 2; 0 4; 3 2; 4 1.
The algorithm may modify the content of A, but may not use any other memory.
Each distinct value must be printed exactly once.
Values may be printed in any order.

Possible solution will have 2 steps:
Sort your array
Iterate on array storing current value and number of its occurrences and printing current value/count pair when current value changes
This solution does not require extra memory and has complexity O(n*log(n)) as sorting is the "heaviest" part of algorithm

Related

how to print permutations of array of strings in c without recursion? [closed]

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
Is anyone here who can explain me how to print the permutations of a array of strings in iterative method without using recursion. Please explain with code.
If the string is "abcd", put all of the "a" chars in position 0 for the first n-1! arrays, in position 1 for the next n-1! arrays, etc. Then put all of the "b" chars in position 1 for the first n-2! arrays, etc, all of the "c" chars in position 2 for the first n-3! arrays, etc, and all of the "d" chars in position 3 for the first n-4! arrays, etc, using modulo n arithmetic in each case to move from position 3 to position 0 as you are filling out the arrays.

Matrix in C without arrays [closed]

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 3 years ago.
Improve this question
Hello my coding teacher give us this exercise:
"Write a C program that, considering two values row and col (both int) chosen by the user, prints a matrix composed by row rows and col columns in which the first element is 1 and every next element is the previous one incremented by one."
I know that a matrix can be composed with arrays but we haven't study them yet so I have to make that only using basic C functions. How can I do?
It is true that, arrays can be used to store matrices like data in memory.
In your case, yout don't have to store anything. The problem is only about displaying matrix.
Please try to implement following steps in C.
Initialize counter to 1
For i = 1 to row
For j = 1 to col
Display counter
Increment counter
Put a line break
That's all.
Try this on a bit of paper.
Choose rows, say 2, and columns say 3.
You need a current value, starting at 1.
Write this down
1
Now move a bit and write the next value
1 2
Same again
1 2 3
... keep an eye on how many numbers you have printed out... We're at the 3 columns now.
So, print a new line '\n' and continue
1 2 3
4
This might end up being a bit wonky if the numbers get big, but you need a current_value starting at 1, and a loop, over rows and columns.
You can walk over the numbers and print as you go, rather than storing them.
int current_value = 1;
for(int row=0; row < rows; ++row)
{
for(int column = 0; column < columns; ++column)
{
printf("%i ", current_value++);
}
printf("\n");
}
Allocate memory for the matrix elements using malloc(). You'll need columns * rows * element size bytes.
malloc() will return you a pointer to the memory.
Use pointer arithmetic to access the elements.

Optimization of multiplexer to improve performance [closed]

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 4 years ago.
Improve this question
I am currently working on a school project that is a circuit simulator. One of the components to this circuit can be a multiplexer, which has n inputs, log2(n) selectors, and 1 output.
The way I determine which output is needed is by doing the following:
Generate (# of selectors) bit gray code table, and loop through the table and compare to the values of the selectors. Whichever row is a match is the output needed.
However, for larger multiplexers (16:1, 32:1), this becomes quite slow. Is there a more efficient way to get the output needed without having to compare every single possible graycode possibility?
So you wish to construct a unique number in the range [0, 2n) (the index of the selected input) from n inputs (the selector signals) which are in the range [0, 1].
That's binary code!
Assign one of the selector signals the value 1, the next signal the value 2, then 4, and so on. Add them together. Select the corresponding numbered input.
unsigned selectedInput = 0;
if (selector1) selectedInput += 1; /* or |= */
if (selector2) selectedInput += 2;
if (selector3) selectedInput += 4;
And so on. In the generic case:
unsigned selectedInput = 0;
for (int i = 0; i < selectorCount; ++i)
if (selectors[i]) selectedInput |= 1u << i;

How to Initialize an array in Assembly Language [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
Kindly tell me how to initialize an array in Assembly Language e.g
Initialize an array named as “Even”, which contain first ten EVEN numbers.
Even = 0,2,4,6,8,10,12,14,16,18
Initialize an array named as “Odd”, which contain first ten ODD numbers.
Odd=1,3,5,7,9,11,13,15,17,19,21
Add Both arrays (index wise) into a third array named as “Sum”.
SUM [0] = Even [0] + Odd [0]
SUM 1 = Even 1 + Odd 1
Up to
SUM [n] = Even [n] + Odd [n]
There are many ways of doing this. The simplest to create the array in a declaration:
EVEN:
.WORD 0, 2, 4
ODD:
.WORD 1, 3, 5
Using whatever size data you want.
To add them, you can:
Unroll the loop
Us a macro
Call a recursive function

Trouble starting an algorithm [closed]

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 6 years ago.
Improve this question
I'm having trouble on thinking of away to attack this problem.
X is defined below. For n=1,x=0.5,n=2,x=0.833.As you add more terms, X increases. Calculate n for which X becomes larger than 4. First write the algorithm and then implement the code in C.
x= 1/2+1/3+...1/n+1 answer: n = 83
The only thing I'm sure of is that it uses a for loop.At first I was thinking something like
For(int i = 0; i <= n.....
That doesn't seem close though.I dunno..Can I get a hint on where to start?
You will obviously compute the partial sums X.n and stop when X.n<4 and X.n+1>4.
To compute the partial sums, keep an accumulator variable and add the fractions one after the other
n= 0
S= 0
// Repeat the following instructions
n+= 1
S+= 1/(n+1) // Now, S = X.n
Remains to find the stopping condition. As the value of S goes increasing from 0, we will stop as soon as S exceeds 4. In other words, continue as long as S remains below 4.
n= 0
S= 0
while S < 4
n+= 1
S+= 1/(n+1) // Now, S = X.n
Translate that to C syntax.
Remains to look closer at the possibility that X.n = 4.

Resources