Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm a newbie in C++ STL. I have some problem in the below code with respect to the output.Why is this not providing the expected output?
#include<iostream>
#include<algorithm>
#include<vector>
#include<iterator>
using namespace std;
void show(vector<int> vect)
{
for(int i=0;i<vect.size();i++)
{
cout<<vect[i]<<" ";
}
}
int main()
{
int arr[5]={ 1, 2, 3, 4, 5 };
vector<int> vect1(arr,arr+5);
vector<int>::iterator it;
it=vect1.begin();
advance(it,3);
copy(vect1.begin(),vect1.end(),inserter(vect1,it));
show(vect1);
return 0;
}
Expected output:1 2 3 1 2 3 4 5 4 5
Actual output:1 2 3 1 0 3 4 5 4 5
Can anyone help me in finding out where it went wrong?
Replace the copy with this:
vect1.insert(it, std::begin(arr), std::end(arr));
where the arguments to insert are:
it is the position you want to insert into vector vect1;
std::begin(arr) is the start position of range to insert;
std::end(arr) is the end position of range to insert.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to do dfs traversal using recursive call, My Graph, and vector<int>visited are global variables. num_v variable corresponds to number of vertices,num_e corresponds to number of edges I tried calling two DFS calls in main, but my output matches to the correct DFS order for the first case. and it gives incorrect out for the other, kindly help where I'm getting wrong in the implementation of it.
sample input: first line is a number of vertices and number of edges. next, each line is directed nodes between two-point.
5 5
1 2
2 4
4 5
5 3
1 3
expected output:
1 2 4 5 3
2 4 5 3
my wrong output
1 2 4 5 3
2
code:
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
int mx=1e5;
//int num_v;
vector<vector<int>>Graph(mx);
vector<int>visited;
void dfs(int v,vector<int>&vec){
visited[v]=1;
vec.push_back(v);
for(int neigh:Graph[v]){
if(visited[neigh]!=1){
dfs(neigh,vec);
}
}
}
int main(){
int num_v;int num_e;
cin>>num_v>>num_e;
visited.resize(num_v + 1);
for(int i=0;i<num_e;i++){
int u,v;
cin>>u>>v;
Graph[u].push_back(v);
}
vector<int>vec;
dfs(1,vec);
for(auto x:vec){
cout<<x<<" ";
}
visited.clear();
cout<<endl;
vector<int>tt;
dfs(2,tt);
for(auto y:tt){
cout<<y<<" ";
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to obtain the absolute difference of the sum of left and right diagonal of the given 2d array.
I have written the following function-
int diagonalDifference(vector<vector<int>> arr) {
int n=arr.size();
int summ1=0,summ2=0,result=0;
for(int i=0;i<n;i++)
{ for(int j=0;j<n;j++)
{
if(i==j)
{ summ1=summ1+arr[i][j];}
else if((i+j)==(n-1))
{ summ2=summ2+arr[i][j];}
}
}
result=abs(summ1-summ2);
return result;
}
input array
11 2 4
4 5 6
10 8 -12
Explanation- summ1=11+5+(-12)
summ2=4++5+10 result=|4-19|=|-15|=15
expected output: 15
The output I am getting is 10
Here is a solution in O(n) complexity. reference
for (int i = 0; i < n; i++)
{
summ1 += arr[i][i];
summ2 += arr[i][n-i-1];
}
The else-if should be an if by itself.
The else-if prevents the 5 from being added to summ2. Which means the final calculation is: (11+5-12)-(4+10) = -10. Whose absolute value is 10.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have to store the matrix
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
in a variable called "a".I am given the following code block
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
.................
which I have to finish.I think there should be some connection between the elements of the matrix,but I can't see any.
EDIT:I must not use any additional variables.
You have an array of 5 times 5 integral values; i obviously stands for a row index, whereas j indicates a column index. The respective value in each cell then is (i-1)*5 + j.
But, probably a pitfall introduced by your teacher, be aware that array indizes in c++ start from 0, not 1. And that's why I'm giving more hints than usual for a question without any attempt of solving it:
int arr[5][5];
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
val = // enter the expression here
rowIndex = i-1;
columnIndex = j-1;
// insert the one missing statement here :-)
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am taking a course on edx.org Introduction to C++ by Microsoft. I get unwanted output when looping through a single dimensional array. The code is below.
<#include <iostream>
int main() {
int arrayName[10] = { 1,2,3,4,5,6,7,8,9,10 };
for (int i = 1; arrayName[i] <= 20; i++) {
std::cout << i << std::endl;
}
The output of this is:
1
2
3
4
5
6
7
8
9
10
11
Where does the 11 come from? And, if I make i=0, it also prints a 0. How does it print more than 10? And, when I try to change arrayName[10] to arrayName[9], I get a compiler error that there are too many initialized values:
int arrayName[10] = { 1,2,3,4,5,6,7,8,9,10 };
do {
std::cout << i << std::endl;
i++;
} while (arrayName[i] < 5);
The output is:
12
13
14
15
16
17
18
That do-while loop outputs 7 integers that I did not specify to be included in the arrayName[] array.
I don't know what I am doing wrong or what I am not understanding.
Please help. Thank you!
First, note that arrays in c++ start at index 0. So in int arrayName[3] = {10, 42, 88}; then arrayName[1] is 42, not 10. That means the last element in this array is int arrayName[2]. There is no element at index 3.
Your array only contains 10 elements (indices 0 to 9). The standard does not specify what happens when you access an element past the end of an array, anything can happen. In your case, arrayName[10] and arrayName[11] happens to give you something less than or equal to 20, and then arrayName[12] gave you something greater than 20, ending the loop. If you try it on another computer, or even at a different time, the results will vary. It might also crash (this is the best case scenario).
See this answer for more information on undefined behavior.
I finally found this: Correct way of loop through the C++ arrays, answer by https://stackoverflow.com/users/1619294/mark-garcia.
Changed my code to:
std::cout << "Looping through arrayName3 with std::array and letting the compiler determine how many objects to print:" << std::endl;
// Need to #include <array>
std::array<int, 10> arrayName3 = { 1,2,3,4,5,6,7,8,9,10 };
for (const auto& i : arrayName3) // Range-for
{
std::cout << i << std::endl;
}
The output was what I wanted:
1
2
3
4
5
6
7
8
9
10
This let's the compiler know it is deciding what to output. It would be great to know how to change this to control how many indices to loop through.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can u Guys Please give me tips on how to reduce the compilation time of my c or c++ programmes...
Some basic simple techniques will be helpful.
I was solving a question through a site(https://www.codechef.com/problems/TRISQ)
The Question was :-
What is the maximum number of squares of size 2x2 that can be fit in a right angled isosceles triangle of base B.One side of the square must be parallel to the base of the isosceles triangle.Base is the shortest side of the triangle.
First line contains T, the number of test cases.
Each of the following T lines contains 1 integer B.
Output exactly T lines, each line containing the required answer.
Sample Input
11
1
2
3
4
5
6
7
8
9
10
11
Sample Output
0
0
0
1
1
3
3
6
6
10
10
MY CODE
#include<iostream>
using namespace std;
int main()
{
int T,N,a,i,j;
cin>>T;
while(T--)
{
a=0;
cin>>N;
N=N/2;
N--;
j=N;
for(i=0;i<j;i++)
{
a+=N;
N--;
}
cout<<a<<endl;
}
}
So how do u guys think that this code (for eg) can be edited for better compilation time?
First profile.
Second, turn up optimizations levels on you compiler.
Thirdly, replace your for loop with multiplication / algebra. For example, the line
a+=N
is the fundamental basis for multiplication (repetitive addition), and the loop can be replaced by:
a += j * N; N -= j;
Replacing the loop will speed up your program (if your compiler hasn't already replaced the loop).
Printing the assembly language for the function will show how the compiler applied optimizations.
Edit 1:
Less code means a faster build time as well. I don't know if time difference in building is measurable.