C++ :: How To Make 5x3 2D Vector Of Integers

Feb 8, 2014

I am trying to make a 5x3 2D-vector of integers, then set its i-capacity to be 5 and j-capacity to be 3, i.e:

vec2D[i][j] i = 1,2,3,4,5 j = 1,2,3

and then assign integer values to it.

#include <vector>
#include <iostream>
using namespace std;
int main () {
vector<vector<int> > vec2D;

[Code] ....

It compiles, but does not work properly:

Test.exe exited with code -1073741819

i-capacity before reserve: 0
i-capacity after reserve: 5

i = 0
j-capacity before reserve: 336043326
j-capacity after reserve: 336043326
i = 1
j-capacity before reserve: 4282929217
j-capacity after reserve: 4282929217
Press <RETURN> to close this window...

I am trying to convert a C code with dynamic 2D arrays, to a C++ code. I prefer to keep the vec2D[i][j] = ... way of assignment instead of using vec2D.push_back(...).

View 8 Replies


ADVERTISEMENT

C++ :: Recursive Function 2 - Create Vector Of Vector Of Integers

Mar 26, 2013

Lets say that I have a vector of vector of integers. <1,2,3,4> , <5,6,7,8>, <10,11,12,13>

How do I make a function that creates vector of vector of every different integers?

<1,5,10> , <1,5,11>, <1,5,12>, <1,5,13>
<1,6,10> , <1,6,11>, <1,6,12>, <1,6,13>
<1,7,10> , <1,7,11>, <1,7,12>, <1,7,13>
<1,8,10>, <1,8,11>, <1,8,12>, <1,8, 13>
<2,5,10>, <2,5,11>, <2,5,12>, <2,5,13>

and so on...

View 2 Replies View Related

C++ :: Vector With Strings And Integers

Apr 5, 2013

I have a file where the first column shows letters, second column shows numbers.

How could I make it to print everything exactly how it is in the file – a vector/array with strings and integers?

View 13 Replies View Related

C++ :: Vector Of Integers - How To Print Indicators

Apr 8, 2014

I have a vector of integers, which can take values 0 or 1, say

0 0 0 1 1

I need to print indicators, telling in which position 1-values occur. So

0 0 0 1 0
and
0 0 0 0 1

View 11 Replies View Related

C++ :: Program To Create A Vector Of Integers

Feb 10, 2015

1. Write a c++ program to create a vector of integers. copy the vector contents into list, sort the contents, then copy selected items into another vector(like elements less than 10 etc)

View 1 Replies View Related

C :: Binary Tree - Return A Vector Of Integers

Oct 19, 2014

So I have a Binary Tree and I need to return a vector of integers (the nodes) of the heaviest path in the tree.

First, is it possible to do in C? Because I think a vector is an ADT in C++.

I've started writing something recursive, which worked for a balanced tree of height 1, and failed for longer height.

This is what I've written - [URL] ....

View 6 Replies View Related

C++ :: Generate Random Integers In The Range And Store Them In A Vector

Sep 3, 2014

You are to write a C++ program to generate random integers in the range [ LOW = 1, HIGH = 10000 ] and to store them in a vector < int > of size VEC_SIZE = 250. Then, sort the contents of the vector (in ascending order) and display it on stdout.

To sort the contents of a vector, use the sort ( ) function from the STL. In addition to the main ( ) routine, implement the following subroutines in your program:

• void genRndNums ( vector < int >& v ) : This routine generates VEC_SIZE integers and puts them in vector v. Initializes the random number generator (RNG) by calling the function srand ( ) with the seed value SEED = 1, and generates random integers by calling the function rand ( ).

• void printVec ( const vector < int >& v ) : This routine displays the contents of vector v on stdout, printing exactly NO_ITEMS = 12 numbers on a single line, except perhaps the last line. The sorted numbers need to be properly aligned on the output. For each printed number, allocate ITEM_W = 5 spaces on stdout.

Programming Notes:

• You are not allowed to use any I/O functions from the C library, such as scanf or printf. Instead, use the I/O functions from the C++ library, such as cin or cout.
• Let v be a vector of integers, then the call: sort ( v.begin ( ), v.end ( ) ) sorts the elements of v in ascending order. The detailed description of the sort ( ) routine can be found on the course web site and in the course textbook.
• Execute the srand ( ) function only once before generating the first random integer with the given seed value SEED. The rand ( ) function generates a random integer in the range [ 0, RAND_MAX ], where the constant value RAND_MAX is the largest random integer returned by the rand ( ) function and its value is system dependent. To normalize the return value to a value in the range [ LOW, HIGH ], execute: rand ( ) % ( HIGH – LOW + 1 ) + LOW.

View 1 Replies View Related

C++ :: Sorting Large Numbers Of Vector Of Random Integers Returns All Zero

Jul 31, 2014

I tried to sort a large numbers of vector of random integers with std::sort(), but when the number increases over 10M, std::sort returns all zero in values. Does std::sort have a limitation of input numbers?

View 1 Replies View Related

C/C++ :: Statistical Analysis Of Vector Data Set Of Integers - Frequency Distribution?

Feb 16, 2014

I am working on my second c++ project that is a statistical analysis of a vector data set of integers. I created a struct called range with a maximum value, minimum value and count for occurrence. The frequencies are stored in a vector<range>.

I have tried messing around with the increment to get the high range test above the maximum element, though my ranges cumulatively increase by 9.9. I'm not sure how the professor's program has the distance between high and low ranges as 10 instead of 9.9, which is the increment. I'm also unsure why half way through his buckets, the ranges appear as 9.99 briefly and then return to 10 after (40.00 - 49.99 then 49.99 - 59.99). I feel like I am missing something very obvious on line 04 or 10 in my code.

Here is the code for the frequency that accepts a vector reference for my data set of integers.

void frequency(vector<int>& data_set){
double max = findMax(data_set);
double min = findMin(data_set);
double increment = (max - min) / 10;
double percentage = 0.0;

[Code] ....

View 1 Replies View Related

C++ :: How To Make Vector Of Char

Jun 8, 2013

let say

char temp[8][8];

and you want to make vector of this char

vector<????> boardVec;

View 2 Replies View Related

C++ :: How To Make Cin Accept Different Punctuation From Vector

Mar 7, 2013

My program is a dictionary vector with a cin at the end that will read your input and check if it's in the dictionary.

#include "std_lib_facilities_3.h"
#include <algorithm>
#include <string>
#include <iostream>

string translate_to_lower(string s){
transform(s.begi[/code]n(), s[/code].end(), s.begin(), (int (*)(int)) tolower);

[Code] ...

How do I make the program accept inputs such as "hello?", "hello!", and "hello,"?

View 1 Replies View Related

C++ :: How To Make Sure Dereference To Vector Is Valid

Oct 6, 2014

I have this piece of code in parts of my path finding algorithm

for( int head; head < q.size(); ++ head ){
walk& w = q[head];

// do manything with w
if( some_condition ) q.push_back( walk( w.x + 1, w.y, head ) );
}

However I notice that sometimes w is cannot be dereferenced. It can but it throws junk number at me. Perhaps the vector is changing it size and move the whole array to a different location. Is there anyway to make sure that w is always valid ?

I just want to use w because of shorter typing and cleaner look not because of performance. I also refrain from using macro.

View 8 Replies View Related

C++ :: How To Make Random Letters With A Vector

Mar 9, 2014

I am supposed to make a scrabble game and randomize 10 of the letters.

Here's my code:
class Spinner
Spinner::Spinner(string things[], int numThings[], int n)
{
currentPosition = 0;

[Code].....

View 2 Replies View Related

C++ :: Read Set Of Integers Then Find And Print Sum Of Even And Odd Integers

Apr 21, 2014

I'm a beginner at c++ and I need to write a program that reads a set of integers and then finds and prints the sum of the even and odd integers. The program cannot tell the user how many integers to enter. I need to have separate totals for the even and odd numbers. what would I need to use so that I can read whatever number of values the user inputs and get the sum of even and odd?

View 2 Replies View Related

C++ :: Using Vector Push Back Function To Output Contents Of Vector (similar To Array)

Feb 9, 2015

How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.

here is my code:

#include <iostream>
#include <array>
#include <vector>
using namespace std;
int duplicate( vector < int > &vector1, const int value, const int counter)

[Code].....

View 3 Replies View Related

C++ :: Open File And Read In Rows To String Vector And Return Vector

Jun 7, 2012

I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,

N,N'-dimethylethylenediamine.mol

This is the function that opens the file :

Code:
// opens mol file, reads in rows to string vector and returns vector
vector<string> get_mol_file(string& filePath) {
vector<string> mol_file;
string new_mol_line;
// create an input stream and open the mol file
ifstream read_mol_input;
read_mol_input.open( filePath.c_str() );

[Code] ....

The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.

View 9 Replies View Related

C++ :: Create Class Vector As Template And Define Operations On Vector?

May 13, 2013

I need to create a class vector as a template and define operations on vectors.

And this is what I made.

#include<iostream>
using namespace std;
template<class T>

[Code].....

View 2 Replies View Related

C++ :: Find Function To A Vector Of Structures Vector

Jul 5, 2013

I have asked a related question before, and it was resolved successfully. In the past, when I wanted to use std::max_element in order to find the maximum element (or even sort by using std::sort) of a vector of structures according to one of the members of the structure, all I had to do was to insert a specially designed comparison function as the third argument of the function std::max::element. But the latter comparison function naturally accepts two arguments internally.

For instance, here is a test program that successfully finds the maximum according to just one member of the structure:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

[Code] ....

And the output was this, as expected:
Maximum element S.a of vector<S> vec is at: 9
[I]max element of vec.a between slot 3 and slot 6 is: 6, and its index is: 6 vec[6].a = 6
[I]max element of vec.a between slot 4 and slot 7 is: 7, and its index is: 7 vec[7].a = 7
[I]max element of vec.a between slot 5 and slot 8 is: 8, and its index is: 8 vec[8].a = 8
[I]max element of vec.a between slot 6 and slot 9 is: 9, and its index is: 9 vec[9].a = 9

However, I now need to search and find an element of vector<myStruct> according to just one member of myStruct, instead of finding the maximum or sorting as before. This presents a problem because the function std::find does not accept such a comparison function as its third argument.

This was the description of the std::find function that I found: find - C++ Reference

Code:
template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);

I could also find another function called std::find_if, but this only accepts a unary predicate like this: find_if - C++ Reference

Code:
template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);

And once again this is either inadequate of I don't see how to use it directly, because for the third argument I would like to insert a function that takes two arguments with a syntax like this:

Code:
int x=7;
std::vector<S>::iterator result;
result = std::find(vec.begin(), vec.end(), []( const (int x, const S & S_1) { return ( x == S_1.a ) ; } ) ;

Is there another std function that I can use to make search and find an element according to just one member of myStruct?

Or perhaps there is a clever way to pass two arguments to the unary predicate.

View 4 Replies View Related

C/C++ :: Cannot Print Vector Of Vector Of Doubles

Mar 31, 2014

I am trying to print a matrix solution that I've stored in a vector of doubles. The original matrix was stored in a vector of vector of doubles. I want to print to the screen and an output file. Right now it just prints to screen column-style and the output file is horizontal-style. I tried to change the solution from vector of doubles to a vector of vector of doubles like the original matrix but when I run my code it crashes. Here is what I am referring to:

void readMatrix(vector<vector<double>> &matrix, vector<double> &b, int &N, string input) {
ifstream in(input);
in >> N;
matrix.resize(N);
b.resize(N);

[Code] ....

However when I change my printResult function to this (I removed the string argument because I just want to get it working to the screen first):

void printResult(vector<vector<double>> &sol, const int N) {
//ofstream out(output);
//int j;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++){

[Code] ....

The program crashes. Am I even printing the matrix correctly?

View 4 Replies View Related

C/C++ :: Swapping Vector Int And Vector Strings

Mar 30, 2013

I want to have it so that when i ask for the person witch item they want to drop on the ground it goes into another vector that i can pick back up the item if they want it back and erase when they walk away.

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cmath>  
using namespace std;  
    struct InventoryItem

[Code] ....

View 4 Replies View Related

C++ :: Can't Read In Matrix Into Vector Of Vector Of Int

Mar 24, 2012

Code:
#include <iostream>
#include <istream>
#include <fstream>
#include <vector>

[Code]....

Why is it reading in nothing for the arrays, and also making the size of the total thing the total number of numbers? It should have a size of 2, not 5.

View 14 Replies View Related

C++ :: Copy Part Of Vector 1 To Vector 2

Jun 21, 2012

I am trying to copy vector to vector as follow:

Code:

std::vector<unsigned char> vec1;
//insert some values into vec1
std::vector<unsigned char> vec2;

Now I want to to copy 2 bytes from vec1 starting at index 5., why do i need to know how many bytes from the end of vec1?? can't i just specify how many bytes i want to copy from starting index?

std::copy(vec1.begin()+5, vec1.end()-??, vec2.begin());

View 2 Replies View Related

C/C++ :: Sum Of First N Integers

Oct 5, 2014

Write a program, sumit.c, that reads an integer value n from the user and then sums the integers between 0 and n. Your program should be able to handle both positive and negative values of n and should write the sum to the output file sumitout.txt. Note that your sum should not actually include 0, since that doesn't affect the sum. Your program should open the file sumitout.txt for appending so that the file can record a series of runs.

For marking purposes run your program twice with values n = −10 and n = 5.

Here is a sample run:

Enter n: -4 The sum of integers from -1 to -4 is -1

#include<stdio.h>
int main(void) {
//declare your variables
int n, i, term, total = 0;
//promt user to enter and read a value of n
printf("Enter n: ");
scanf("%d", &n);
//calculate the sum

[Code] ....

View 2 Replies View Related

C++ :: Converting 1D Vector Into 2D Vector

Apr 20, 2013

Code:
#include <iostream>
#include <vector>
int main() {
std::vector<std::vector<double> > DV; //2d vector
std::vector<double>temp(8,0.0); //1d vector

[Code] .....

The conversion actually works but it does not give the expected the result. The output should be:

Code:
1 2 3
4 5 6
7 8

and it outputs:

Code:
123123123

View 5 Replies View Related

C++ :: What Is Another Name For A Vector Inside Of A Vector

Sep 11, 2013

What is another name for a Vector inside of a Vector. What is the name of this construct? Would it be a Constructor Vector? I think this is a trick question my teacher is asking...

View 4 Replies View Related

C :: Dividing Two Integers

May 14, 2014

i am relatively new to C programming so i run into problems on daily basis. But this time i have something i just cant figuer out and i was hoping you could point me towards the right track. I am trying to divide two integers.DevValue by KpTotal. for some reason my micro controller allways crashes.Y is a variable of a distance measuring sensor. i have a 4x4 keypad to enter a three digit number (e.i 123) so Kp1 = 1 Kp2 = 2 Kp3 =3.

Code:

int kp1, kp2, kp3, kpTotal = 0;
char txt[6] = ""
int keypadPort at PORTD;
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
}

[code]....

i think it has something to do with the format of the value. i read that the micro controller crash when dividing by zero.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved