so i have an array of 300 numbers.
lets say like Tally [300];
i need to write a code which checks if any of the numbers of the arrays repeat it self and how many times does the number repeats it self.
so like if Tally [1] contains 33, and tally [4] and tally [50] and tally [70] all contain 33...
i will like to at the end have on the screen something like..33 came up 4 times or 33 = 4, or something like that...help please :)/> thanks in advance...
am not asking for the code to be written for me...but giving me an idea on how to go about it even in words and not in codes...but some guidance in words will be enough..thanks
You could do a loop to see weather they repeat,for example a for loop,start at Tally[0] if it contains 33 then print it on the screen and then move on to the next array and if the next array is equal to Tally[0] then print it on the screen and do that until all the arrays indices are checked.
you could build a map<int, int> mapping each value to the number of times it appears in the array (takes a single pass doing ++stats[value] for every value in the array, or simply convert the array directly into a multiset<int>, and then do a pass over the map/multiset printing the values whose counts are greater than one.
/* code by usandfriends */
#include <iostream>
#define RESET 0
main()
{
int howmany,i,n,j,count,counter,lowest,highest,tmp;
count=0;
lowest=0;
highest=0;
std::cout<<"How long is your list? ";
std::cin>>howmany;
std::cout<<std::endl<<std::endl;
int tally[howmany];
for(i=0;i<howmany;i++)
{
std::cout<<"Item number "<<i+1<<": ";
std::cin>>tally[i];
if(lowest>=tally[i])
{
lowest=tally[i];
}
if(highest<=tally[i])
{
highest=tally[i];
}
}
std::cout<<std::endl<<std::endl<<"We will now find out how many times values are repeated in your list."<<std::endl<<std::endl;
for(counter=0;counter<=howmany;counter++) //bubble sort
{
for(j=1;j<howmany;j++)
{
if(tally[j]>tally[j-1])
{;}
else
{
tmp=tally[j];
tally[j]=tally[j-1];
tally[j-1]=tmp;
}
}
}
for(i=0;i<howmany;i++) //count repeating array items
{
if(i!=0)
{
if(tally[i]!=tally[i-1])
{
count=RESET;
}
if(tally[i]!=tally[i+1])
{
count=count+1;
std::cout<<"Items with "<<tally[i]<<" value: "<<count<<std::endl;
}
}
for(n=lowest;n<=highest;n++)
{
if(tally[i]==n)
{
count++;
}
}
}
}
DO NOT USE THIS CODE. It is merely an example. This code is: too long(not simplified enough) and takes up a ton of memory. Please use this AS AN EXAMPLE.
If anyone has any suggestions to fix my code up, please feel free to comment. ;D