I need help with an assignment. I have to modify the program below to determine the number that was entered the most times in a row and display the output. I can not use files or arrays. I am having trouble with the logic and need some guidance. Thanks.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

    float num, sum, total_num, total_pos;
    float total_neg, total_zero;
    char choice;
    
    do
    {
    
    //initialize counters
    sum=0;
    total_num=0;
    total_pos=0;
    total_neg=0;
    total_zero=0;
    
    cout<<"#################################################\n";
    cout<<"#                                               #\n";
    cout<<"# You will be asked to enter a number.          #\n";
    cout<<"# You may enter positive, negative or decimal   #\n";
    cout<<"# values including zero. The program will then  #\n";
    cout<<"# calculate the sum of the numbers and tabulate #\n";
    cout<<"# the types of numbers you entered.             #\n";
    cout<<"# When you are finished, enter 9999.            #\n";
    cout<<"#                                               #\n";
    cout<<"#################################################\n\n";
    cout<<"Please enter a number: ";
    cin>>num;
    
    while (num != 9999)
    { 
      if (num > 0)
      {  
             sum = sum + num;
             total_num++;
             total_pos++;
             cout<<"Enter another number. ";
             cin>>num;
      }
      
      else if (num < 0)
      {
           sum = sum + num;
           total_num++;
           total_neg++;
           cout<<"Enter another number. ";
           cin>>num;
      }
      
      else
      {
          total_num++;
          total_zero++;
          total_pos++;
          cout<<"Enter another number. ";
          cin>>num;
      }
    }
    
    //output of the data entered
    cout<<endl<<endl;
    cout<<"#################################################\n";
    cout<<"# Here is your breakdown:                       #\n";
    cout<<"#################################################\n";
    cout<<"#                                               #\n";
    cout<<"# Total numbers entered: "<<setw(22)<<total_num<<" #\n";
    cout<<"# Total positive numbers entered: "<<setw(13)<<total_pos<<" #\n";
    cout<<"# Total negative numbers entered: "<<setw(13)<<total_neg<<" #\n";
    cout<<"# Total zeros entered: "<<setw(24)<<total_zero<<" #\n";
    cout<<"# The sum of all numbers entered: "<<setw(13)<<sum<<" #\n";
    cout<<"#                                               #\n";
    cout<<"#################################################\n";
    
    //menu option to continue or end program
    cout<<"\nWould you like to enter more numbers? (Y/N) ";
    cin>>choice;
    }while (choice=='y' || choice=='Y');
    return 0;
    
}

Here is some code that I'm trying to use to isolate the counting/comparing process but it's not working out.

int main()
{
    int num, sum, count, count2, count_total;
    int row_count, row_total, num_row;
    
    sum=0;
    count=0;
    count2=0;
    num_row=0;
    row_count=0;
    
    cout<<"enter a number. ";
    cin>>num;
    while (num != 9999)
    {
          
          if (num == num)
             {
                  count = count++;
                  row_count = row_count++;
                  sum = sum + num;
                  num_row = num;
                  cout<<"enter a number. ";
                  cin>>num;
             }
          else
             {
                  row_total = row_count;
                  row_count = 0;
                  
                  
                  sum = sum + num;
                  count2 = count2++;
                  cout<<"enter a number. ";
                  cin>>num;
             }
    }
    
    count_total=count+count2;
    cout<<"sum = "<<sum<<endl;
    cout<<"count = "<<count_total<<endl;
    cout<<"row total = "<<row_total<<endl;
    
    system ("pause");
    return 0;
}

Recommended Answers

All 3 Replies

I need help with an assignment. I have to modify the program below to determine the number that was entered the most times in a row and display the output. I can not use files or arrays. I am having trouble with the logic and need some guidance. Thanks.

I would use 2 number groups. 1 would remember the max count and corresponding number, and 1 would remember the current count and number. Here's some qseudo code for this algorithm

def  doStuff():
    get input store in maxNum
    set maxCount = 1
    set currCount = 1
    set prevNum = maxNum
    while still getting input:
        get input, store in currNum
        if currNum == prevNum:
            currCount += 1
            if currCount > maxCount:
                maxCount = currCount
                maxNum = currNum
        else:
             currCount = 0
        prevNum = currNum
    return maxCount, maxNum

Thanks for the input. It helped out immensely. I took your ideas and fine tuned it. It now works like I wanted. The only drawback is when someone enters several numbers the same amount of times, for example 5 5 6 6 2 2 1 0, it will display 5 as being the number entered the most. What I should have it do is display 5, 6 and 2, but that may be outside of the scope for the assignment (I hope). Here is my latest revision of my code:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    //declare the variables
    double num, sum, total_num, total_pos;
    int total_neg, total_zero;
    double max_num, prev_num;
    int curr_count, max_count;
    char choice;
    
    do
    {
    
    //initialize counters
    sum=0;
    total_num=0;
    total_pos=0;
    total_neg=0;
    total_zero=0;
    
    //Display with instructions
    cout<<"#################################################\n";
    cout<<"#                                               #\n";
    cout<<"# You will be asked to enter a number.          #\n";
    cout<<"# You may enter positive, negative or decimal   #\n";
    cout<<"# values including zero. The program will then  #\n";
    cout<<"# calculate the sum of the numbers and tabulate #\n";
    cout<<"# the types of numbers you entered.             #\n";
    cout<<"# When you are finished, enter 9999.            #\n";
    cout<<"#                                               #\n";
    cout<<"#################################################\n\n";
    
    //Get input from user
    cout<<"Please enter a number: ";
    cin>>num;
    
    /*initialize counters & set variables for tracking 
    numbers entered consecutively and their value */
    max_num = num;
    max_count = 0;
    curr_count = 0;
    prev_num = max_num;
    
    //main section
    while (num != 9999)
    { 
      // Positive number sorting    
      if (num > 0)
      {  
             sum = sum + num;
             total_num++;
             total_pos++;
             
             //determines if numbers entered seqentially are the same
             if (num == prev_num)
             {
                  curr_count++;
          
          
                  if (curr_count > max_count)
                  {
                      max_count = curr_count;
                      max_num = num;
                      prev_num = num;
                      cout<<"Please enter a number: ";
                      cin>>num;
                  }
                  else
                  {
                      prev_num = num;
                      cout<<"Please enter a number: ";
                      cin>>num;
                  }
             }   
             else
             {
                 curr_count = 1;
                 prev_num = num;
                 cout<<"Please enter a number: ";
                 cin>>num;
             }
      }
      
      //Negative number sorting
      else if (num < 0)
      {
           sum = sum + num;
           total_num++;
           total_neg++;
           
           //determines if numbers entered seqentially are the same
           if (num == prev_num)
             {
                  curr_count++;
          
          
                  if (curr_count > max_count)
                  {
                      max_count = curr_count;
                      max_num = num;
                      prev_num = num;
                      cout<<"Please enter a number: ";
                      cin>>num;
                  }
                  else
                  {
                      prev_num = num;
                      cout<<"Please enter a number: ";
                      cin>>num;
                  }
             }   
             else
             {
                 curr_count = 1;
                 prev_num = num;
                 cout<<"Please enter a number: ";
                 cin>>num;
             }
      }
      
      //Zero sorting
      else
      {
          total_num++;
          total_zero++;
          total_pos++;
          
          //determines if numbers entered seqentially are the same
          if (num == prev_num)
             {
                  curr_count++;
          
          
                  if (curr_count > max_count)
                  {
                      max_count = curr_count;
                      max_num = num;
                      prev_num = num;
                      cout<<"Please enter a number: ";
                      cin>>num;
                  }
                  else
                  {
                      prev_num = num;
                      cout<<"Please enter a number: ";
                      cin>>num;
                  }
             }   
             else
             {
                 curr_count = 1;
                 prev_num = num;
                 cout<<"Please enter a number: ";
                 cin>>num;
             }
      }
    }
    
    //output of the data entered
    cout<<endl<<endl;
    cout<<"#################################################\n";
    cout<<"# Here is your breakdown:                       #\n";
    cout<<"#################################################\n";
    cout<<"#                                               #\n";
    cout<<"# Total numbers entered: "<<setw(22)<<total_num<<" #\n";
    cout<<"# Total positive numbers entered: "<<setw(13)<<total_pos<<" #\n";
    cout<<"# Total negative numbers entered: "<<setw(13)<<total_neg<<" #\n";
    cout<<"# Total zeros entered: "<<setw(24)<<total_zero<<" #\n";
    cout<<"# Number entered the most in a row: "<<setw(11)<<max_num<<" #\n";
    cout<<"# Number of times it was entered: "<<setw(13)<<max_count<<" #\n";
    cout<<"# The sum of all numbers entered: "<<setw(13)<<sum<<" #\n";
    cout<<"#                                               #\n";
    cout<<"#################################################\n";
    
    
    //menu option to continue or end program
    cout<<"\nWould you like to enter more numbers? (Y/N) ";
    cin>>choice;
    }while (choice=='y' || choice=='Y');
    return 0;
    
}

Thanks for the help.

Glad I could help!

If you needed to keep track of all numbers entered the maximum # of times, you would just use some sort of container to hold all of the numbers that met the qualification. Then, if a new number exceeded the maximum, you would purge the container and insert only the new maximum.

Good work!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.