C++ :: Use 2D Vector Instead Of 2D Arrays?

Mar 15, 2013

how to use 2D vector instead of 2D arrays?

View 1 Replies


ADVERTISEMENT

C++ :: Function For Two Dimensional Arrays With Vector

Nov 8, 2014

#include <iostream>
#include <cassert>
#include <vector>
using namespace std;
vector<int> flatten(int a[100][200]) {
vector<int> v;
for (int i = 0; i < 100; ++i) {

[Code]...

I made a function for two dimensional arrays with vector, but I don't know how to make the test code for it.

View 4 Replies View Related

C/C++ :: Two Arrays With Different Sizes Into One 2 Dimensional Vector

Mar 24, 2012

I am using 2 ARRAYS OF DIFFERENT SIZES in One 2-Dimensional Vector, and my output is not correct. The arrays are size 4 and size 13.

I want COLUMN 0 to have: 55, 66, 77, 88.

I want COLUMNs 1-12 to have 1,2,3,4,5,6,7,8,9,10,10,10,11 in EACH ROW. It would seem that the 2nd loop for the size 13 array would need to loop 4 times in order to fill 4 rows, however, I'm not sure how to do that. Here is what I have so far in code and output:

#include <iostream>    
#include <vector>  
using namespace std;  
int main() {
    int typeArray[4] = {55,66,77,88};
    int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};  

[Code] ....

OUTPUT:
55 0 0 0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10 10 10 11
77 0 0 0 0 0 0 0 0 0 0 0 0
88 0 0 0 0 0 0 0 0 0 0 0 0

How to populate rows correctly?

View 2 Replies View Related

C++ :: 2 Arrays - Vector Outputs Incorrect Answers

Oct 19, 2014

Program: I have 2 arrays: 1 for the correct answers to a quiz, 1 for the user. I then have a vector to hold the incorrect answers. It keeps outputting what looks like alt characters, why.

Here is the code:

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

[Code] .....

View 2 Replies View Related

C/C++ :: Two Hard-coded Arrays Into One 2-Dimensional Vector

Mar 20, 2012

Two Hard-coded Arrays into One 2-Dimensional Vector I have 2 arrays, each of them are hard-coded with integer values. I also have one 2-Dimensional vector and I want to put 1 array into the first column of the vector and the other array into the 2nd column of the vector. The reason is that I want to do math on the 2nd column of the vector only.

I am able to accomplish this with 3 arrays. Two of them are 1-Dimensional and the third array is 2-Dimensional.

I know how to pass ONE Array into ONE vector:

Code:
vector<int> myVector(typeArray, typeArray + 4);

however, when I declare a 2-Dimensional vector:

Code:
vector< vector <int> > myVector(3, vector<int> (2,0))

I am not seeing how to add TWO arrays or how to OUTPUT it to the screen.

Here is my code using DevCPP:

#include <iostream>    
#include <vector>
#include <Windows.h>

[Code]...

I don't get any errors, however, I don't know how to output it to the screen to see what it looks like.

View 5 Replies View Related

C++ :: Using Arrays As Sources Of Data For Arrays In A Structure?

Feb 6, 2014

I define "Comwords" as a string, but apparently it takes the members as chars, then I can't set strings in a structure equal to the chars.

I see to also be having unknown problems with the ComMAL array and loading it values into another element of the same structure.

How to correct this? I was thinking of casting char elements as strings, but could find no reference in my library book regarding how to do that (lots on casting int's a doubles...)

Code:

int _tmain(int argc, _TCHAR* argv[]) {
int comm = 10;
int targ = 5;
int death;
struct AI_WORDS

[Code]....

View 2 Replies View Related

C :: Assigning Values To Arrays / Printing Arrays

Jul 1, 2014

Using a for loop, construct two 100 element arrays, x and y, such that element i of x stores the value sin(2*pi*i/100)) and the corresponding element of y stores cos((2*pi*i/100)). Print the values stored in the elements of x and y as you calculate them.

I have attempted to solve it but I'm not sure why the value 0 is only being printed, maybe I haven't assigned sin(2i/100)) and cos((2i/100)) to the arrays properly?

Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main () {

[Code] .....

View 3 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++ :: 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++ :: 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++ :: Ranged For Loop With Arrays Of Arrays

Dec 17, 2014

int arr[4][5];
for (int(&row)[5] : arr){
for (int &column : row){
} }

Why do we name it row instead column?

int arr[row][column]

Wouldn't this be like

int arr[4][5];
for (int(&column)[5] : arr){
for (int &row: column){
}

It just seems funny to me because we use row[5] which should be 4 if we meant row? Or did I read it wrong in C++ prime !?

View 2 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++ :: Dynamic Creation Of Arrays Of Pointers To Arrays Of Pointers

Apr 15, 2013

I'm trying to write a function that takes a 32bit address and a data to store at this address.

I'm wanting to take the 32 bit memory address eg 0x12345678 and split it
into 4 x 2 bytes
12, 34, 56, 78

then each of the 4 entries is at most a 256 entry array.eg
FF, FF, FF, FF

So in this example, 0x12 points to 0x34 in the second array, which points to 0x56 in the third array, which finally points to 0x78 in the last array. This last array holds the actual data.

After successfully doing 0x12345678, say I might get a read for 0x1234AABB. So, the first and second pointers already exist, but I then have to create and write to dynamically created arrays.

The arrays need to have all entries set to NULL so that i know whether to follow the pointers to overwrite a previously entered value or create new arrays and pointers.

It all looks good and simple in the pseudo code I've written up but I'm having trouble coding it. I'm currently trying to deal with the first entry case, ie all array elements are NULL, but I'm getting confused with the pointers and creation of new arrays.

void cpu::store(unsigned int mem_add,unsigned int mem_val) {
int first = (mem_address&4278190080)>>24;
int second = (mem_address&16711680)>>16;
int third = (mem_address&65280)>>8;
int fourth= (mem_address&255);

[Code] .....

A1 has been declared as
int* A1[256] ;

View 3 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++ :: How To Sum Two Int Arrays

Jul 24, 2013

like for example if

Num1[0]=2
Num2[0]=3

this is not working x= Num1[0] + Num2[0];

is there any other way ?

View 11 Replies View Related

C++ :: Add Arrays / Get The Sum Of Two Arrays?

Nov 13, 2013

Basically on this assignment I must write program that inputs two positive integers of at most 100 digits and outputs the sum of the numbers into an array. The two positive numbers must come from different arrays and the sum has to be stored into another array.

View 2 Replies View Related

C++ :: How To Sum Up Two Arrays Together

Jul 23, 2013

For example if we have

array1[5]= {1,2,3,4,5}
and
array2[5]={2,3,4,0,7)

how do you sum them up as 12345 + 23407 . And when i say sum I mean normal mathematical sum.

here is my code:

int main () {
string str1;
string str2;

[Code].....

View 4 Replies View Related

C++ :: How To Merge Two Arrays

Feb 12, 2014

Compiled on turbocpp 4.5.....error is that the final merged array is much bigger with garbage values...

Code:
//cpp program to concatenate 2 arrays
#include<iostream.h>
#include<conio.h>
//the class
class array

[Code] .....

View 6 Replies View Related

C :: How To Set Up A Function Using Arrays

Apr 16, 2013

how to make the functions for b, c, d. if possible running through on how to would be more beneficial than just giving me the answer.

Global warming. As part of a global warming analysis, a research facility tracks outdoor temperatures at the North Pole once a day, at noon, for a year. At the end of each month, these temperatures are entered into the computer and processed. The operator will enter 28, 29, 30, or 31 data items, depending on the month.

You may use 500 as a sentinel value after the last temperature, since that is lower than absolute 0. Your main program should call the read_temps(), hot_days(), and print_temps() functions described here:

(b) Write a function, read_temps(), that has one parameter, an array called temps, in which to store the temperatures. Read the real data values for one month and store them into the slots of an array. Return the actual number of temperatures read as the result of the function.

(c) Write a function, hot_days(), that has two parameters: the number of temperatures for the current month and an array in which the temperatures are stored. Search through the temperature array and count all the days on which the noon temperature exceeds 32F. Return this count.

(d) Write a function, print_temps(), with the same two parameters plus the count of hot days. Print a neat table of temperatures. At the same time, calculate the average temperature for the month and print it at the end of the table, followed by the number of hot days.

View 1 Replies View Related

C :: Intersection Of 2 Arrays

Jan 12, 2015

Code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
int a[10],b[10],c[10],i,j,k,n,m;
printf ("Enter the size of the first vector: ");
scanf ("%d", &n);

[Code] .....

Now, this is what I've been able to do so far, i know it's very basic but I just started learning programming! I tried different methods found on internet but none of them work for me. I want to find the intersection of arrays a & b then put them in array c(or just display them?)

View 6 Replies View Related







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