repeating numbers

i dont know how to count repeated numbers

Page 1 of 1

12 Replies - 11547 Views - Last Post: 05 February 2010 - 03:26 AM Rate Topic: -----

#1 crx_turbo2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-February 10

repeating numbers

Posted 04 February 2010 - 05:24 PM

I'm trying to figure out how to make my program count repeated numbers in a row and what number it was that was typed in a row. Im just stumped, this is a sample of my code ive tried running it a few different ways other than this but i keep getting 0 for number repeated and total times repeated so I dont know what im missing.
Also im trying to do this without the use of arrays.



---------------------------------------------------------------------------------------------------------------------
// This program takes all numbers entered and displays total numbers entered,
// sum of all numbers, and shows how many positive and negative numbers that
// were entered total. Also displays the number that is repeated the most in 
// a row and how many times it was repeated in a row.

#include<iostream>
using namespace std;

int main()
{
    
    float num1;
    float tsum=0;
    float rnum=0;
    int pcount=0;
    int ncount=0;
    int numcount=0;
    int rcount=0;
    
    
    
    cout<< " Please enter as many numbers as you wish and press 9999 to stop,"<<endl;
    cout<< endl;
    do
    {
           cout<< " Please enter a number: ";
            cin >> num1,rnum;
           cout<<endl;
            if (num1 !=9999)
             numcount++;
            if (num1>= 0 && num1 != 9999)
             pcount++;
            if (num1< 0 && num1 != 9999)
             ncount++;
            if (num1 != 9999)
             tsum+=num1; 
            if (num1==rnum && num1 !=9999)
             rcount++;
                                     
             } while(num1 != 9999);
             cout<<endl;
    cout<< " The total numbers entered is "<<numcount<<endl;
    cout<<endl;
    cout<< " The total number of negative numbers entered is "<<ncount<<endl;
    cout<<endl;
    cout<< " The total number of positive numbers entered is "<<pcount<<endl;
    cout<<endl;
    cout<< " The amount of times a number was repeated in a row was "<< rcount <<endl;
    cout<<endl;
    cout<< " The number repeated the most in a row was "<< rnum <<endl;
    cout<<endl;
    cout<< " The sum of all numbers is "<<tsum<<endl;
    cout<<endl;    
             
             
system("pause");
return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: repeating numbers

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: repeating numbers

Posted 04 February 2010 - 05:34 PM

The problem with approaching this w/o any data structures is that you can't hold a record of what you've already entered.


Is this approach a "requirement"?
Was This Post Helpful? 0
  • +
  • -

#3 crx_turbo2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-February 10

Re: repeating numbers

Posted 04 February 2010 - 06:06 PM

well i was goin to use EX: int num1; num2; num3;...etc but that would make it really long because I would have to to repeat the loops over and over. But then the loops would need to restart after a different number was entered if it was not reapeted.

maybe

cout << " enter a number";
cin >> num1;
count <<" enter a number";
cin >> num2;
if (num1==num2)
{rnumcount++
cout<< "enter a number";
cin >>num3;
rnum=num3

if (num1==num3)
{rnumcount++;
count << enter a number..... and so on

but that would be me hoping that someone didnt repeat a number say, 3 times so i dont know how that would really work out...
Was This Post Helpful? 0
  • +
  • -

#4 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: repeating numbers

Posted 04 February 2010 - 06:39 PM

and when will this loop end? do you have any termination condition for the loop was it 9999?
in this case it is better to use a while loop.
have two variables num and num_old, while num is the current input and num_old is the previous output.
so as long as num and num_old are equal it means that a number was repeated.
int num, num_old;
	int countInRow = 0;
	cout<<"Enter numbers\n";
	cin>>num;
	num_old = num;
	while(num != 9999)
	{
		if(num == num_old){
			countInRow++;
			num_old =num;
		}
		else{
			cout<<num_old<<" repeated "<<countInRow<<" times\n";
			countInRow = 1;
		}
		num_old = num;
		cin>>num;
	}
    


This post has been edited by japanir: 04 February 2010 - 06:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 crx_turbo2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-February 10

Re: repeating numbers

Posted 04 February 2010 - 07:01 PM

ok so lets say i want to get to numbers but store 1 number into two differnent varible names
like
 cout<< "get numbers";
         cin>> num1; <---- put into 2 diff names
  


would you use rnum=num1;?

and also would i need to compare like (num1==rnum||num2==rnum)

because
do
    {
           cout<< " Please enter a number: ";
           cin >> num1;
           cout<<endl;
           cout<< " Please enter a number: ";
           cin >> num2;
            if (num1 !=9999 || num2 !=9999 )
             numcount++;
            if (num1>= 0 && num1 != 9999||num2>= 0 && num2 != 9999 )
             pcount++;
            if (num1< 0 && num1 != 9999||num2< 0 && num2 != 9999)
             ncount++;
            if (num1 != 9999)
             tsum+=num1; 
                                     
             } while(num1 != 9999)


is all one loop
Was This Post Helpful? 0
  • +
  • -

#6 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: repeating numbers

Posted 04 February 2010 - 07:17 PM

I am sorry i didnt understand the question.. what is rnum?
anyways, the code i posted is working. you can test it.
here it is again:
void main() {
int num, num_old; 
int countInRow = 0; 
cout<<"Enter numbers\n"; 
cin>>num; 
num_old = num; 
while(num != 9999) { 
if(num == num_old){ 
countInRow++; 
num_old =num; 
} else{ 
cout<<num_old<<" repeated "<<countInRow<<" times\n"; 
countInRow = 1; 
} 
num_old = num; 
cin>>num; 
}
}


Was This Post Helpful? 0
  • +
  • -

#7 crx_turbo2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-February 10

Re: repeating numbers

Posted 04 February 2010 - 07:38 PM

Yeah I don't know what I was asking either lol my mind is just going everywhere right now lol

rnum= repeated number(just my varible name)

And I understand what your code is doing but Ive got all this other code that needs to fit in it somewhere like this part

cout<< " Please enter a number: ";
           cin >> num1;
           cout<<endl;
            if (num1 !=9999)
             numcount++;
            if (num1>= 0 && num1 != 9999)
             pcount++;
            if (num1< 0 && num1 != 9999)
             ncount++;
            if (num1 != 9999)
             tsum+=num1; 
            
            rnum=num1;
            if (num1==rnum && num1!= 9999)
            rcount++;
            
            if (num1==9999)
            break;
            
            
           cout<< " Please enter a number: ";
           cin >> num2;
           cout<<endl;
            if (num2 != 9999)
             numcount++;
            if (num2 >= 0 && num2 != 9999 )
             pcount++;
            if (num2< 0 && num2 != 9999)
             ncount++;
            if (num2 !=9999)
             tsum+=num2;
             
            if(rnum==num2)
             rcount++;
            
                                     
             } while(num1 != 9999 && num2 != 9999);



And that works but only to a certain point. I finally got my counters in the program to go up so thats a good sign but I don't really know what to do to make my program differentiate between say a 4 and a 5.
like I will have four 5s entered and then a 4 but itll still count the 4 as a repeated number. :/
Was This Post Helpful? 0
  • +
  • -

#8 crx_turbo2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-February 10

Re: repeating numbers

Posted 04 February 2010 - 08:24 PM

ok right now my code is almost perfect but my repeatednumber count is off by 1 and idk what I need to add.

 // This program takes all numbers entered and displays total numbers entered,
// sum of all numbers, and shows how many positive and negative numbers that
// were entered total. Also displays the number that is repeated the most in 
// a row and how many times it was repeated in a row.

#include<iostream>
using namespace std;

int main()
{
    
    float num1;
    float num2;
    float tsum=0;
    float rnum=0;
    int pcount=0;
    int ncount=0;
    int numcount=0;
    int rcount=0;
    
    
    
    cout<< " Please enter as many numbers as you wish and press 9999 to stop,"<<endl;
    cout<< endl;
    do
    {
           cout<< " Please enter a number: ";
           cin >> num1;
           cout<<endl;
            if (num1 !=9999)
             numcount++;
            if (num1>= 0 && num1 != 9999)
             pcount++;
            if (num1< 0 && num1 != 9999)
             ncount++;
            if (num1 != 9999)
             tsum+=num1; 
            if (num1==rnum && num1!= 9999)
            rcount++;
            if (num1==9999)
            break;
            
            
           cout<< " Please enter a number: ";
           cin >> num2;
           cout<<endl;
            if (num2 != 9999)
             numcount++;
            if (num2 >= 0 && num2 != 9999 )
             pcount++;
            if (num2< 0 && num2 != 9999)
             ncount++;
            if (num2 !=9999)
             tsum+=num2;
            if(num1==num2)
             {rnum=num1;
             rcount++;
             }
            
                                     
             } while(num1 != 9999 && num2 != 9999);
             cout<<endl;
    cout<< " The total numbers entered is "<<numcount<<endl;
    cout<<endl;
    cout<< " The total number of negative numbers entered is "<<ncount<<endl;
    cout<<endl;
    cout<< " The total number of positive numbers entered is "<<pcount<<endl;
    cout<<endl;
    cout<< " The amount of times a number was repeated in a row was "<< rcount <<endl;
    cout<<endl;
    cout<< " The number repeated the most in a row was "<< rnum <<endl;
    cout<<endl;
    cout<< " The sum of all numbers is "<<tsum<<endl;
    cout<<endl;    
             
             
system("pause");
return 0;
}


Was This Post Helpful? 0
  • +
  • -

#9 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: repeating numbers

Posted 04 February 2010 - 08:42 PM

You're doing the same conditional over and over. Might I recommend you move the 9999 check to the beginning, then follow up with a cascading else if structure with the remaining things to check once you know the input is not 9999.
Was This Post Helpful? 0
  • +
  • -

#10 crx_turbo2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-February 10

Re: repeating numbers

Posted 04 February 2010 - 08:58 PM

yeah I see you point, that would looke about 10 times cleaner but, the problem im having is when my program finishes its counting (or was one less number than i was putting into the program) EX: i would put five 5s in a row but it will come out as 4 numbers put in a row.
Now its coming out 1 over the number i put in.
Was This Post Helpful? 0
  • +
  • -

#11 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: repeating numbers

Posted 04 February 2010 - 09:08 PM

Updated code listing, we aren't mind readers :)


Check your brackets.
Was This Post Helpful? 0
  • +
  • -

#12 crx_turbo2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-February 10

Re: repeating numbers

Posted 04 February 2010 - 09:29 PM

ok a more detailed explanation about whats not working

1. if i just enter a repeating number 5,5,5,5,5 the total times number repeated count will come out right
2. if i enter a number before repeating number 1,2,2,2,2, the total times number repeated count will be right
3. if i enter more than one number before repeating number 1,2,3,3,3,3, count will be off +1
4. if i enter a number after repeating number 3,3,3,3,5, count will be wrong +1

 do
    {
           cout<< " Please enter a number: ";
           cin >> num1;
           cout<<endl;
            if (num1==9999)
            break;
             numcount++;
            if (num1>= 0)
             pcount++;
            if (num1< 0)
             ncount++;
            if (num1!=9999)
             tsum+=num1; 
            if (num1==num1)
             rcount++;
            
            
            
           cout<< " Please enter a number: ";
           cin >> num2;
           cout<<endl;
           if (num2==9999)
           break;
             numcount++;
            if (num2 >= 0)
             pcount++;
            if (num2< 0)
             ncount++;
            tsum+=num2;
            if(num1==num2)
             {rnum=num2;
             rcount++;
             }
            
            } while(num1 != 9999 && num2 != 9999);


Was This Post Helpful? 0
  • +
  • -

#13 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: repeating numbers

Posted 05 February 2010 - 03:26 AM

you are doing to much checking.
you dont have to input num1 and num2.
only input num1, and assign num2 the value of num1 for in each iteration.
then you will have to check only if num1 is 9999.
always compare num1 and num2.
if they are equal, thatt's a repeat. so raise number of repeats by 1.
else,print the number of repeats, and initiate number of reapeats to 1.

just to be sure, are you tracking only one repeatition of numbers? the program should exit after a number was erpeate more than once, and a different value was inserted?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1