Error when trying to Run the code, Range_Error(?) - codeblocks

I'm having this problem, my program compiles normally but when I try to run it the error
"terminate called after throwing an instance of "Range_error"
what(): Range error:0
This application has requested the Runtime to terminate it in an unusual way."
comes up. My code is the following, I'm using windows 8 and coding in CodeBlocks 13.12 with GNU GCC compiler.
> #include <iostream> #include <vector> #include <string> #include "std_lib_facilities.h"
>
> using namespace std;
>
> class Name_pairs{
> vector<string> name;
> vector<double> age;
>
> public:
> void read_names()
> {
> string control="yes";
> int i=0;
> while(control!="no")
> {
> cout<<"Insert a name "<<endl;
> cin>>name[i];
> cout<<"Do you want to introduce another name?"<<endl;
> cin>>control;
> }
> }
> void read_ages()
> {
> for(int i=0; i<=name.size();i++)
> {
> cout<<"introduce an age for "<<name[i]<<endl;
> cin>>age[i];
> }
> }
> void print()
> {
> for(int i=0;i<=name.size();i++)
> {
> cout<<name[i]<<", "<<age[i]<<endl;
> }
> }
> void Sort()
> {
> string tempn;
> double tempa;
> for(int i=0; i<name.size();i++)
> {
> for(int j=i+1;j<name.size();j++)
> {
> if(name[i]>name[j])
> {
> tempn=name[i];
> tempa=age[i];
> name[i]=name[j];
> age[i]=age[j];
> name[j]=tempn;
> age[j]=tempa;
> }
> }
> }
> }
> }; int main() {
> Name_pairs List;
> List.read_names();
> List.read_ages();
> List.Sort();
> List.print();
> return 0;
> }
Please if anyone could help :D Thanks in advance.

cin>>name[i];
You are assigning element i of vector name while name is empty
and has no elements. You can only assign somevector[i] if such an element exists. Do
string s;
cin >> s;
name.push_back(s);
to add a new element at the end.
(And BTW, you never increment i in read_names, so every the name
would be assigned to name[0], overwriting the previous name, even
if element name[0] existed.)
Also, loops like this:
for(int i=0; i<=name.size();i++)
Are advancing i the Nth element of a vector that is N elements long.
The index of the last element is N - 1, so they overshoot the
vector. Should be:
for(int i=0; i < name.size();i++)
as you have in your Sort()

Related

Need to find highest non repeating number in custom vector

I'm creating a program, where you input n amount of mushroom pickers, they are in a shroom picking contest, they can find shroomA (worth 5 points), shroomB (worth 3 points) and shroomC (worth 15 points). I need to find the contest winner and print his/her name, but if two or more contestants have the same amount of points they are disqualified, meaning I need to find the highest non repeating result.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class ShroomPicker {
private:
string name;
long long int shroomA, shroomB, shroomC;
public:
void Input() {
char Name[100];
long long int shrooma, shroomb, shroomc;
cin >> Name >> shrooma >> shroomb >> shroomc;
name = Name;
shroomA = shrooma; shroomB = shroomb; shroomC = shroomc;
}
long long int calcPoints() {
return shroomA * 5 + shroomB * 3 + shroomC * 15;
}
string winnersName() {
return name;
}
};
int main() {
int n;
cin >> n;
vector<ShroomPicker> shr;
for (int i = 0; i < n; i++) {
ShroomPicker s;
s.Input();
shr.push_back(s);
}
long long int hiscore = 0;
int num = 0;
for (int i = 0; i < n; i++) {
long long int temp = 0;
temp = shr[i].calcPoints();
if (temp > hiscore) {
hiscore = temp;
num = i;
}
}
cout << shr[num].winnersName();
}
I made this program which finds the highest score even if repeats more than once, could someone suggest how I can find the highest non repeating score?
edit:
for (int i = 0; i < n; i++) {
long long int temp = 0;
temp = shr[i].calcPoints();
if (scoreMap.find(temp) == scoreMap.end()) {
scoreMap[temp] = Info{ i, false };
}
else {
scoreMap[temp] = Info{ i, true };
}
}
I would suggest sorting the list of participants in decreasing number of mushrooms picked (O[nlogn]) and then look through the list from start to finish (O[n] max). The first participant whose number of mushrooms picked is different than those of the adjacent participants (in the sorted list) is the winner.
The fastest (O(N)) way I can think of is to have:
struct Info
{
int picker_index;
bool disqualified;
}
// map from score to the Info object above
std::unordered_map<int, Info> scoreMap;
Iterate through pickers and update the map as follows:
-- If no item in the map, just add scoreMap[score] = Info {picker_index, false};
-- else, set disqualified = true on the existing item;
Once the map is constructed, find the max key in the map for which disqualified = false; similar to what you are doing now.

How to use std::unordered_set to a vector<vector<int>> to remove duplicates

I have a std::vector< std::vector< int> > that contains prime numbers. I wish to eliminate double entries, as the vector contents will be exported to a file and should only contain unique primes.
The prime numbers will come from certain intervals, fed to my algorithm through a file, e.g. (32,345). There may be cases where the intervals will overlap e.g. (54,434),(345,596). I wish to use std::unordered_set, since it will be the most efficient for my case.
I have seen examples with a std::vector< int> and tried to accommodate that to my needs, to no avail.
Here is code I tried, inspired by the print method of a 2d vector.
std::vector<std::vector<int> > sharedAnswersVec;
...
...
std::unordered_set<int> unique_primes;
for (auto i = 0; i < sharedAnswerVec.size(); i++)
{
for (auto j = 0; j < sharedAnswerVec[j].size(); j++)
{
unique_primes.insert({ i,j });
sharedAnswerVec.assign(unique_primes.begin(), unique_primes.end());
sort(sharedAnswerVec.begin(), sharedAnswerVec.end());
}
}
sharedAnswerVec cannot be std::vector< int>, has to be std::vector< std::vector< int> >.
With that in mind, what changes must I do in order to correct it?
I am a beginner trying to learn, so if the question seems silly to you, please bear that in mind.
You are trying to insert something into your std::unordered_set<int> something which is not an int.
It is not clear to me what your "intervals" are, or what you are doing when sorting. Perhaps this is some use?
#include <iostream>
#include <vector>
#include <set>
int main()
{
std::vector<std::vector<int> > sharedAnswersVec = {
{1,2,3,5,7,11,13,17,19,23,29},
{2,5,11,17,23,29}
};
std::set<int> unique_primes;
for ( const auto& v : sharedAnswersVec )
{
for ( auto i : v )
{
unique_primes.insert(i);
}
}
for ( auto i : unique_primes )
{
std::cout << i << std::endl;
}
return 0;
}

Recursive algorithm to find all possible solutions in a nonogram row

I am trying to write a simple nonogram solver, in a kind of bruteforce way, but I am stuck on a relatively easy task. Let's say I have a row with clues [2,3] that has a length of 10
so the solutions are:
$$-$$$----
$$--$$$---
$$---$$$--
$$----$$$-
$$-----$$$
-$$----$$$
--$$---$$$
---$$--$$$
----$$-$$$
-$$---$$$-
--$$-$$$--
I want to find all the possible solutions for a row
I know that I have to consider each block separately, and each block will have an availible space of n-(sum of remaining blocks length + number of remaining blocks) but I do not know how to progress from here
Well, this question already have a good answer, so think of this one more as an advertisement of python's prowess.
def place(blocks,total):
if not blocks: return ["-"*total]
if blocks[0]>total: return []
starts = total-blocks[0] #starts = 2 means possible starting indexes are [0,1,2]
if len(blocks)==1: #this is special case
return [("-"*i+"$"*blocks[0]+"-"*(starts-i)) for i in range(starts+1)]
ans = []
for i in range(total-blocks[0]): #append current solutions
for sol in place(blocks[1:],starts-i-1): #with all possible other solutiona
ans.append("-"*i+"$"*blocks[0]+"-"+sol)
return ans
To test it:
for i in place([2,3,2],12):
print(i)
Which produces output like:
$$-$$$-$$---
$$-$$$--$$--
$$-$$$---$$-
$$-$$$----$$
$$--$$$-$$--
$$--$$$--$$-
$$--$$$---$$
$$---$$$-$$-
$$---$$$--$$
$$----$$$-$$
-$$-$$$-$$--
-$$-$$$--$$-
-$$-$$$---$$
-$$--$$$-$$-
-$$--$$$--$$
-$$---$$$-$$
--$$-$$$-$$-
--$$-$$$--$$
--$$--$$$-$$
---$$-$$$-$$
This is what i got:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
typedef std::vector<bool> tRow;
void printRow(tRow row){
for (bool i : row){
std::cout << ((i) ? '$' : '-');
}
std::cout << std::endl;
}
int requiredCells(const std::vector<int> nums){
int sum = 0;
for (int i : nums){
sum += (i + 1); // The number + the at-least-one-cell gap at is right
}
return (sum == 0) ? 0 : sum - 1; // The right-most number don't need any gap
}
bool appendRow(tRow init, const std::vector<int> pendingNums, unsigned int rowSize, std::vector<tRow> &comb){
if (pendingNums.size() <= 0){
comb.push_back(init);
return false;
}
int cellsRequired = requiredCells(pendingNums);
if (cellsRequired > rowSize){
return false; // There are no combinations
}
tRow prefix;
int gapSize = 0;
std::vector<int> pNumsAux = pendingNums;
pNumsAux.erase(pNumsAux.begin());
unsigned int space = rowSize;
while ((gapSize + cellsRequired) <= rowSize){
space = rowSize;
space -= gapSize;
prefix.clear();
prefix = init;
for (int i = 0; i < gapSize; ++i){
prefix.push_back(false);
}
for (int i = 0; i < pendingNums[0]; ++i){
prefix.push_back(true);
space--;
}
if (space > 0){
prefix.push_back(false);
space--;
}
appendRow(prefix, pNumsAux, space, comb);
++gapSize;
}
return true;
}
std::vector<tRow> getCombinations(const std::vector<int> row, unsigned int rowSize) {
std::vector<tRow> comb;
tRow init;
appendRow(init, row, rowSize, comb);
return comb;
}
int main(){
std::vector<int> row = { 2, 3 };
auto ret = getCombinations(row, 10);
for (tRow r : ret){
while (r.size() < 10)
r.push_back(false);
printRow(r);
}
return 0;
}
And my output is:
$$-$$$----
$$--$$$---
$$---$$$--
$$----$$$--
$$-----$$$
-$$-$$$----
-$$--$$$--
-$$---$$$-
-$$----$$$-
--$$-$$$--
--$$--$$$-
--$$---$$$
---$$-$$$-
---$$--$$$
----$$-$$$
For sure, this must be absolutely improvable.
Note: i did't test it more than already written case
Hope it works for you

Debug Assertion Failed - MSVCP110D.dll

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string token = "000000:::AAAA:::000011:::Hello 8:::::::D Jay!";
string * stringArray = new string[token.size()];
string interim;
int r = 0;
int arrayCounter = 0;
for(int x = 0; x < token.length(); x++)
{
if(token[x] != ':')
{
interim[r] = token[x];
r++;
}
}
for (int x = 0; x < r; x++)
{
cout << interim[x] << endl;
}
system("pause");
return 0;
}
I am new and learning, and have narrowed it down to the line:
interim[r] = token[x];
..But i don't know why it crashes. Advice? I am coding in Visual C++ VSE2012
The string interim has a size of zero. Setting interim[r] = token[x] modifies the string at location r without changing its size. With a size of zero this is undefined behavior.
interim += token[x] is probably what you want.
Example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string token = "000000:::AAAA:::000011:::Hello 8:::::::D Jay!";
string interim;
for(int x = 0; x < token.length(); x++)
{
if(token[x] != ':')
{
interim += token[x];
}
}
cout << interim << endl;
system("pause");
return 0;
}
Output:
000000AAAA000011Hello 8D Jay!
Press any key to continue . . .

N nested for-loops

I need to create N nested loops to print all combinations of a binary sequence of length N. Im not sure how to do this.
Any help would be greatly appreciated. Thanks.
Use recursion. e.g., in Java
public class Foo {
public static void main(String[] args) {
new Foo().printCombo("", 5);
}
void printCombo(String soFar, int len) {
if (len == 1) {
System.out.println(soFar+"0");
System.out.println(soFar+"1");
}
else {
printCombo(soFar+"0", len-1);
printCombo(soFar+"1", len-1);
}
}
}
will print
00000
00001
00010
...
11101
11110
11111
You have two options here:
Use backtracking instead.
Write a program that generates a dynamic program with N loops and then executes it.
You don't need any nested loops for this. You need one recursive function to print a binary value of length N and a for loop to iterate over all numbers [0 .. (2^N)-1].
user949300's solution is also very good, but it might not work in all languages.
Here's my solution(s), the recursive one is approximately twice as slow as the iterative one:
#include <stdio.h>
#ifdef RECURSIVE
void print_bin(int num, int len)
{
if(len == 0)
{
printf("\n");
return;
}
print_bin(num >> 1, len -1);
putchar((num & 1) + '0');
}
#else
void print_bin(int num, int len)
{
char str[len+1];
int i;
str[len] = '\0';
for (i = 0; i < len; i++)
{
str[len-1-i] = !!(num & (1 << i)) + '0';
}
printf("%s\n", str);
}
#endif
int main()
{
int len = 24;
int i;
int end = 1 << len;
for (i = 0; i < end ; i++)
{
print_bin(i, len);
}
return 0;
}
(I tried this myself on a Mac printing all binary numbers of length 24 and the terminal froze. But that is probably a poor terminal implementation. :-)
$ gcc -O3 binary.c ; time ./a.out > /dev/null ; gcc -O3 -DRECURSIVE binary.c ; time ./a.out > /dev/null
real 0m1.875s
user 0m1.859s
sys 0m0.008s
real 0m3.327s
user 0m3.310s
sys 0m0.010s
I don't think we need recursion or n nested for-loops to solve this problem. It would be easy to handle this using bit manipulation.
In C++, as an example:
for(int i=0;i<(1<<n);i++)
{
for(int j=0;j<n;j++)
if(i&(1<<j))
printf("1");
else
printf("0");
printf("\n");
}

Resources