Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6917163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:41:44+00:00 2026-05-27T09:41:44+00:00

I am using the following code to compute the mode #include <iosfwd> #include <vector>

  • 0

I am using the following code to compute the mode

#include <iosfwd>
#include <vector>
#include <iostream>

int prices[]={1,1,2,2,2,3,3,3};

int main()
{
  double currval=prices[0];
  int ctr=1;
  int maxctr=1;
  double modval=prices[0];
  for (int i=1;i<8;i++) {
    if(prices[i]==currval) {
      ++ctr;
    }else {
      if(ctr>maxctr) {
    maxctr=ctr;
    modval=currval;
      }
      currval=prices[i];
      ctr=1;
    }
  }
 std::cout<<"mode is: "<<modval<<std::endl;
  return 0;
}

but unfortunately, this returns the first mode.

mode is: 2

I want to continue to pass over the array and capture all the modes in another array, so then I can select the max or min or average of the array of modes. So, in my example, I would have an array with content {2,3}.
any suggestions on how I do that.
thanks!
EDITED:

#include <iosfwd>
#include <vector>
#include <iostream>

int prices[]={1,1,2,2,2,3,3,3};
std::vector<int> result;

int main()
{
  double currval=prices[0];
  int ctr=1;
  int maxctr=1;
  double modval=prices[0];
  for (int i=1;i<8;i++) {
    if(prices[i]==currval) {
      ++ctr;
    }else {
      if(ctr>maxctr) {
    maxctr=ctr;
    result.clear();
    result.push_back(currval);
      } else {
    if (ctr==maxctr) {
      result.push_back(currval);
    }
      }
      currval=prices[i];
      ctr=1;
    }
  }
 if(ctr>maxctr) {
    maxctr=ctr;
    result.clear();
    result.push_back(currval);
      } else {
    if (ctr==maxctr) {
      result.push_back(currval);
    }
      }

  return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:41:45+00:00Added an answer on May 27, 2026 at 9:41 am

    Your code’s part after else was wrong, as it will get executed after both if and else which is not what wanted.

    #include <iosfwd>
    #include <vector>
    #include <iostream>
    
    int prices[]={1,1,2,2,2,3,3,3};
    
    using namespace std;
    
    int main()
    {
        int ctr=1;
        int maxctr=1;
        vector<int> allModes;
        int max, min;
    
        for (int i=0; i<8; i++) {
    
            if(prices[i+1]==prices[i]) {
                ++ctr;
            }
            else {
                if(ctr == maxctr) {
                    allModes.push_back(prices[i]);  //if it is same as previous mode add it to vector
                    //max = prices[i];
                }
                if(ctr > maxctr) {
                    allModes.clear();           //if it is greater than previous mode then previous number is not a mode now
                                                //so clear old vector
                    allModes.push_back(prices[i]);  
                    maxctr = ctr;
                    //max = prices[i];
                    //min = prices[i];
                }
                ctr=1;
            }
        }
    
    
        std::cout<<"Mode: ";
        for (  vector<int>::iterator it=allModes.begin() ; it < allModes.end(); ++it ) {
            std::cout<< " " << *it;
        }
        return 0;
    }
    

    Assuming array is in ascending order.

    But if you want to store min or max or avg you can do this without using vector.

    1. For min while transverse don’t change mode if it has same count as old one

    2. For max you will assign new value for same count

    3. For avg, just add all numbers but keep track of how many number added. then at end of loop just take avg = sum / no. of element

    So in all three cases, vector isn’t required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I open a pop-up window using following code in main.html function openwindow(url) { window.open(url,
I am using the following code to compute execution time in milli-secs. struct timespec
I am using following code to compute MD5SUM of a file - byte[] b
I am using the following Python code to compute the User+Sys time. t =
I am using following code in rowdatabound function. Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object,
I am using following code to check whether a check box on my website
I am using following code to design my home page. The output (as shown
I'm using following code but cannot return data from MySQL. This is the output:
I am setting the row height of my UITableView using following code [tableView setRowHeight:
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.