The following is the start of my C++ code. I'd like to be able to type in 5 inputs and get them stored in the vector. However the code breaks on the double array element access. How can I fix this and why is it breaking? The error I'm getting on my IDE (CLion) is "could not find operator[]".
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::setprecision;
using std::sort;
using std::streamsize;
using std::string;
using std::vector;
int main () {
vector<int> inputVec;
int x;
while (cin >> x && inputVec.size() < 5) {
inputVec.push_back(x);
cout << inputVec.size() << endl;
}
sort(inputVec.begin(), inputVec.end());
cout << "before if 1" << endl;
typedef vector<double>::size_type vec_sz;
vec_sz size = inputVec.size();
cout << "before if 3" << endl;
if (size < 4) {
cout << "too few inputs";
} else {
cout << "calculating quartiles" << endl;
int quartileSize = size / 4;
int leftOvers = size - quartileSize * 4;
int count = 0;
vector<vector<int>> quartiles(4);
for (int i = 0; i < 3; i++) {
for (int j = 0; i < quartileSize; j++) {
quartiles[i][j] = inputVec[count++];
}
}
Related
The array I have is
int age[5] = {11,2,23,4,15}
but it does not print out 11,2,23,4, 15.
#include <iostream>
#include <array>
using namespace std;
int main() {
int age[5] = {11,2,23,4,15};
cout << age[5] << endl;
}
The name of the array is not age[5].
The name of the array is age.
The expression age[5] represents the array's sixth element, which does not exist.
In fact, there is no built-in logic for printing a whole array in a formatted manner, so even cout << age << endl is not correct.
If you want to print the array, do it element-by-element in a loop.
Arrays are 0-indexed. In your example, the valid indexes are 0..4. You are trying to print a single int from age[5], which is out of bounds.
You need to loop through the indexes of the array, eg:
#include <iostream>
using namespace std;
int main() {
int age[5] = {11,2,23,4,15};
for(int i = 0; i < 5; ++i) {
cout << age[i] << " ";
}
cout << endl;
}
Alternatively:
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int, 5> age{11,2,23,4,15};
for(int val : age) {
cout << val << " ";
}
cout << endl;
}
#include <iostream>
#include <vector>
#include <set>
#include <list>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <memory>
#include "C:\Users\HP\Documents\Dreyfus-Wagner\Dijkstra.h"
using namespace std;
#define MAX 100001
#define pir pair<int, int>
#define pbd(x) push_back(x)
using namespace std;
#define MAX 100001
#define pir pair<int, int>
#define pbd(x) push_back(x)
struct Out
{
int *p;
};
vector<pir> Graph[MAX];
int main()
{
int nodes1, edges1, starting1;
// create graph
cout << "Enter the number of vertices and edges: ";
cin >> nodes1 >> edges1;
cout << "Enter the edges with weigth: <source> <destination> <weigth>: \n";
int u1, v1, w1;
struct Out output[nodes1];
int adjMatrix[nodes1][nodes1];
for (int i = 0; i < edges1; i++)
{
cin >> u1 >> v1 >> w1;
Graph[u1].pbd(pir(v1, w1));
Graph[v1].pbd(pir(u1, w1)); // for undirected
}
cout << "Enter the source node: ";
cin >> starting1;
for (int i = 1; i <= nodes1; i++)
{
output[i].p = Djiktra(nodes1, edges1, i, Graph);
}
for (int j = 1; j <= nodes1; j++)
{
for (int i = 1; i <= nodes1; i++)
{
cout << "*(p + " << i << ") : ";
cout << *(output[j].p + i) << endl;
;
}
cout << " " << endl;
}
}
If we call the single instance of function it gives correct output.
Note : Djiktra function is stored in another file that is referred in header. The idea is that to find shortest lengths of elements per node
Could anyone explain why the output is incorrect when called inside for loop?
I'm working with matrices in C++. The task is to find odd numbers in a matrix. I created a function, but when executed with Debug > Start Without Debugging... I get the error:
Debug assertion failed. Expression: vector subscript out of range.
This is my code:
File MatrixVec.h:
#include "MatrixVec.h"
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
MatrixVec::MatrixVec(int rows, int cols, int range)
{
row.assign(cols, 0);
mat.assign(rows, row);
for (int i = 0; i < mat.size(); i++) {
for (int j = 0; j < mat[i].size(); j++){
mat[i][j] = rand() % range;
}
}
}
void MatrixVec::process()
{
int *oddNum = new int(mat[0].size());
for (int i = 0; i < mat[0].size(); i++) {
oddNum[i] = 0;
}
for (int i = 0; i < mat.size(); i++) {
for (int j = 0; j < mat[0].size(); j++) {
oddNum[j] += mat[i][j] % 2;
}
}
for (int i = 0; i < mat[0].size(); i++) {
cout << i + 1 << ". kolona " << oddNum[i]<< " neparnih elemenata." << endl;
}
//Edit
delete[] oddNum; //forgot this line
}
File MatrixVec.h:
#include "Matrix.h"
#include <vector>
class MatrixVec : public Matrix {
public:
MatrixVec(int rows, int cols, int range);
void print();
void process();
private:
std::vector<int> row;
std::vector<std::vector<int> > mat;
};
File Matrix.h:
class Matrix {
public:
virtual void print() = 0;
virtual void process() = 0;
};
File main.cpp
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include "Matrix1D.h"
#include "Matrix2D.h"
#include "MatrixVec.h"
using namespace std;
#define NUMBER_RANGE 10
int main(int argc, char* argv[])
{
if (argc != 3)
{
cout << "Niste uneli potrebne argumente za pokretanje programa!" << endl;
cout << "Argumenti komandne linije treba da budu:" << endl;
cout << "1. N dimenzija matrice" << endl;
cout << "2. M dimenzija matrice" << endl;
exit(-1);
}
// inicijalizacija generatora nasumičnih brojeva
srand(unsigned int(time(NULL)));
int rowNum = atoi(argv[1]);
int colNum = atoi(argv[2]);
// a)
cout << endl << endl << "a) Matrix 1D representation" << endl;
Matrix1D mat1(rowNum, colNum, 10);
mat1.print();
mat1.process();
//b)
cout << endl << endl << "b) Matrix 2D representation" << endl;
Matrix2D mat2(rowNum, colNum, 10);
mat2.print();
mat2.process();
//c)
cout << endl << endl << "c) Matrix vector of vector representation" << endl;
MatrixVec mat3(rowNum, colNum, 10);
mat3.print();
mat3.process();
return 0;
}
This line int *oddNum = new int(mat[0].size()); actually creating one int with value of mat[0].size(). You want something like this to create an array of size mat[0].size().
int *oddNum = new int[mat[0].size()];
Note: This still not answer the exact error. Seems like your mat vector is empty.
I am a beginner and taking a CSC course, I have to write a program that converts a user input string into the sum of the ASCII value of each character, here is what I have so far, and Im still pretty far from being done. But any help would be greatly appreciated. Thanks
#include <iostream>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
using std::string;
using std::cout;
using std::endl;
int main()
{
{
int x;
std::cout << "enter string" << std::endl;
std::cin >> x;
}
string text = "STRING";
for (int i = 0; i < text.size(); i++)
cout << (int)text[i] << endl;
return 0;
}
You can use a range-based for loop to traverse the string and then add up each char within:
#include <iostream>
#include <string>
int main()
{
int sum = 0; // this is where all the values are being added to
std::string s;
std::cout << "enter string and press enter." << std::endl;
std::cin >> s; // string that the user enters will be stored in s
for (char c : s)
sum += c;
std::cout << "total ASCII values: " << sum << std::endl;
return 0;
}
For some odd reason, I have an assignment to shuffle the contents of a vector without using the shuffle or random_shuffle functions that are available in the C++ standard library. The following is some basic code with a (non-functioning) function to do the job to give you a clearer idea of what I'm getting at:
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
// Shuffle Vector Function:
void shuffle_vector(std::vector<string> &names)
{
}
// end function
int main(void)
{
srand(time(0));
vector<string> names;
names.push_back("Sally");
names.push_back("Sue");
names.push_back("Bob");
names.push_back("Fred");
cout << "Your names:" << endl;
for (int i = 0; i < names.size(); i++)
{
cout << i + 1 << ". " << names[i] << endl;
}
cout << "Press Enter to shuffle.";
cin.get();
shuffle_vector(names);
cout << "\nYour shuffled names:" << endl;
for (int i = 0; i < names.size(); i++)
{
cout << i + 1 << ". " << names[i] << endl;
}
cin.get();
}
The way I thought to do it is to:
"push_back" the vector to create a temporary spot
randomly assign an index into the temporary spot
randomly assign an index into the newly-empty spot
put the index in the temporary spot into the last remaining empty index
"pop_back" the vector to its original size
(like with switching indexes in arrays)
I don't know how exactly to execute this but also--more importantly--if this would even work or if it's the best way to go about it. How would you do it?
Bam! This was actually pretty fun to figure out!
I used rand and a "for" loop that iterated 100 times to randomize it. I also added a "temporary" index that was deleted after the shuffling was complete.
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
// Shuffle Vector Function:
void shuffle_vector(std::vector<string> &names)
{
for (int i = 0; i < 100; i++)
{
int randomIndex = rand() % names.size();
int randomIndex2 = rand() % names.size();
if (randomIndex2 == randomIndex) // make sure the two random values aren't the same
{
do {
randomIndex2 = rand() % names.size();
} while (randomIndex2 == randomIndex);
}
names.push_back("temporary"); // create temporary index at the end of the vector
int last_index_number = (names.size() - 1);
names[last_index_number] = names[randomIndex];
names[randomIndex] = names[randomIndex2];
names[randomIndex2] = names[last_index_number];
names.pop_back(); // bring vector back to original size
}
}
// end function
int main(void)
{
srand(time(0));
vector<string> names;
names.push_back("Sally");
names.push_back("Sue");
names.push_back("Bob");
names.push_back("Fred");
cout << "Your names:" << endl;
for (int i = 0; i < names.size(); i++)
{
cout << i + 1 << ". " << names[i] << endl;
}
cout << "Press Enter to shuffle.";
cin.get();
shuffle_vector(names);
cout << "\nYour shuffled names:" << endl;
for (int i = 0; i < names.size(); i++)
{
cout << i + 1 << ". " << names[i] << endl;
}
cin.get();
}