Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Number of times number repeats

Reply
Thread Tools

Number of times number repeats

 
 
viuxrluxvbbc@mailinator.com
Guest
Posts: n/a
 
      12-06-2005
Hi im trying to write a program that will read in numbers and display
them in ascending order along with a count of how many times it
repeats. i got the numerical order portion done but cant figure out the
other part. i keep on getting a wrong number
please help
#include <iostream>
using namespace std;

int main()
{
int numbers[4] = {8,3,8,5};
int temp, counter, index, see, times, appears;
for (counter = 0; counter < 4; counter++)
{
for (index = 0; index < 4 - counter; index++)
{
if (numbers[index] > numbers[index + 1])
{
temp = numbers[index];
numbers[index] = numbers[index+1];
numbers[index+1] = temp;
}
}
}

for (int times = 0; times < 4 ; times++)
{
if (numbers[times] !=
appears++;
else
appears = 0;

}
cout << appears; //wrong

for (see = 0; see < 4; see++)
{
cout << numbers[see] << " ";
}

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      12-06-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi im trying to write a program that will read in numbers and display
> them in ascending order along with a count of how many times it
> repeats. i got the numerical order portion done but cant figure out the
> other part. i keep on getting a wrong number
> please help
> #include <iostream>
> using namespace std;
>
> int main()
> {
> int numbers[4] = {8,3,8,5};
> int temp, counter, index, see, times, appears;
> for (counter = 0; counter < 4; counter++)
> {
> for (index = 0; index < 4 - counter; index++)
> {
> if (numbers[index] > numbers[index + 1])
> {
> temp = numbers[index];
> numbers[index] = numbers[index+1];
> numbers[index+1] = temp;
> }
> }
> }
>
> for (int times = 0; times < 4 ; times++)
> {
> if (numbers[times] !=
> appears++;
> else
> appears = 0;
>
> }
> cout << appears; //wrong
>
> for (see = 0; see < 4; see++)
> {
> cout << numbers[see] << " ";
> }


You could use a std::map<int,unsigned int>. It would associate a number
(the key) with its count (the value). Consult your C++ text for more
info, try it out, and ask for help if you get stuck.

Cheers! --M

 
Reply With Quote
 
 
 
 
BobR
Guest
Posts: n/a
 
      12-06-2005

(E-Mail Removed) wrote in message
<(E-Mail Removed) .com>...
>Hi im trying to write a program that will read in numbers and display
>them in ascending order along with a count of how many times it
>repeats. i got the numerical order portion done but cant figure out the
>other part. i keep on getting a wrong number
>please help
>#include <iostream>
>using namespace std;
>
>int main(){
> int numbers[4] = {8,3,8,5};
> int temp, counter, index, see, times, appears;
> for(counter = 0; counter < 4; counter++){
> for(index = 0; index < 4 - counter; index++){
> if (numbers[index] > numbers[index + 1]){
> temp = numbers[index];
> numbers[index] = numbers[index+1];
> numbers[index+1] = temp;
> }
> }
> }
> for(int times = 0; times < 4 ; times++){
> if(numbers[times] != appears++;
> else appears = 0;
> }
> cout << appears; //wrong
> for(see = 0; see < 4; see++){ cout << numbers[see] << " ";}
>


#include <iostream>
#include <ostream>

// int viuxrluxvbbc_main(std:stream &cout){ // tested with
int main(){
using std::cout;
cout <<"___ viuxrluxvbbc_main() ___"<<std::endl;
int numbers[4] = {8,3,8,5};
int temp, appears;
for(int counter = 0; counter < 4; ++counter){
for(int index = 0; index < 4 - counter; ++index){
if(numbers[index] > numbers[index + 1]){ // swap
temp = numbers[index];
numbers[index] = numbers[index+1];
numbers[index+1] = temp;
}
} //for(index)
} //for(counter)
// -- PROBLEM -- [ note the comments! ]
for(int times = 0; times < 4 ; ++times){
if( numbers[times] != 8 ) ++appears; // inc if not equal to 8
else appears = 0; // otherwise, set 'appears' to zero.
} //for(times)
cout <<"appears="<< appears<<std::endl; //wrong
// -- PROBLEM end --

// -- PROBLEM fix --
appears = 0;
for(int times = 0; times < 4 ; ++times){
if( numbers[times] == 8 ) ++appears; // inc if equal to 8
} //for(times)
cout <<"appears="<< appears<<std::endl; //wrong
// -- PROBLEM fix end --

for(int see = 0; see < 4; ++see){ cout << numbers[see] << " ";} //for(see)
cout <<"___ viuxrluxvbbc_main() end ___"<<std::endl;
return 0; // always return 0, EXIT_SUCCESS or EXIT_FAILURE.
} //main() end
// ---------------------
// - output -
___ viuxrluxvbbc_main() ___
appears=0
appears=2
3 5 8 8
___ viuxrluxvbbc_main() end ___


--
Bob R
POVrookie


 
Reply With Quote
 
viuxrluxvbbc@mailinator.com
Guest
Posts: n/a
 
      12-07-2005
well i got it to work. i need some help with the output tho.
say if a number repeats, how can i get it to display only once but
still display a correct repetition count
right now its outputting something like this:

Number Count
8 3
8 3
8 3
1 1

but i wanna fix so it displays this:
Number Count
8 3
1 1

heres the code

#include <iostream>
using namespace std;

const int size = 7;

int main()
{
int numbers[size] = {1,6,8,5,4,5,6};
int temp, counter, index, numorder, times, appears = 0, me;
cout << "This program will read in numbers and display them in
ascending order." << endl;
cout << "It will also count the number of times a number is
repeated." << endl;
cout << endl;
for (counter = 0; counter < 7; counter++)
{
for (index = 0; index < size - counter; index++)
{
if (numbers[index] > numbers[index + 1])
{
temp = numbers[index];
numbers[index] = numbers[index+1];
numbers[index+1] = temp;
}
}
}

for (numorder = 0; numorder < size; numorder++)
{
cout << numbers[numorder] << " ";
}
cout << endl;
cout << "\nNumber Count" << endl;

for (int times = 0; times < size ; times++)
{
appears = 0;
for (int me = size - 1 ; me >= 0; me--)
{
if (numbers[times] == numbers [me] )
{
appears++;
}

}
cout << numbers[times] << " " <<
appears << endl;
}

cin.get();cin.get();
return 0;
}

 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      12-07-2005
On 2005-12-07, (E-Mail Removed) <(E-Mail Removed)> wrote:
> well i got it to work. i need some help with the output tho.
> say if a number repeats, how can i get it to display only once
> but still display a correct repetition count right now its
> outputting something like this:
>
> Number Count
> 8 3
> 8 3
> 8 3
> 1 1
>
> but i wanna fix so it displays this:
> Number Count
> 8 3
> 1 1
>
> heres the code
>
> #include <iostream>
> using namespace std;
>
> const int size = 7;
>
> int main()
> {
> int numbers[size] = {1,6,8,5,4,5,6};
> int temp, counter, index, numorder, times, appears = 0, me;
> cout << "This program will read in numbers and display them in
> ascending order." << endl;
> cout << "It will also count the number of times a number is
> repeated." << endl;
> cout << endl;
> for (counter = 0; counter < 7; counter++)
> {
> for (index = 0; index < size - counter; index++)
> {
> if (numbers[index] > numbers[index + 1])
> {
> temp = numbers[index];
> numbers[index] = numbers[index+1];
> numbers[index+1] = temp;
> }
> }
> }
>
> for (numorder = 0; numorder < size; numorder++)
> {
> cout << numbers[numorder] << " ";
> }
> cout << endl;
> cout << "\nNumber Count" << endl;
>
> for (int times = 0; times < size ; times++)
> {
> appears = 0;
> for (int me = size - 1 ; me >= 0; me--)
> {
> if (numbers[times] == numbers [me] )
> {
> appears++;
> }
>
> }
> cout << numbers[times] << " " <<
> appears << endl;
> }


You are counting the total appearances every time a number
appears. You only need to count once for each number. It will
save time, and produce the index you need, to count from
position [times] instead of from the back of the array.

For example, assuming my list is 1, 1, 4, 5, 5:

I see a one, so I count from there to the end of the ones. I
print the output. 1, 2. I then start looking past the end of the
ones, at the four. And so on.

In psuedocode:

set i to 0.
loop1: if i is not less than size, break from loop1.
set j to i.
loop2: if j is not less than size, or
numbers[i] <> number[j], break from loop2.
increment j by one.
Loop.
print numbers[i], (j-i+1).
set i to j+1;

It translates into two or three lines of C++ code.

For fun, it can be solved with std::equal_range.

std:air<const int*, const int*> p(0, numbers);
while (p.second != numbers+size) {
p = std::equal_range(p.second, numbers+size, *p.second);
std::cout << *p.first << ": " << p.second-p.first << '\n';
}

--
Neil Cerutti
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HTTP_ACCEPT_LANGUAGE; repeats and repeats value; ie recent update? cate Javascript 1 06-14-2010 03:30 PM
Tab repeats function 6 times Thiago Jorge Javascript 1 10-12-2006 03:23 AM
ASP.NET 2.0 - browser refresh repeats operation Amelyan ASP .Net 2 12-19-2005 02:45 AM
Newbee : Can a Repeater repeats ascx control ? Mick ASP .Net 1 11-29-2004 01:41 PM
Refresh repeats last action kj12345 ASP .Net 3 09-29-2004 07:57 AM



Advertisments