c++) i have error when putting 1D array in 2D array - c++

whats the prob
#include <iostream>
using namespace std;
int main() {
int M; //height
int N; //length
cin >> M;
cin >> N;
int list[M][N], smaller[N];
string smaller_str;
for (int i = 0;i < M; ++i){
getline(cin, smaller_str);
for (int j = 0; j < N; i = i++) {
cin >> smaller_str[j];
}
list[i] = smaller;
}
}
i want to put 1D array "smaller" in to 2D array list
however i do get errors in the "list[i] = smaller;" part
i need help guys

Unless you want to check for errors, you won't need to use getline in this case. Just read each elements via >> operator.
Also note that Variable-Length Arrays like int list[M][N]; is not in the standard C++. You should use std::vector instead of that.
Another point is the the i = i++ in the inner loop is wrong. It should be ++j.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int M; //height
int N; //length
cin >> M;
cin >> N;
//int list[M][N];
std::vector<std::vector<int> > list(M, std::vector<int>(N));
for (int i = 0;i < M; ++i){
for (int j = 0; j < N; ++j) {
cin >> list[i][j];
}
}
}

You cannot assign another arrays to an array, what you can do is copy/ move its content. For this, use std::copy,
...
int list[M][N], smaller[N];
string smaller_str;
for (int i = 0; i < M; ++i) {
getline(cin, smaller_str);
for (int j = 0; j < N; i = i++) {
cin >> smaller_str[j];
}
std::copy(smaller, smaller + N, reinterpret_cast<int*>(&list[i]));
}
Note: If you would like to move the content rather than copying, swap the std::copy with std::move.

Related

C++ 2d array using vectors returns a consistent incorrect result on the second query

I'm doing a coding challenge where the aim is to take x arrays and query it y times. Each array is given a size (N) and a list of values and each query wants a specific value (b) from a specific array (a).
The input is given as follows:
X Y
N n n n ...
N n n n ...
a b
a b
Here is all of my code:
#include <vector>
#include <iostream>
using namespace std;
int main() {
int numArrays;
int numQueries;
scanf("%d %d", &numArrays, &numQueries);
vector<int*> arrays;
for (int i = 0; i < numArrays; i++)
{
int size;
scanf("%d", &size);
int arr[size];
for (int j = 0; j < size; j++)
{
scanf("%d", &arr[j]);
}
arrays.push_back(arr);
}
for (int i = 0; i < numQueries; i++)
{
int arr, ind;
scanf("%d %d", &arr, &ind);
printf("%d\n", arrays.at(arr)[ind]);
}
return 0;
}
The test input is 2 arrays and 2 queries given as follows:
2 2
3 1 2 3
5 9 8 7 6 5
0 1
1 3
The expected output should give 2 for the first query (which seems to be working fine) and then 6 for the second query however my actual output is this:
2
32767
Does anybody know why the second output comes out as 32767. This seems to be a consistent value no matter what the arrays contain or what the second query is looking for. I think it's likely something to do with my vector declaration but I'm still relatively new to C++ so I'm not sure.
The problem has been already described in comments by others: You are storing a pointer to an array on the stack that will be undefined outside the for loop. Just use vectors for both array dimensions and let them handle pointers and memory allocations for you:
#include <iostream>
#include <utility>
#include <vector>
int main() {
size_t numArrays, numQueries;
std::cin >> numArrays >> numQueries;
std::vector<std::vector<int>> arrays;
arrays.reserve(numArrays);
for (size_t i = 0; i < numArrays; ++i) {
size_t size;
std::cin >> size;
std::vector<int> array;
array.reserve(size);
for (size_t j = 0; j < size; ++j) {
int number;
std::cin >> number;
array.push_back(number);
}
arrays.push_back(std::move(array));
}
for (size_t i = 0; i < numQueries; ++i) {
size_t arr, ind;
std::cin >> arr >> ind;
std::cout << arrays[arr][ind] << std::endl;
}
}
Admittedly, this lacks even the most basic error checking and error handling; it is simply a rewrite of the code from the question.
Of course you can handle the memory allocations yourself, but in that case you also need to make sure things get properly deallocated. (Again, the following example lacks error checking.)
#include <iostream>
#include <memory>
#include <utility>
int main() {
size_t numArrays, numQueries;
std::cin >> numArrays >> numQueries;
const auto arrays{
std::make_unique_for_overwrite<std::unique_ptr<int[]>[]>(numArrays)};
for (size_t i = 0; i < numArrays; ++i) {
size_t size;
std::cin >> size;
arrays[i] = std::make_unique_for_overwrite<int[]>(size);
for (size_t j = 0; j < size; ++j) std::cin >> arrays[i][j];
}
for (size_t i = 0; i < numQueries; ++i) {
size_t arr, ind;
std::cin >> arr >> ind;
std::cout << arrays[arr][ind] << std::endl;
}
}

C++ Segmentation fault while trying to create a vector of vectors

I want to create a vector of vectors in C++ with dimensions nx2 where n(rows) is given by user. I am trying to insert values in this vector using a for loop but As soon as give the value of n(rows), it gives a Segmentation fault error
What to do?
#include <iostream>
#include <vector>
#include <cstdlib>
#define col 2
using namespace std;
int main()
{
int row;
cin >> row;
vector<vector<int>> vec;
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
cin >> vec[i][j];
}
return 0;
}
You need to resize a vector before inserting elements. Or use push_back to insert incrementally.
vector<vector<int>> vec;
vec.resize(row);
for (int i = 0; i < row; ++i)
{
vec[i].resize(col);
for (int j = 0; j < col; ++j)
{
cin >> vec[i][j];
}
}
OR:
vector<vector<int>> vec;
vec.resize(row);
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
int value;
cin >> value;
vec[i].push_back(value);
}
}

Why the output is not showing?

My program is to find the smallest positive number missing from an array. With the following input I expect an output of 2.
6
0
-9
1
3
-4
5
My problem is that it does not give any output. Can anyone explain this please?
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int array[n];
for (int i = 0; i < n; i++)
{
cin >> array[n];
}
int const N = 1e4+2;
bool indexarray[N];
for (int i = 0; i < N; i++)
{
indexarray[i] = false;
}
for (int i = 0; i < n; i++)
{
if (array[i] > 0)
{
indexarray[array[i]] = true;
}
}
int ans = -1;
for (int i = 1; i < N; i++)
{
if (indexarray[i] == false)
{
ans = i;
}
}
cout << ans << endl;
return 0;
}
I think because int array[n]; makes an array called array with n elements in it, with the first one starting at array[0].
cin >> array[n]; needs to modify array[n], but because the first element is array[0], the last element is array[n-1], and array[n] does not exist. Your code gave an error and exited.
Try changing
for (int i = 0; i < n; i++)
{
cin >> array[n];
}
to
for (int i = 0; i < n; i++)
{
cin >> array[i];
}
Also, I think variable length arrays are non-standard, so maybe try changing that. Replace it with std::vector<int> array(n) should work.

Process returned -1073741819 (0xC0000005) in vectors

I know that the vector is a dynamic array so you can enter values as you want. But in this code bellow the code crashes before I insert any value in the vector !! I don't know what I am doing wrong and I am new in programming. I tried to give it a size but it is not working.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int H,W;
cin >> H >> W;
vector <vector <int>> A;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
cin >> A[i][j];
return 0;
}
You are reading values to A[i][j] while the vector A has no elements.
You have to allocate elements to read something there.
To allocate beforehand, you can use constructors.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int H,W;
cin >> H >> W;
// allocate H W-element vectors
vector <vector <int>> A(H, vector <int>(W));
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
cin >> A[i][j];
return 0;
}
Alternatively, you can insert elements via push_back().
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int H,W;
cin >> H >> W;
vector <vector <int>> A;
for (int i = 0; i < H; i++)
{
vector <int> row;
for (int j = 0; j < W; j++)
{
int value;
cin >> value;
row.push_back(value);
}
A.push_back(row);
}
return 0;
}

How can I read specific lines of a text file into an array of type int in C++?

I need to read text files into an array. The text files are formatted so that the first line contains the array size and the following lines are the elements of the array:
6
5
3
2
1
6
4
Where the array size equals 6. The files need to be read in this format:
a.exe < testfiles/input
Here's what I have so far:
int main(){
int arraySize = 0;
cin >> arraySize;
int array[arraySize];
for(i = 1; i < arraySize; i++){
cin >> array[i];
}
for(j = 0; j < arraySize-1; j++){
//insertion sort here
}
}
The array remains all zeros but is the correct size. Can anyone see my error?
EDIT: I've fixed the array to be dynamically allocated. The program now executes without an error, but now the entire array is full of the second entry of the text file, in this case, 5.
Since this question reads like a homework assignment, I'm guessing you are not allowed to use std. On the off chance you are, however, here is a much safer way to accomplish what you are attempting:
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> nums;
int arraySize;
cin >> arraySize;
for(int i = 0; i < arraySize; i++){
int value;
cin >> value;
nums.push_back(value);
}
for (auto num : nums)
cout << num << " ";
return 0;
}
In a C++ array declaration, array size must be an integral const expression. Simplest change is to use a dynamic array:.
int main(){
using namespace std;
int arraySize = 0;
cin >> arraySize;
int * array = new int[arraySize];
for(int i = 1; i < arraySize; i++){
cin >> array[i];
}
for(int j = 0; j < arraySize-1; j++){
//insertion sort here
}
delete [] array;
}