Number of times number repeats

V

viuxrluxvbbc

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] << " ";
}
 
M

mlimber

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
 
B

BobR

(e-mail address removed) wrote in message
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::eek: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 ___
 
V

viuxrluxvbbc

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;
}
 
N

Neil Cerutti

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 <> number[j], break from loop2.
increment j by one.
Loop.
print numbers, (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';
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top