Question

C++, THIS IS FOR AN UNSORTED ARRAY.

I'm trying to implement an "if" statement that will check if there are 2 or more modes in a data set. I tried checking "if (count == total)" then it would increment "pos" by 1 and put the other mode in the list (m[pos-1] = values[index]), but it kept giving me the same number multiple times. For instance, if the data set was {1,5,10,4,7,3,8,3,9,10} (the 3 and 10 occur twice) then i would get somewhere between 3 and 6 "10's" in the output for my mode. (std::sort and algorithm are off limits.)

void calcMode (int valuesll, int listlength) int pos1: int mlposl/vill hold mode int count # 1. //starts count at 1 int total = 1: //holds highest number off occurences for (int index - 0: index < listlength; index++) //iterates list for (int í = 1: íくlistlength; ǐ++) //iterates through list to compare values(index) to each Iteration of values [1] if (values [index] - values[i])//checks if equal to count occurences count++: //increments count else if (count > total) total-count; n(pos-1] = values [index] ; if (count < total) continue count = 1; cout << Mode: ; for (int x- 0; x < pos: x++) cout << (m[pos-1]) <<

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include <iostream>
using namespace std;

int main()
{
int i, j, sum = 0, n , k = 0, c = 1, max = 0, mode;
  
cout<<"Enter the length\n";
cin>>n;
int a[n] , b[n]={0};
  
cout<<"Enter the set of numbers\n";
for (i = 0; i < n; i++)
{
cin>>a[i];
}
for (i = 0; i < n - 1; i++)
{
mode = 0;
for (j = i + 1; j < n; j++)
{
if (a[i] == a[j]) {
mode++;
}
}
  
if ((mode > max) && (mode != 0)) {
k = 0;
max = mode;
b[k] = a[i];
k++;
}
  
else if (mode == max) {
b[k] = a[i];
k++;
}
  
}
  
for (i = 0; i < n; i++)
{
if (a[i] == b[i])
c++;
}
if (c == n)
cout<<"\nThere is no mode";
else
{
cout<<"\nMode\t= ";
for (i = 0; i < k; i++)
cout<<b[i]<<" ";
}
cout<<"\n";
return 0;
}

//sample output

CAUsers\ IshuManish\ Documents nodes.exe Enter the length Enter the set of numbers 10 19 Mode-10 3 Process exited after 37.25 seconds with return value Press any key to continue - - -

Add a comment
Know the answer?
Add Answer to:
C++, THIS IS FOR AN UNSORTED ARRAY. I'm trying to implement an "if" statement that will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have written this function in C++. A string of integers is passed in as dString...

    I have written this function in C++. A string of integers is passed in as dString and an integer is passed in as count. each dString integer is then indexed and passed into strTolnt integer array. each strTolnt integer is then passed through an equation and stored in temp array. I have printed each index in the array and the correct values are displayed, but when I print the entire array the value is displayed as hex. temp array is...

  • howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE]...

    howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8};    int index;    index=0;    res = values[index];    for (int j=1; j<SIZE; j++)    {        if (values[j] > res)        {            res = values[j];            index = j;        cout << index << res << endl;        }    }    cout <<...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

  • What's wrong with my code? : I'm trying to use recursive functions to display and count...

    What's wrong with my code? : I'm trying to use recursive functions to display and count all the even numbers of an array. However, I can't seem get the program output the number of even integers . Here's the output I want: Here are the 5 even numbers: // Luckily, however, my code does output all the even numbers. But, I also want the program to tell me how may even integers there are. 2 8 14 18 22 MY...

  • I need to update this C++ code according to these instructions. The team name should be...

    I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...

  • (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the...

    (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the date of birth.            **fall.sort(compare_DOB); //Here lies your error** #include <fstream> #include <iostream> #include <string> #include <list> #include <cctype> using namespace std; const char THIS_TERM[] = "fall.txt"; const string HEADER = "\nWhat do you want to do ?\n\n" "\t. Add a new friend name and DOB ? \n" "\t. Edit/Erase a friend record ? \n" "\t. Sort a Friend's record ? \n"...

  • This is a C++ assignment that I'm trying to create and would like some help understanding...

    This is a C++ assignment that I'm trying to create and would like some help understanding while loops, with possible integration of for loops and if statements. This program only uses while, for, and if. I have some code that I have started, but where to go from there is what's giving me some trouble. This is involves a sentinel controlled while loop, and there are a lot of specifications below that I must have in the program. The program...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • This is a C++ assignment that I'm trying to create and would like some help understanding...

    This is a C++ assignment that I'm trying to create and would like some help understanding while loops, with possible integration of for loops and if statements. This program only uses while, for, and if. I have some code that I have started, but where to go from there is what's giving me some trouble. This is involves a sentinel controlled while loop, and there are a lot of specifications below that I must have in the program. The program...

  • MUST BE ANSWERED BY USING C++ Question 1 Unsorted List Implement a template class UnsortedList as...

    MUST BE ANSWERED BY USING C++ Question 1 Unsorted List Implement a template class UnsortedList as defined by the following skeleton: #define MAX_ITEMS 10 typedef char ItemType; class UnsortedList {        private:             int length; ItemType values[MAX_ITEMS]; int currentPos;        public:             SortedList( ); // default constructor: lenght=0, currentPos=-1             void MakeEmpty;    // let length=0             void InsertItem(ItemType x);   // insert x into the list                 void DeleteItem(ItemType x); // delete x from the list bool IsFull( );   // test...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT