How to insert variable amount of data in 2d array in c++ - c++

I have the following file input.txt which contains the values :
0 0
0 1
0 2
0 3
1 1
1 4
2 1
2 4
2 3
I want to insert these values to two arrays. I want them to look like this in the end.
0 0 1 2 3
1 1 4
2 1 4 3
First array will contain the id and second array will contain the elements. I have managed to dynamically allocate memory for these two arrays using the following :
int n,m,i=0,tempNum,lines;
int NumberOfIds=0;
ifstream read("input2.txt");
while(read>>n>>m){
if (i==0){
tempNum=n;
}
if (tempNum != n){
NumberOfIds++;
}
tempNum = n;
i++;
}
lines=i-1;
//printf("%d", j);
int** idElements = new int*[NumberOfIds];
int* ids = new int[NumberOfIds];
int* numberOfIdElements = new int[NumberOfIds];
// Rewinds file
read.clear();
read.seekg(0, ios::beg);
int counter = 0;
NumberOfIds=0;
while(read>>n>>m){
if (tempNum != n){
numberOfIdElements[NumberOfIds] = counter;
NumberOfIds++;
counter = 0;
}
tempNum = n;
counter++;
i++;
}
for(int k = 0; k < NumberOfIds; ++k)
idElements[k] = new int[numberOfIdElements[k]];
Now I am stuck in how to insert the data. Any help would be appreciated.

You can do this with std::vector's fairly easily:
std::vector<std::vector<int>> arr;
while(read>>n>>m){
//if we're too small, push empty rows till we're big enough
while(n + 1 > arr.size()) {
arr.push_back(std::vector<int>());
}
arr.at(n).push_back(m); //push m onto row n
}
Alternatively, if your first column numbers become large (so you wouldn't want to make many empty rows), you can use an std::map:
std::map<int, std::vector<int>> arr;
while(read>>n>>m){
//works because map subscript creates a key/val pair if one doesn't exist
//so if this row doesn't exist, we get a new vector to push_back to
arr[n].push_back(m); //push m onto row n
}
See it in action here: ideone

Here is another approach using a map of sets:
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <map>
#include <set>
int main()
{
std::map<int, std::set<int>> m;
std::ifstream ifs{"input.txt"};
// read
for(std::string line; std::getline(ifs, line);){
std::stringstream ss{line};
int key;
ss >> key;
for (int val; ss >> val;)
m[key].insert(val);
}
// print
for(auto i : m) {
std::cout << i.first << '\t';
for(auto j : i.second)
std::cout << j << ' ';
std::cout << '\n';
}
}
You might select other containers depending of your requirements for sorting or preserving duplicates.

Related

Why doesn't push_back keep working in a loop?

Completely new to C++. Programmed selection sort on 1D array of arbitrary length. Want to allow user to keep inputting integers into console to make an array of desired length, to be subsequently sorted.
Can only seem to make arrays of length 2 using a while loop for adding elements. Code and example of erroneous result when inputting 6, 2, 3, and 9 shown below.
Script:
// Preprocessor directives and namespace declaration
#include <iostream>
#include <vector>
using namespace std;
// Function
void SelectionSort(int *arr, int len)
{
// Loop through index j in arr
for (int j = 0; j < len; j++) {
// Assume element j is minimum, and initialise minIndex
int min = arr[j];
int minIndex = j;
// Loop through comparisons to determine actual minimum
// (of elements after and including j)
for (int i = j; i < len; i++)
{
if (min > arr[i])
{
min = arr[i];
minIndex = i;
}
}
// Swap minimum with element j
int temp = arr[j];
arr[j] = min;
arr[minIndex] = temp;
}
// Display resulting array
for (int i = 0; i + 1 < len; i++)
{
cout << arr[i] << ", ";
}
cout << arr[len - 1] << endl;
}
// Main
int main()
{
// Explain program to user
cout << "Sort 1D array of user-inputted length/contents" << endl;
cout << "To finish array, enter -999" << endl;
// Initialise dynamic array
vector<int> vDyn (1);
vDyn[0] = 0;
cout << "Enter first element of array: ";
int firstElement = 0;
cin >> firstElement;
vDyn[0] = firstElement;
// Loop to define elements until desired length reached
bool keepGoing = true;
while (keepGoing == true)
{
cout << "Enter another element: ";
int newElement = 0;
cin >> newElement;
if (newElement != -999)
{
vDyn.push_back(newElement);
} else
{
keepGoing = false;
}
}
// Convert vector to array (dynamic to static)
int* v = &vDyn[0];
// Get array length
int len = sizeof(v) / sizeof(v[0]);
// Run SelectionSort function
SelectionSort(v, len);
return 0;
}
Terminal:
Sort 1D array of user-inputted length/contents
To finish array, enter -999
Enter first element of array: 6
Enter another element: 2
Enter another element: 3
Enter another element: 9
Enter another element: -999
2, 6
This declaration
int len = sizeof(v) / sizeof(v[0]);
is equivalent to the declaration
int len = sizeof( int * ) / sizeof( int );
because the variable v is declared like
int* v = &vDyn[0];
The size of a pointer is equal usually to 4 or 8 bytes. So the variable length will have the value either 1 or 2 and does not depend on the number of elements stored in the vector..
Instead you should use for example
size_t len = vDyn.size();
You could declare the function like
void SelectionSort(int *arr, size_t len);
and call it like
SelectionSort( vDyn.data(), vDyn.size() );
Also as in C++ there is standard function std::swap declared in the header <utility> then instead of this code snippet
// Swap minimum with element j
int temp = arr[j];
arr[j] = min;
arr[minIndex] = temp;
you could just write
if ( j != minIndex ) std::swap( arr[j], arr[minIndex] );
And the inner for loop could look like
for ( size_t i = j + 1; i < len; i++)
^^^^^
In fact your function SelectionSort is a C function. A C++ function should be more general and use iterators. In this case it could sort arrays along with other containers.
Here is a demonstration program that shows a more general function called for an array based on a vector.
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
template <typename ForwardIterator>
void SelectionSort( ForwardIterator first, ForwardIterator last )
{
for ( ; first != last; ++first )
{
auto current_min = first;
for ( auto next = std::next( first ); next != last; ++next )
{
if ( *next < *current_min ) current_min = next;
}
if ( current_min != first )
{
std::iter_swap( current_min, first );
}
}
}
int main()
{
std::vector<int> v = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
for ( const auto &item : v )
{
std::cout << item << ' ';
}
std::cout << '\n';
SelectionSort( v.data(), v.data() + v.size() );
for ( const auto &item : v )
{
std::cout << item << ' ';
}
std::cout << '\n';
}
The program output is
9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9
In general you need also to write an overloaded function that accepts also a comparison function.
// Convert vector to array (dynamic to static)
int* v = &vDyn[0];
This line doesn't convert the array to anything. You merely take address of the first element in the vector.
If you want to take an underlying c-array from std::vector you are supposed to use data property of it.
Also, since the array is decayed into a pointer, it no longer contains data of its size. You should rely on std::vector properties (i.e. std::vector::size) to pass this information forward

How to convert numbers into vector of integers?

I want my program to ask the user to input any number and then store it into a std::vector where every single digit is allocated to a separate vector index:
input: 142
output vector: [1, 4, 2]
I tried this:
int main()
{
std::vector<int> v;
int number;
cin >> number;
for(unsigned int i = 100; i > 0; i/=10)
{
v.push_back(number/i);
number -= (number/i)*i;
}
for(size_t i = 0; i < v.size(); ++i)
{
std::cout<<v[i]<<std::endl;
}
}
It works. But what should I do when the input-length is unknown?
Use simply std::string and for each char(which is actually integers) of string convert to integer as follows: SEE LIVE HERE
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<int> v;
std::string number = "123456789987654321";
for(auto& Integer: number)
v.emplace_back(static_cast<int>(Integer - '0'));
for(const auto& it: v) std::cout << it << " ";
}
Output:
1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1
In case of entering unwanted characters/ user inputs(for example some negative numbers or even this: +-1234567), you can go for something with try-catch. try to convert the char to int, otherwise skip in the catch block as follows. SEE LIVE HERE
#include <iostream>
#include <vector>
#include <string>
#include <exception>
int main()
{
std::vector<int> v;
std::string number = "+-1234567";
for(auto& Integer: number)
{
std::string Char(1, Integer); // convert to string
try { v.emplace_back(std::stoi(Char)); }
catch(...) { continue; } // any case of exceptions
/* or using much simpler std::isdigit from <cctype>
by which conversion to std::string and try-catch can be avoided.
if(std::isdigit(Integer))
v.emplace_back(static_cast<int>(Integer - '0'));
*/
}
for(const auto& it: v) std::cout << it << " ";
}
Output:
1 2 3 4 5 6 7
Edte: As per #Aconcagua suggested, included the solution with std::isdigit
while(number) {
v.push_back(number%10);
number /= 10;
}
std::reverse(v.begin(), v.end());
Change the for initilization unsigned int i = number rather than unsigned int i = 100. The re-written for statement will be:
for(unsigned int i = number; i > 0; i/=10)
I you want to stay with digits and std::deque is an option you could do the following:
int main()
{
std::deque<int> v;
int number;
cin >> number;
while(number != 0)
{
v.push_front(number%10);
number = number/10;
}
for(size_t i = 0; i < v.size(); ++i)
{
std::cout<<v[i]<<std::endl;
}
return 0;
}
With the modulo operator you simply take the last digit and insert it in the front of the deque. Afterwards you "cut off" that digit with /10.
I used a deque, because you can't push front with vectors.

run time error with function convert from string to int

I have a string contains numbers but also contains spaces between them, i need to convert the string to int and store them in an int array .
Th following function produces a run time error
void fun(string m)
{
string temp;
int j = 0;
int h = 0;
int d;
int arr[10];
for (int i = 0; i < m.length(); i++)
{
while (m[i] != ' ')
temp[j++] = m[i];
d = atoi(temp.c_str());
arr[h++] = d;
}
for (int i = 0; i < sizeof(arr); i++)
{
cout << arr[i];
}
}
I would suggest using a stringstream for this vs. rolling your own implementation.
#include <sstream>
#include <iterator>
#include <iostream>
int main()
{
std::stringstream ss("1 2 3 4 5 6 7");
auto head = std::istream_iterator<int>(ss);
auto tail = std::istream_iterator<int>();
while(head!=tail)
{
std::cout << *head << "\n";
++head;
}
return 0;
}
if you're receiving the string in a method you can easily adapt the function above to create an empty stringstream and then pass it the string.
#include <sstream>
#include <iterator>
#include <iostream>
int main()
{
std::string astring = "1 2 3 4 5 6";
std::stringstream ss;
ss << astring;
auto head = std::istream_iterator<int>(ss);
auto tail = std::istream_iterator<int>();
while(head!=tail)
{
std::cout << *head << "\n";
++head;
}
return 0;
}
You are making several mistakes in your code.
You have not initialized temp to contain anything, but you are trying to index into its characters. Instead of temp[j++] = m[i], you need to use temp += m[i]; or temp.push_back(m[i]); or temp.append(&m[i], 1);. Or consider using an std::ostringstream to gather the characters, and then call its str() method to extract the final std::string value.
you are not incrementing i in your while loop. As soon as the for loop reaches a non-whitespace character, your while loop will end up running endlessly, continuously (and unsuccessfully) trying to append the same character to temp.
you are not doing any bounds checking to make sure your for loop does not exceed arr's max capacity of 10 numbers.
you are misusing sizeof(). sizeof(arr) returns the number of bytes that arr occupies in memory (10 * sizeof(int), which is 20 or 40 bytes, depending on your compiler's implementation of int). It is not the number of elements in the array (10). So you will exceed the bounds of the array trying to display 20/40 numbers instead of 10 numbers.
Your code is more complicated than it needs to be. You can use a std::istringstream for parsing and let it ignore whitespace for you. Use std::vector or other dynamically-sized container to receive the parsed numbers.
#include <sstream>
#include <vector>
void fun(string m)
{
std::istreamstream iss(m);
std::vector<int> vec;
int num;
vec.reserve(10); // optional
while (iss >> num) {
vec.push_back(num);
}
for (int i = 0; i < vec.size(); ++i) {
std::cout << vec[i];
}
/*
alternatively:
for (std::vector<int>::iterator iter = vec.begin(); iter != vec.end(); ++iter) {
std::cout << *iter;
}
*/
/*
alternatively (C++11 and later):
for (auto i: vec) {
std::cout << i;
}
*/
/*
alternatively (C++11 and later):
std::for_each(vec.begin(), vec.end(), [](int &n){ std::cout << n; });
*/
}
You cannot do for (int i = 0; i < sizeof(arr); i++)
The sizeof() operator gives you the size of something in bytes, which for an array of 10 ints probably amounts to 40. You need to use something like this:
#define COUNTOF(x) ((x)/sizeof((x)[0]))
for (int i = 0; i < COUNTOF(arr); i++)
The problem is in your while loop, J is 0 the first iteration through the While loop and gets incremented the first time it's called while also trying to assign it to the next letter.
Despite this issue I'd suggest using a string stream for this problem like others have said.
Here's a good reference.

Initializing a 2D Array C++

My goal is to read in a file at the command line, which is to be an adjacency matrix representation of a directed graph, and store the values in a 2D array. My plan is to read the number of integers on a line and use that as the size for my array(i.e. a 5 line input, with 5 integers per line would mean i would create a 5 x 5 array). However, to initialize an array, a constant value is needed, and counting the integers per line and storing it in a variable to use as my size parameter, does not allow me to create the array.
Sample input:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Code:
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
string currentLine;
int i, m = 0;
int count;
ifstream input(argv[1]);
int storage[10000];
printf("Original matrix: \n" );
if(input.is_open())
{
while(getline(input, currentLine))
{
istringstream iss(currentLine);
count = 0;
while(iss >> i)
{
if(iss.eof())//at end of each line, ends loop
{
count++;
storage[m] = i;
m++;
printf("%d \n", i);
break;
}
else
{
count++;
storage[m] = i;
m++;
printf("%d ", i);
}
}
}
}
int **matrix;
matrix = new int*[count];
for(int y = 0; y < count; y++)
matrix[y] = new int[count];
for(int r = 0; r < count; r++)
for(int c = 0; c < count; r++)
matrix[r][c] = storage[r+c];
printf("first = %d ", matrix[0][0]);
system("PAUSE");
return 0;
}
Based on my input, I should create a 5 x 5 array. But the line
int matrix[count][count];
Gives me an error saying that the "count" size parameter should be a constant. Is my way of counting the size of the input to use an invalid way of doing this, or is there a way to create a constant to use as my size param?
Instead of using 2D native C++ arrays, consider using a vector of vectors.
#include <vector>
#include <iostream>
int main() {
using namespace std;
int count = 5;
// create a matrix of 'count' rows by 0 columns
vector<vector<int>> matrix(count);
// resize the column count for each row
for (auto& r : matrix)
r.resize(count);
// use it just like an array
for (int i = 0; i < count; ++i)
matrix[i][i] = i;
for (int r = 0; r < count; ++r) {
for (int c = 0; c < count; ++c)
cout << matrix[r][c] << ' ';
cout << endl;
}
}
you will need to use a dynamic array
int *myArray; //Declare pointer to type of array
myArray = new int[x]; //use 'new' to create array of size x
myArray[3] = 10; //Use as normal (static) array
...
delete [] myArrray; //remeber to free memeory when finished.
http://www.cplusplus.com/forum/beginner/1601/
http://www.cplusplus.com/doc/tutorial/dynamic/
You are not going to get a 2-D array. You will have to work with Array of Array.
If you are willing to work with dynamic allocations, please do something like this:
int *myArray = new int [count]; // variable length array can get you into trouble here,
// if you are not careful as the inner dimension needs
// to be freed before the array goes out of scope.
for (/*iterate from 0 count*/) {
myArray[i] = new int [count];
}
myArray[m][n] to give you a semblance of what you wanted.

Sorting vector (c++)

I have to sort a vector N into M equals parts(M less than N).
But those equal parts have to be in the middle. If I have parts that are not equal i need to put them in the first and/or last element.Difference between first and last has to be minimal. I have managed to make a sort of that function. Function is making new vector(sizeof(M)) and inside stores number of parts from vector N.
ex. 10/7 vector M [1 1 2 2 2 1 1] 1+1+2+2+2+1+1=10 into 7 parts. Meaning I am taking n or n+1 objects for vector N and storing index in vector M. Values inside do not have to be equal.
Now I am having trouble because my deviations can only be first and last element.
And also i am having problems whit for
ex. 12/9 because i get M[1 1 1 2 2 2 1 1 1 ]
but if I can only have first and last as different
then it should be like M[3 1 1 1 1 1 1 1 2]
So my question : Is there any better way of making this?
#include "stdafx.h"
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int N = 12; // size of vector
int M = 9; // numbers of divisions
static void subsizes(int vecSize, int subCount, vector<int> &v)
{
int baseSize = vecSize / subCount;
int bumps = vecSize % subCount;
int i,n=0,nPlus,counterPlus=0,counterN,counterLeft;
vector<int> temp(subCount); // for new results
vector<int> proba(subCount);
for (i = 0; i < subCount; i++) //dividing to n, n+1 and placing them in vector
{
temp[i]= baseSize + (i < bumps);
}
for (i=0; i<subCount; i++) // finding what numbers are n, n+1
{
nPlus=temp[i];
if(n==0 && n<nPlus){
n=nPlus;}
}
for(i=0; i<subCount;i++) //counting n, n+1
{
if(nPlus==temp[i])counterPlus++;
}
counterN=subCount-counterPlus;
counterLeft=counterPlus-2;
for(i=0; i<counterPlus/2; i++) //placing them in right order
temp[i]=nPlus;
for(i=counterPlus/2; i<counterPlus/2+counterN; i++)
temp[i]=n;
for(i=counterPlus/2+counterN; i<subCount; i++)
temp[i]=nPlus;
cout<<endl<<"Divided vector is :"<<endl; //printing results
for(i=0; i<subCount;i++)
{
int part = temp[i];
cout<<"At : vector["<<i<<"] nubmer of objects is --- "<<part<<endl;
}
putchar('\n');
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> vec(N);
int vecSize = vec.size(); //length of original vector
int subCount=M; //number of sub-vectors parts
generate(vec.begin(), vec.end(), rand); // filling vector with C function rand()
cout<<"Vector is [ ";
for (auto i : vec) // printing out a vector
{
cout<<i<<" ";
}
cout<<"]"<<endl;
subsizes(vecSize,subCount,vec); // calling funciton that divideds and print results
system("PAUSE");
return 0;
}
You are complicating this, a lot.
If I understand you correctly
you want to take a vector of size N and divide it into M parts
Each of the M parts should have the same length
If there is remaining elements in N, you want to distribute the remainder to extra sub-parts at the beginning and end
Your output is a vector of the sizes of sub-parts
edit: it seems my assumption of "you want to distribute the remainder to extra sub-parts at the beginning and end" was wrong
If that is indeed what you want then you can just do this
void subsizes(int vecSize, int subCount, vector<int> &v)
{
int regular_size = (vecSize+2) / subCount;
int remainder = vecSize - regular_size * subCount;
vector<int> sizes(subCount, regular_size);
int front_remainder = remainder - (remainder / 2);
int back_remainder = remainder / 2;
sizes.front() += front_remainder;
sizes.back() += back_remainder;
for (int i = 0; i<sizes.size(); i++)
{
int part = sizes[i];
cout << "At : vector[" << i << "] nubmer of objects is --- " << part << endl;
}
putchar('\n');
}
I'm not sure I have a complete grip on your problem, but it appears that you are allowed to change the elements contained in vec. In which case, this is probably what you are looking for:
void subsizes(vector<int>& vec)
{
if(vec.size() > 1) // Don't do anything if there aren't at least 2 elements
{
// Get the sum of all the elements,
// but we're going to be adding back in 1s in every element but the last
// so subtract (vec.size() - 1) from this total.
// This is done by initializing accumulate to 1 - vec.size()
int last = accumulate(vec.begin(), vec.end(), 1 - vec.size());
// Now put 1s in all the elements
fill(vec.begin(), vec.end(), 1);
// Change the last element to the value accumulated in last
vec.back() = last;
}
}