This one?There was a recent thread on the same topic. I remember contributing some code. Could be worth a look for ideas. If you need help finding it, just ask.
https://forum.allaboutcircuits.com/...-repeated-number-in-array.167628/post-1483405
This one?There was a recent thread on the same topic. I remember contributing some code. Could be worth a look for ideas. If you need help finding it, just ask.
I referenced that thread in post #9.There was a recent thread on the same topic. I remember contributing some code. Could be worth a look for ideas. If you need help finding it, just ask.
I have seen that but I am trying to understand logic on paper first. Can anyone help me with the flow chart where I'm strugglingI referenced that thread in post #9.
Now you need to examine your toy problem and look for the underlying patterns.Numbers[ ] = [ 6, 4, 2, 1, 3, 1 ]
is 6 == 4 ? if yes then 6 is repeated and if no then look for second element
is 6 == 2? if yes then 6 is repeated and if no then look for second element
is 6 == 1? if yes then 6 is repeated and if no then look for second element
is 6 == 3? if yes then 6 is repeated and if no then look for second element
is 6 == 1? if yes then 6 is repeated and if no then look for second element
is 4 == 2? if yes then 4 is repeated and if no then look for third element
is 4 == 1? if yes then 4 is repeated and if no then look for third element
is 4 == 3? if yes then 4 is repeated and if no then look for third element
is 4 == 1? if yes then 4 is repeated and if no then look for third element
is 2 == 1? if yes then 2 is repeated and if no then look for fourth element
is 2 == 3? if yes then 2 is repeated and if no then look for fourth element
is 2 == 1? if yes then 2 is repeated and if no then look for fourth element
is 1 == 3? if yes then 1 is repeated and if no then look for fifth element
is 1 == 1? if yes then 1 is repeated and if no then look for fifth element
is 3 == 1? if yes then 3 is repeated and if no then stop
I certainly went with the sort first approach. In the back of my mind I wondered if there was some more elegant method, since a sort does use CPU time if the array is huge. I figured that the time a programmer spends avoiding a sort would be more than the time the computer spends just doing it.While your approach may work, have you considered simply sorting the numbers. Once sorted, the pairs will be adjacent to each other. You could also look for triplets, etc., as mentioned in that other thread on finding the mode.
Sorting requires accessing all of the data at least once. Then another pass would be required to access the sorted data. An algorithm that visits each element once will be faster.I’d be very curious to speedtest algorithms to see if sorting first is the winning strategy or whether another approach wins.
There are a number of compromise approaches.I certainly went with the sort first approach. In the back of my mind I wondered if there was some more elegant method, since a sort does use CPU time if the array is huge. I figured that the time a programmer spends avoiding a sort would be more than the time the computer spends just doing it.
I’d be very curious to speedtest algorithms to see if sorting first is the winning strategy or whether another approach wins.
Roger that. The context of the other thread was whatever you could come up with in a job interview. My opinion is that code that works for most cases would be the minimal acceptable answer. Ten percent of a “perfect” solution might impress the right person but would be a very risky strategy in an interview.But there really is no point trying get fancy until you are capable to doing it simple. Otherwise you lose sight of the forest for the trees.
;*******************************************************************************
list p=16f1829 ; list directive to define processor
#include <p16f1829.inc> ; processor specific variable definitions
errorlevel -302, -305
list st=off
RADIX dec
;*******************************************************************************
__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
;__0x09E4
__CONFIG _CONFIG2, _WRT_OFF & _PLLEN_ON & _STVREN_OFF & _BORV_19 & _LVP_OFF
;__0x1DFF
CBLOCK 0x20
answer
ENDC
#define RAMoffset 0x2A ;0x20 + 10
#define ArraySize 10
#define RAMend 52 ;CBLOCK start + RAMoffset + Array Size
org 0x00
bra Start
Start
ClearRAM
movlw RAMoffset ;begining GPR register |B0
movwf FSR1 ; |B0
RAMloop
clrf INDF1 ; |B0
incf FSR1,f ; |B0
movlw RAMend ;ending GPR register |B0
xorwf FSR1,w ; |B0
btfss STATUS,2 ; |B0
bra RAMloop ; |B0
movlw RAMoffset
movwf FSR1
Array2Test ;6,4,2,1,3,1
movlw 6
call Loop
movlw 4
call Loop
movlw 2
call Loop
movlw 3 ;1 Various numbers tested here
call Loop
movlw 3
call Loop
movlw 1
call Loop
;add safety net for end in case no duplicates
NOP
Test4Dup
Loop ;enter with value in WREG
addwf FSR1,f
btfsc INDF1,7
bra Duplicate
bsf INDF1,7
subwf FSR1,f
return
Duplicate
movwf answer
nop ;light LED or other signal
bra $-1
END
I think I have chosen a difficult path which is very difficult to walk. Where do i start to find the repeated numbers. How to set small tasks to achieve this big problem.Can you turn that into pseudocode or a flow chart.
Humans are good at recognizing patterns, as are some other species, including monkeys, who might actually be better than us. So for the problem you show, most humans would solve it by inspection, I think.Can you manually show the calculation how human know the repeated number on paper.
Stop. Of course you do. How would you do it in your head? Copy that process in your flowchart. Your mind keeps track of a number and counts how many times it's seen that number.... seems like you can repeat that process in your flow chart maybe perhaps using another array....Program supposed to find repeated number
Numbers[ ] = [ 6, 4, 2, 1, 3, 1 ]
Program output : Number 1 repeats 2 times
I am trying to solve with flow chart but after arrow I have no idea how to processed next steps.
View attachment 203226
If you can follow the logic of the flowchart I posted, it should be straightforward to write the code.I think I have chosen a difficult path which is very difficult to walk. Where do i start to find the repeated numbers. How to set small tasks to achieve this big problem.
Let's start with the most basic step of processing a list of numbers -- walking across the list and doing something.I think I have chosen a difficult path which is very difficult to walk. Where do i start to find the repeated numbers. How to set small tasks to achieve this big problem.
Can you manually show the calculation how human know the repeated number on paper. I mean calculation not flow chart and pseudocode so that I can try to make flow chart for handy calculation
Here is my codeSo, just for example, we might have
int data[] = {5, 3, 7, 1, 6, 3, 9, 12 , 21, 13, 16};
int sizeOfData = 11;
Can you write the code to walk across the data and do nothing more than just print out each number on a separate line?
#include <stdio.h>
int main()
{
int data[11] = {5, 3, 7, 1, 6, 3, 9, 12 , 21, 13, 16};
for ( int sizeOfData = 0; sizeOfData <11; sizeOfData++)
printf( "data = %d \n", data[ sizeOfData]);
return 0;
}
most of people suggest advice to solve problem with paper and pencil. I have tried my best attempt to solve problem if possible can someone show me how do you solve the problem with paper and pencil?This is an interesting problem and one that emphasizes the importance of employing a proper problem solving strategy in the first place.
As I have always maintained, writing code is the last thing you want to do. You need to arrive at an algorithm or strategy first. Then you can draw a flowchart. Pseudocode serves the same purpose as a flowchart. You may create either or both as an exercise.
Granted. Paper and pencil is a useful visual aid.most of people suggest advice to solve problem with paper and pencil. I have tried my best attempt to solve problem if possible can someone show me how do you solve the problem with paper and pencil?