473,804 Members | 3,228 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Number of times number repeats

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
4 5021
vi**********@ma ilinator.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,un signed 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

vi**********@ma ilinator.com wrote in message
<11************ **********@z14g 2000cwz.googleg roups.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_ma in(std::ostream &cout){ // tested with
int main(){
using std::cout;
cout <<"___ viuxrluxvbbc_ma in() ___"<<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::e ndl; //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::e ndl; //wrong
// -- PROBLEM fix end --

for(int see = 0; see < 4; ++see){ cout << numbers[see] << " ";} //for(see)
cout <<"___ viuxrluxvbbc_ma in() end ___"<<std::endl ;
return 0; // always return 0, EXIT_SUCCESS or EXIT_FAILURE.
} //main() end
// ---------------------
// - output -
___ viuxrluxvbbc_ma in() ___
appears=0
appears=2
3 5 8 8
___ viuxrluxvbbc_ma in() end ___
--
Bob R
POVrookie
Dec 6 '05 #3
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.g et();
return 0;
}

Dec 7 '05 #4
On 2005-12-07, vi**********@ma ilinator.com <vi**********@m ailinator.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_rang e.

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

--
Neil Cerutti
Dec 7 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
5044
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
6
14679
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed as many times that there are
5
1793
by: cpptutor2000 | last post by:
Could some C++ guru please help me with this problem? Suppose I have a string representation of a very large number as: char *strNum = "1234"; now suppose I want to store this number with each digit as an element of a large array: unsigned int Num; Now I want to transfer them digit by digit to the unsigned int array, with code as the following, but it does NOT work.
15
8072
by: Stormkid | last post by:
Hey Gang, I'm trying to figure out the best way to add two times together of the format hh:mm:ss any suggestions would be great thanks Todd
19
4457
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an array 4) Figure out if the original number is evenly divisible by any of the numbers in the array.
3
2663
by: Nagarjuna502 | last post by:
Hai, I am having 3 questions, 1.How To find harddisk number in C# 2.How To find CPU number in C# 3.How to find IP address of the system in C# where we are working.
3
3456
by: raghunath1981 | last post by:
Hi All, Could you please help me with writing a sql for counting number of times a varchar (1000) repeated in the table. I think I cannot use groupby/distinct etc. Please give me an idea how I could do that if I want the output such that each row is the varchar (distinct) and number times it repeated. I was trying several things but it did not work :( select tab1.varchar_col , tab2.CNT from tab tab1 ( select count(*) from tab group...
3
2277
by: ambi21hs | last post by:
i need a validation code in c for employee number, only number should be allowed can any one help me out with dis..
3
2201
by: Richa Chhabra | last post by:
Is there any validation on character case as well in C++
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.