By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,880 Members | 2,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,880 IT Pros & Developers. It's quick & easy.

Number of times number repeats

P: n/a
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] !=8)
appears++;
else
appears = 0;

}
cout << appears; //wrong

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

Dec 6 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
vi**********@mailinator.com 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] !=8)
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

Dec 6 '05 #2

P: n/a

vi**********@mailinator.com wrote in message
<11**********************@z14g2000cwz.googlegroups .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] !=8) appears++;
else appears = 0;
}
cout << appears; //wrong
for(see = 0; see < 4; see++){ cout << numbers[see] << " ";}


#include <iostream>
#include <ostream>

// int viuxrluxvbbc_main(std::ostream &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
Dec 6 '05 #3

P: n/a
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;
}

Dec 7 '05 #4

P: n/a
On 2005-12-07, vi**********@mailinator.com <vi**********@mailinator.com> 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::pair<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
Dec 7 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.