Why is this code outputting so many numbers? [duplicate] - c++
This question already has answers here:
ARRAYSIZE C++ macro: how does it work?
(7 answers)
c++ sizeof(array) return twice the array's declared length
(6 answers)
Closed 3 years ago.
Starting with two arrays a and b, I am trying to output a matrix c with dimensions sizeof(a) and sizeof(b), whose entries are the product of every pair of the Cartesian product of a and b.
Theses products are also stored in a two dimensional array c.
My code is below.
#include <iostream>
#include <string>
int main()
{
int a[]= { 1,2,3,4,5,5 };
int b[]= { 1,23,2,32,42,4 };
int c[sizeof(a)][sizeof(b)];
for (int i = 0; i < sizeof(a); i++) {
for (int j = 0; j < sizeof(b); j++) {
c[i][j] = a[i]* b[j] ;
std::cout << c[i][j] << " ";
}
std::cout << "\n";
}
return 0;
}
My output is:
1 23 2 32 42 4 -858993460 -858993460 1 2 3 4 5 5 -858993460 16710224 15543422 1 2161328 2122464 16710312 15543008 196436084 15536213
2 46 4 64 84 8 -1717986920 -1717986920 2 4 6 8 10 10 -1717986920 33420448 31086844 2 4322656 4244928 33420624 31086016 392872168 31072426
3 69 6 96 126 12 1717986916 1717986916 3 6 9 12 15 15 1717986916 50130672 46630266 3 6483984 6367392 50130936 46629024 589308252 46608639
...
This is just a small part of the output.
sizeof(a) is not the length of the array, it is the number of bytes required to store it.
Since the element type of the array is larger than one byte each, the numbers are different.
Related
How to relocate an element in one array in C++
I took this interview question and I failed, so I'm here to not fail again! I have an array of int with size 16 and a 5 < givenIndex < 10. I have to take the element in this index a print every possible array (there are 16) by moving the element at givenIndex through every position in array and pushing rest of elements. For example: int array[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; int givenIndex = 6; Since array[givenIndex] = 7, I need to move 7 to every possible position and print that array. [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] [7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16] [1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16] [1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16] And that's for 16 cases. What I was trying was: for(int i = 0;i<16;i++){ array[i] = array[indexInsercion] if (i<indexInsert){ //right shift array[i] = array[i+1] }else if(i == indexInsert){ //no shift }else{ //left shift array[i] = array[i-1] } } Can I get some help?
We can only guess what the interviewer expected to see. If I was the interviewer I would like to see that you keep things simple. This is code I think one can expect to be written from scratch in an interview situation: #include <iostream> #include <array> template <size_t size> void print_replaced(const std::array<int,size>& x,size_t index){ for (int i=0;i<size;++i){ for (int j=0;j<i;++j) { if (j == index) continue; std::cout << x[j] << " "; } std::cout << x[index] << " "; for (int j=i;j<size;++j) { if (j == index) continue; std::cout << x[j] << " "; } std::cout << "\n"; } } int main() { std::array<int,16> x{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; print_replaced(x,6); } It is a first approach at the problem, with a loop that prints 16 different combinations of the array elements. Printing each line follows simple logic: We print all elements before the one that should be replaced, then the one that should be shuffled, then the remaining elements. It is simple, but wrong. Its output is: 7 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 7 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 2 7 3 4 5 6 8 9 10 11 12 13 14 15 16 1 2 3 7 4 5 6 8 9 10 11 12 13 14 15 16 1 2 3 4 7 5 6 8 9 10 11 12 13 14 15 16 1 2 3 4 5 7 6 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 8 7 9 10 11 12 13 14 15 16 1 2 3 4 5 6 8 9 7 10 11 12 13 14 15 16 1 2 3 4 5 6 8 9 10 7 11 12 13 14 15 16 1 2 3 4 5 6 8 9 10 11 7 12 13 14 15 16 1 2 3 4 5 6 8 9 10 11 12 7 13 14 15 16 1 2 3 4 5 6 8 9 10 11 12 13 7 14 15 16 1 2 3 4 5 6 8 9 10 11 12 13 14 7 15 16 1 2 3 4 5 6 8 9 10 11 12 13 14 15 7 16 There is one line that appears twice and the last line is missing. As an interviewer I would not be surprised that the first attempt does not produce correct output. I don't care about that. Thats not a minus. What I would care about is how you react on that. Do you know the next steps? Do you have a strategy to fix the wrong output? Or do you just panic because you didn't manage to write the correct code on the first attempt? This is what I would like to check in an interview and then thats the end of the exercise. I want to ask more different questions rather than giving you the time to fix all mistakes and write correct well tested code, because I know that this takes more time than we have in the interview. I'll leave it to you to fix the above code ;)
Here's a quick stab at it. Basically just keep track of where the given index should go and print it there as well as skip the original position it would be in. #include <iostream> int main() { int array[16] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 }; int givenIndex = 6; for (int p = 0; p <= 16; ++p) { if (p != givenIndex) { std::cout << "["; for (int i = 0; i < 16; ++i) { if (i == p) { if (i > 0) { std::cout << ","; } std::cout << array[givenIndex]; } if (array[i] != array[givenIndex]) { if (i > 0 || p == 0) { std::cout << ","; } std::cout << array[i]; } } if (p == 16) { std::cout << "," << array[givenIndex]; } std::cout << "]\n"; } } } Output: [7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16] [1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16] [1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16] [1,2,3,7,4,5,6,8,9,10,11,12,13,14,15,16] [1,2,3,4,7,5,6,8,9,10,11,12,13,14,15,16] [1,2,3,4,5,7,6,8,9,10,11,12,13,14,15,16] [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] [1,2,3,4,5,6,8,7,9,10,11,12,13,14,15,16] [1,2,3,4,5,6,8,9,7,10,11,12,13,14,15,16] [1,2,3,4,5,6,8,9,10,7,11,12,13,14,15,16] [1,2,3,4,5,6,8,9,10,11,7,12,13,14,15,16] [1,2,3,4,5,6,8,9,10,11,12,7,13,14,15,16] [1,2,3,4,5,6,8,9,10,11,12,13,7,14,15,16] [1,2,3,4,5,6,8,9,10,11,12,13,14,7,15,16] [1,2,3,4,5,6,8,9,10,11,12,13,14,15,7,16] [1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,7]
If the expectation is to just print the elements of array in the given order: Keep the track of current index of array element to be print, say indx - If the position of current element processing is equal to row number then print the element at givenIndex. If indx is equal to givenIndex skip it and print indx + 1 element, otherwise print element at indx and increase indx by 1. Implementation: #include <iostream> #include <array> int main() { std::array<int, 16> array = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; std::size_t givenIndex = 6; for (std::size_t i = 0, indx = 0; i < array.size(); indx = 0, ++i) { std::cout << '['; for (std::size_t j = 0; j < array.size(); ++j) { if (j == i) { std::cout << array[givenIndex] << ','; continue; } if (indx == givenIndex) { ++indx; } std::cout << array[indx++] << ','; } std::cout << ']'; std::cout << '\n'; } return 0; } Output: # ./a.out [7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,] [1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16,] [1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16,] [1,2,3,7,4,5,6,8,9,10,11,12,13,14,15,16,] [1,2,3,4,7,5,6,8,9,10,11,12,13,14,15,16,] [1,2,3,4,5,7,6,8,9,10,11,12,13,14,15,16,] [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,] [1,2,3,4,5,6,8,7,9,10,11,12,13,14,15,16,] [1,2,3,4,5,6,8,9,7,10,11,12,13,14,15,16,] [1,2,3,4,5,6,8,9,10,7,11,12,13,14,15,16,] [1,2,3,4,5,6,8,9,10,11,7,12,13,14,15,16,] [1,2,3,4,5,6,8,9,10,11,12,7,13,14,15,16,] [1,2,3,4,5,6,8,9,10,11,12,13,7,14,15,16,] [1,2,3,4,5,6,8,9,10,11,12,13,14,7,15,16,] [1,2,3,4,5,6,8,9,10,11,12,13,14,15,7,16,] [1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,7,] If the expectation is to alter the order of elements in the array and then print the array: First move the element at givenIndex to the 0th index of array and then - Print array In every iteration swap the current element with its next element in the array and print it. Implementation: #include <iostream> #include <array> void print_array (std::array<int, 16>& array) { std::cout << '['; for (std::size_t indx = 0; indx < array.size(); ++indx) { std::cout << array[indx] << ','; } std::cout << ']'; std::cout << '\n'; } void rearrange_array_elem (std::array<int, 16>& array, std::size_t givenIndx) { // move the element at givneIndx to first position in array for (std::size_t j = givenIndx; j > 0; --j) { std::swap (array[j], array[j - 1]); } // print array print_array (array); for (std::size_t indx = 0; indx < array.size() - 1; ++indx) { // swap current element with its next element std::swap (array[indx], array[indx + 1]); print_array (array); } } int main() { std::array<int, 16> array = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; std::size_t givenIndex = 6; rearrange_array_elem (array, givenIndex); return 0; } Output: # ./a.out [7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,] [1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16,] [1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16,] [1,2,3,7,4,5,6,8,9,10,11,12,13,14,15,16,] [1,2,3,4,7,5,6,8,9,10,11,12,13,14,15,16,] [1,2,3,4,5,7,6,8,9,10,11,12,13,14,15,16,] [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,] [1,2,3,4,5,6,8,7,9,10,11,12,13,14,15,16,] [1,2,3,4,5,6,8,9,7,10,11,12,13,14,15,16,] [1,2,3,4,5,6,8,9,10,7,11,12,13,14,15,16,] [1,2,3,4,5,6,8,9,10,11,7,12,13,14,15,16,] [1,2,3,4,5,6,8,9,10,11,12,7,13,14,15,16,] [1,2,3,4,5,6,8,9,10,11,12,13,7,14,15,16,] [1,2,3,4,5,6,8,9,10,11,12,13,14,7,15,16,] [1,2,3,4,5,6,8,9,10,11,12,13,14,15,7,16,] [1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,7,]
How do I save all the numbers from a string into a multi-dimensional array in c++?
I have to write a program that takes a completed sudoku board, saves only the numbers (meaning all the symbols used between the numbers to separate them such as '-', '|' etc cant be saved) into a two-dimensional array. #include <iostream> #include <string> using namespace std; int main() { int input[11] = { 0 }; int sudoku[9][9] = { 0 }; for (int line = 0; line <= 10; line++) { cin >> input[line]; } system("PAUSE"); return 0; } This is the only working code I've got so far. I've tried different kinds of for loops to get this done but I can't figure why it doesn't work. So I wanted to ask, is it even possible save all the numbers of a string into a multi-dimensional array? And if it's not, where is my approach wrong or how could I solve this task? One example of the input would be: .5.1.4.|.8.6.9.|.7.2.3 .8.7.2.|.3.4.5.|.6.1.9 .9.6.3.|.2.1.7.|.5.4.8 -------|-------|------- .6.2.8.|.1.3.4.|.9.5.7 .1.9.7.|.6.5.2.|.8.3.4 .4.3.5.|.7.9.8.|.1.6.2 -------|-------|------- .2.4.6.|.9.7.1.|.3.8.5 .7.5.1.|.4.8.3.|.2.9.6 .3.8.9.|.5.2.6.|.4.7.1
One approach is to use regular expressions. This way the formatting of the sudoku board can change but your will still be able to parse out the numbers. The reason I broke it into two for loops was to easily ignore the row that has no numbers in it. #include <iostream> #include <regex> #include <string> #include <vector> int main() { std::string line; // this regular expression matches a single digit std::regex exp("(\\d)"); std::smatch res; int sudoku[9][9] = {{0}}; int row = 0; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { // get a line of the board std::getline(std::cin, line); // search for the next digit in the line for (int k = 0; std::regex_search(line, res, exp); ++k) { // convert the digit into an integer and store it in the board sudoku[row][k] = std::stoi(res[0]); // the rest of the line after the first match becomes the new // line so that we can search for the next digit line = res.suffix(); } row += 1; } // ignore every third row that is used to separate the board sections std::getline(std::cin, line); } for (int i = 0; i < 9; ++i) { for (int j = 0; j < 9; ++j) { std::cout << sudoku[i][j] << " "; } std::cout << std::endl; } return 0; } For your example board, it produces this output: 5 1 4 8 6 9 7 2 3 8 7 2 3 4 5 6 1 9 9 6 3 2 1 7 5 4 8 6 2 8 1 3 4 9 5 7 1 9 7 6 5 2 8 3 4 4 3 5 7 9 8 1 6 2 2 4 6 9 7 1 3 8 5 7 5 1 4 8 3 2 9 6 3 8 9 5 2 6 4 7 1
Trash in operating memory with dynamic arrays
I've tried to make an algorithm, which counts how many numbers are divided without a remainder. Code works, but every time I run it, I'm getting "trash" numbers in my output. I'm using dynamic arrays to solve a problem. #include <iostream> using namespace std; int main() { int N = 30; int *data = new int [N]; for (int i = 0; i < 100; i++) { for (int c = 1; c < N; c++) { if (i % c == 0) { data[c] += 1; } } } for (int k = 1; k < N; k++) { cout << data[k] << endl; } delete [] data; } I've expected to have, at least, what C++ Shell says: http://cpp.sh/6xtc 100 50 34 25 20 17 15 13 12 10 10 9 8 8 7 7 6 6 6 5 5 5 5 5 4 4 4 4 4 , but got in different IDE the same result: 100 11932994 34 25 20 17 15 13 12 620757039 37045 11951945 8 11927896 7 7 7290 158 6 5 5 570425383 37040 11951941 4 11927892 4 1835102827 859059803
You do int *data = new int [N]; And allocate an N-sized array. Then you immediately start trying to increment the values in it: data[c] += 1; But what was in there to begin with? If you want to guarantee that all the values will be initialized to 0, per this answer you can allocate your array with: int *data = new int [N](); // ^^^^ <Obligatory "you should just be using std::vector!" comment here.\> (But actually though, vectors would make this way easier and avoid this issue entirely)
data[c] += 1; You add something to an uninitialized value. data[c] was not necessarily 0 before that. new does not initialize a dynamic array to zero. Quit all new/delete and use std::vector<>. And, do learn to use the debugger - really helpful in this case.
Ordered merge problems
I've got a little problem with my program and I can't solve what's wrong. Basically, there are two arrays sorted in ascending order and I have to merge them into one. I expect the output to be: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 But it is: 1 2 3 4 5 6 7 8 9 10 11 12 13 0 What am I doing wrong, that the last value in the output isn't 14? I think that the solution to this will be very simple, but I can't figure it out. Here's the code: #include<iostream> #include<cstdlib> using namespace std; int main(){ int arrA[]={1,3,5,7,9,11,13}; int arrB[]={2,4,6,8,10,12,14}; int arrC[sizeof(arrA)/sizeof(int)+sizeof(arrB)/sizeof(int)]; int sizeA=sizeof(arrA)/sizeof(int); int sizeB=sizeof(arrB)/sizeof(int); int sizeC=sizeof(arrC)/sizeof(int); for (int i=0;i<sizeA;){ for (int j=0;j<sizeB;){ if (arrA[i]<=arrB[j]){ arrC[i+j]=arrA[i++]; } else{ arrC[i+j]=arrB[j++]; } } } for (int i=0; i<sizeC; i++){ cout << arrC[i] << " "; } return 0; }
Actually you never get to the point where You assign value to arrC[13] which is the last 14 element. In last iteration for your outer loop i==6 and the same for the inner loop. So you end when i+j is equal to 12.
Assigning a vector to a matrix column in Eigen
This question was asked in haste. The error in my original program, was not the typo in the code that is displayed here. The error was that in my program v was not getting populated due to some conditions. The more useful takeaway from this thread is the demonstration of copying a std::vector to all rows or columns of an Eigen Matrix, in the accepted answer. I want to copy vectors into the columns of a matrix, like the following: #include <Eigen/Dense> #include <vector> #include <iostream> int main() { int m = 10; std::vector<Eigen::VectorXd> v(m); Eigen::MatrixXd S(m,m); for (int i = 0; i != m; ++i) { v[i].resize(m); for (int j = 0; j != m; ++j) { v[i](j) = rand() % m; } //S.cols(i) = v[i]; //needed something like this } return 0; } S is of type Eigen::MatrixXd and dimension mxm. v is a std::vector of Eigen::VectorXd, where each Eigen::VectorXd is of size m and there are m of them in v.
Regarding the original question, you need to wrap the std::vector with an Eigen::Map. You could/should also make the operation a one-liner. The reworded question is reduced to a typo. S.cols(i) should be S.col(i). int main() { size_t sz = 6; Eigen::MatrixXd S(sz, sz); std::vector<double> v(sz); std::vector<Eigen::VectorXd> vv(sz); for(int i = 0; i < sz; i++) { v[i] = i*2; vv[i] = Eigen::VectorXd::LinSpaced(sz, (i+sz), (i+sz)*2); } for (int i = 0; i != sz; ++i) S.col(i) = vv[i]; std::cout << S << "\n\n"; S.rowwise() = Eigen::Map<Eigen::RowVectorXd>(v.data(), sz); std::cout << S << "\n\n"; S.colwise() = Eigen::Map<Eigen::VectorXd>(v.data(), sz); std::cout << S << "\n\n"; return 0; } which would output 6 7 8 9 10 11 7.2 8.4 9.6 10.8 12 13.2 8.4 9.8 11.2 12.6 14 15.4 9.6 11.2 12.8 14.4 16 17.6 10.8 12.6 14.4 16.2 18 19.8 12 14 16 18 20 22 0 2 4 6 8 10 0 2 4 6 8 10 0 2 4 6 8 10 0 2 4 6 8 10 0 2 4 6 8 10 0 2 4 6 8 10 0 0 0 0 0 0 2 2 2 2 2 2 4 4 4 4 4 4 6 6 6 6 6 6 8 8 8 8 8 8 10 10 10 10 10 10