write an algorithm that finds mode in an array of numbers (pascal language)
I don't use pascal, but try to understand this code I wrote in C++. The negative thing about this algorithm is it finds only 1 mode. If there is more than 1, you should use another algorithm or keep modes in an array. #include <iostream> using namespace std; int main(){ int a[10] = {1,2,3,2,2,3,1,0,10,11};//it's our array. int i,j,mode,temp=0,max=1;// temp check how many times the same number occurs //max is 1 at the beginning, you should compare temp with max for(i=0;i<10;i++){ for(j=0;j<10;j++){ if(a[i]==a[j]){//for the number "x", if finds another "x" temp++;//then temp=temp+1 } if(temp>=max){//if temp is greater or equal to max, then our new max = temp. max=temp; mode=a[i]; } } temp=0;//here we set temp 0 before the loop checks for another number } cout<<"mode is: "<<mode<<" with "<<max<<" repetitions "<<endl;//prints mode and max } PS:The code is working, let me know if there is something you didn't get.
the thing is that i do not know anything about c++anyway i will try and i++ means that it decreases by one or not thank you
i++ means i = i+1 not decreasing, it increases the number if you know any other language maybe I can help further
I know Java ( a little because I've just started ) Can you help me with this build a recursive algorithm that checks if a number n>=2 is prime or not (base on the idea that we must check numbers from 2 to sqrt(n). if mod =o that means that the numbers is not prime if mod different from 0 that means that we must check next numbers (i don't know if this note is correct ))
sorry dude, it's a bit late but I've been offline for a month, you can contact me if there is still any question.
Join our real-time social learning platform and learn together with your friends!