Answer the question
In order to leave comments, you need to log in
range_error error, can this loop be fixed?
I'm doing an exercise from Mr. Stroustrup's book, I'm stuck on this task (I'm a beginner). It is necessary to output the number that repeats in the sequence the greatest number of times (mode).
I made a loop that compares in order of vector numbers. It compares them until the number above is greater than the current one (since I used the sort() function). The process must then be repeated. Comparing the moda_loop variable and moda_loop_2 I find the largest mode of the vector.
At what part of the cycle should I correct my mistake?
moda_loop = 0;
moda_loop_2 = 0;
sort(i);
for(zum=0; zum<i.size(); zum++){
for(zumzum=1; zumzum<i.size(); zumzum++){
if(i[zum]==i[zumzum]){
moda_min=i[zum];
moda_max=i[zum];
if(moda_min==i[zumzum+1]){
while(moda==i[b]){
moda_min=i[zumzum+1];
b=zumzum+1;
b++;
moda_loop++;
}
}
if(moda_min<i[zumzum+1]){
moda_min=i[zumzum+1];
if(moda_min==i[zumzum+1]){
while(moda_min==i[b]){
moda_max=i[zumzum+1];
c=zumzum+1;
c++;
moda_loop_2++;
}
}
}
}
}
}
if(moda_loop>moda_loop_2){
moda_min=moda;
}
if(moda_loop<moda_loop_2){
moda_max=moda;
}
#include <D:\C\std_lib_facilities.h>
int main()
{
int num, zum, zumzum;
vector<int> i;
int c, moda, b, moda_loop, moda_loop_2, moda_min, moda_max;
int max, min;
int num_core;
for(int num_core; cin >> num_core; )
i.push_back(num_core);
min = i[0];
max = i[0];
for(num = 1; num<i.size(); num++){
if(max>i[num]) max = i[num];
if(min<i[num]) min = i[num];
}
moda_loop = 0;
moda_loop_2 = 0;
sort(i);
for(zum=0; zum<i.size(); zum++){
for(zumzum=1; zumzum<i.size(); zumzum++){
if(i[zum]==i[zumzum]){
moda_min=i[zum];
moda_max=i[zum];
if(moda_min==i[zumzum+1]){
while(moda==i[b]){
moda_min=i[zumzum+1];
b=zumzum+1;
b++;
moda_loop++;
}
}
if(moda_min<i[zumzum+1]){
moda_min=i[zumzum+1];
if(moda_min==i[zumzum+1]){
while(moda_min==i[b]){
moda_max=i[zumzum+1];
c=zumzum+1;
c++;
moda_loop_2++;
}
}
}
}
}
}
if(moda_loop>moda_loop_2){
moda_min=moda;
}
if(moda_loop<moda_loop_2){
moda_max=moda;
}
cout << "MAX: " << min << '\n';
cout << "MIN: " << max << '\n';
cout << "MODA: " << moda;
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question