C++ array sort is giving unexpected results - c++

My code is giving unexpected results. This code is for sorting the elements in the array. Upon running it gives different answer. Can anyone please suggest where the problem might be?
void func(int *arr,int N){
sort(arr,arr+N);
for(int i=0;i<N;i++){
cout<<arr[i]<<" ";}
cout<<endl;
}
int main() {
int N;
int *arr=new int[N];
cin>>N;
for(int j=0;j<N;j++){
cin>>arr[j];
}
func(arr,N);
return 0;
}
Input:
84
1 0 1 2 1 1 0 0 1 2 1 2 1 2 1 0 0 1 1 2 2 0 0 2 2 2 1 1 1 2 0 0 0 2 0 1 1 1 1 0 0 0 2 2 1 2 2 2 0 2 1 1 2 2 0 2 2 1 1 0 0 2 0 2 2 1 0 1 2 0 0 0 0 2 0 2 2 0 2 1 0 0 2 2
Output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 10 807415840 807415840 807415840
807415840 807416096 807416096 807416352 807416352 824193056 824193056
824193312 824193312 824193312 824193568 824193568 824193568 840970272
840970272 840970272 840970272 840970272 840970272 840970272 840970272
840970528 840970528 840970784 840970784 840970784 840970784 840970784

You have an uninitialized variable in your code.
int N;
From the online cpp reference on Uninitialized variables:
It is possible to create a variable without a value. This is very dangerous, but it can give an efficiency boost in certain situations. To create a variable without an initial value, simply don’t include an initial value:
// This creates an uninitialized int
int N;
The value in an uninitialized variable can be anything – it is unpredictable, and may be different every time the program is run. Reading the value of an uninitialized variable is undefined behaviour – which is always a bad idea. It has to be initialized with a value before you can use it.
So before allocating memory of the array based on the value of N, initialize it. In your case, read into it first.
cin>>N;
int *arr=new int[N];
It is also a good practice to check if cin has succeeded and if the value of N is within acceptable bounds before using it.

You are creating the array when N has an indeterminate value, so the result of your program is undefined.
Move this line;
cin>>N;
before this line:
int *arr=new int[N];

Related

C++ reading from file is not giving expected, or any output

I am trying to make a game which loads it's levels from a text file. I decided to do this with the help of a 2 dimensional vector of integers. Before implementing it in my main code, I first decided to check whether my logic was right so I made a Test.txt file containing the the level I wanted to draw.
Test.txt:-
1 1 0 0 1 0 0 0 0 0 1 1 1 1 1 1 1 0 1 0 1 0 0 1 0 0 1 0 0 1 1 0
1 0 1 0 1 1 1 0 1 1 0 1 1 0 1 1 1 0 1 0 0 1 1 1 1 1 1 0 0 1 0 0
0 0 0 0 0 0 0 1 0 1 0 0 0 1 1 0 1 1 0 0 0 0 0 0 1 0 0 1 0 1 1 0
0 0 1 1 1 1 1 0 0 0 1 0 1 0 1 1 0 0 0 1 1 1 1 0 0 0 1 0 1 1 1 0
1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 1 0 0 1 0 1
0 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 1 0 0 1 0 1 1 0 0 0 0 1 1 0 1
0 1 1 1 0 1 1 0 1 0 1 1 0 1 1 0 0 1 0 0 0 1 1 0 1 1 1 1 1 1 0 1
0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 1 0 1 0 1 1 0 0 1 0 0 0 1 0 0
0 0 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 1 0 0 0 1 1 1 0 1 1 1 1 0 1
0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 1 1 0 1 1 0 1 0 0 1 1 1 1 0 1 1
1 1 0 1 1 1 1 0 0 1 0 0 1 0 0 1 0 1 0 1 1 1 1 1 0 0 0 1 1 0 1 0
0 0 1 0 0 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 1 0 1 0 0 0 0 1 1 0 1
0 1 1 0 0 0 0 0 0 1 1 0 1 1 0 1 1 0 1 0 0 1 0 0 1 1 0 1 1 1 1 0
1 0 1 1 1 0 1 1 1 1 0 0 0 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 0 0
1 0 1 1 0 0 0 0 1 0 0 1 1 0 0 0 1 0 0 1 0 0 0 1 0 1 0 1 1 0 0 1
0 0 1 1 1 0 1 1 1 0 1 0 0 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0
0 0 0 1 0 1 0 1 0 1 0 0 1 1 1 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 1 0
1 0 1 0 1 0 0 0 1 1 0 0 0 1 0 1 1 1 1 1 0 0 1 0 1 0 1 1 1 1 1 1
Each integer is seperated by a space and only one number is supposed to be read once at a time. The 1 and 0 tell the game which tile to draw. Now, with this wrote the following code in c++ to read the file and populate the vector with it's contents. After that it's supposed to output the contents of the vector.
Test.cpp:-
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int num;
vector<vector<int>> nums;
int main(void) {
ifstream FileIn;
FileIn.open("Test.txt");
for(int i = 0; i < 18; i++) {
vector<int> temp;
for(int j = 0; j < 32; j++) {
FileIn >> num;
temp.push_back(num);
}
nums.push_back(temp);
temp.clear();
}
cout << nums.size() << '\n'; // outputs 18
for (unsigned int i=0; i < nums.size(); i++) {
for (unsigned int j=0; j < nums[i].size(); j++) {
cout << nums[i][j];
if (j == nums[i].size() - 1) cout << '\n';
}
}
FileIn.close();
return 0;
}
but this is where the problem starts. The code doesn't output anything to the terminal it just starts and then goes back to the prompt.
The executable compiles with no errors and there are no crashes or runtime error either. There is just no output.
Things i have tried:
Putting in spaces between the numbers
Keeping all integers on the same line
both of the solutions above, but together this time
I am using atom with the platformio terminal plugin on windows 10 (64-bit) on a intel with amd-64 architecture. Any help would be very appreciated.
A few things: Start learning how to use a debugger. Your question to stackoverflow is something you probably could easily answer on your own, if you stepped through your code with a debugger. That'd save you time - and us.
Edited:
Also, you open 'test.txt' without verifying or setting the "current working directory". This will work only if you start the application from the same path, test.txt is in. But if you run the app from someplace else, the working directory may be different.
You did not check for eof or any other error condition. How do you know if opening the file did actually work? Or reading the number? Or that there are exactly the amount of numbers you are expecting.
Checking error conditions may seem like a nuisance, but it's definitely not. Hunting for errors, which you did not check in your code, is much more time consuming than forming a habit to check for errors.
Here is some code:
ifstream FileIn;
FileIn.open("test.txt");
if (!FileIn.good())
cerr << "Could not open file...";
else {
while (!FileIn.eof()) {
int num; ///!!! DONT make `num` a global variable
FileIn >> num;
if (FileIn.bad()) {
cerr << "Invalid number in file...";
return 1; // return prematurely from the application
}
// Do something with the number
}
}
Also, use cout << endl instead of cout << '\n';. endl is the official way of inserting a line break and it will work on any platform, whereas '\n' may or may not work. Some platforms require two characters.
So I finally found the problem, the code, logic, text file everything were working fine. After taking the advice about debuggers from the other answer, this time instead of using atom's terminal plugin, I used powershell and it worked. It gave the output i was expecting. Then i tried the same code with the atom's terminal and that gave no output. So the problem seemed to be not in the code but in the terminal I was using.

Given a directed graph and two vertices ‘u’ and ‘v’ in it, count all the possible walks from ‘u’ to ‘v’ with exactly k edges on the walk

My code is failing for the following test case , please help
1
10
1 1 1 1 0 0 0 1 0 1
0 1 1 0 0 0 1 1 1 1
0 0 0 1 0 1 1 1 0 1
0 0 0 1 0 0 0 1 1 1
1 1 1 1 1 1 1 1 0 1
0 0 0 0 0 1 0 0 1 0
1 0 1 0 1 0 1 1 1 0
0 1 0 0 0 0 1 0 1 0
0 1 0 1 1 0 0 0 0 1
1 0 1 0 1 1 1 0 1 1
2 7 3
Its Correct output is:
11
And Your Code's output is:
6
Given a directed graph and two vertices ‘u’ and ‘v’ in it, count all the possible walks from ‘u’ to ‘v’ with exactly k edges on the walk.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of three lines.
The first line of each test case is N which is number of vertices in input graph.
The second line of each test case contains N x N binary values that represent graph[N][N].
The third line of each test case contains u, v, k where u is starting position, v is destination and k is number of edges.
Output:
Print all possible walks from 'u' to 'v'.
Constraints:
1 ≤ T ≤ 50
1 ≤ N ≤ 20
0 ≤ graph[][] ≤ 1
Example:
Input
1
4
0 1 1 1
0 0 0 1
0 0 0 1
0 0 0 0
0 3 2
Output
2
Explanation:
For example consider the following graph. Let source ‘u’ be vertex 0, destination ‘v’ be 3 and k be 2. The output should be 2 as there are two walk from 0 to 3 with exactly 2 edges. The walks are {0, 2, 3} and {0, 1, 3}
MY CODE
#include <iostream>
using namespace std;
int main() {
//code
int t;
cin>>t;
while(t--)
{
int r,c;
cin>>r;
c=r;
int arr[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>>arr[i][j];
}
}
int u,v,k;
cin>>u>>v>>k;
int dp[r][k+1];
for(int i=0;i<r;i++)
{
for(int j=0;j<k+1;j++)
{
dp[i][j]=0;
}
}
dp[u][0]=1;
for(int j=0;j<k+1;j++)
{
for(int i=0;i<r;i++)
{
if(dp[i][j]!=0)
{
for(int x=0;x<r;x++)
{
if(arr[i][x]==1)
{if(j+1<k+1)
dp[x][j+1]++;
}
}
}
}
}
cout<<dp[v][k]<<endl;
}
return 0;
}

When I use file as the input of a c++ programs, it Corrupts

I use a file as the input for a c++ programs, and the input is not complete. When I output the results to another file, it meets some problems.
Because of the uncompleted input, I think the programs will shutdown after the program, but it doesn't. It continue running and drop into a loop that never ends.
Here's the src of the program:
#include <stdio.h>
int main()
{
while(1) {
int n,i,j;
int a[100][100]={0};
int row[100]={0};
int col[100]={0};
scanf("%d",&n);
if (n==0) break;
int numr=0,numc=0,cr=0,cc=0;
for (i=0;i<n;i++) {
for (j=0;j<n;j++) {
scanf("%d",&a[i][j]);
row[i]=row[i]+a[i][j];
}
if (row[i]%2!=0) {numr++; cr=i;}
}
for (j=0;j<n;j++) {
for (i=0;i<n;i++) col[j]=col[j]+a[i][j];
if (col[j]%2!=0) {numc++; cc=j;}
}
if ((numr==0)&&(numc==0)) printf("OK\n");
else if ((numr==1)&&(numc==1)) printf("Change bit (%d,%d)\n",cr+1,cc+1);
else printf("Corrupt\n");
}
}
And I use the g++ commond to compile the file
g++ testid.cpp -o testid
The input file is:
99
0 0 1 1 1 1 0 1 0 0 1 1 0 0 1 1 1 1 1 0 0 0 1 1 1 0 1 1 0 0 0 1 0 0 1 1 0 1 1 0 0 0 1 1 1 0 1 1 0 0 1 0 0 1 0 0 0 1 1 1 1 1 1 0 0 1 0 1 1 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 1 1 0 1 0 1 1 0 1 1 0 1 0
And I run the programs:
./testid < sample.in > out
It drops into a never ending loops.
The output file look likes:
Corrupt
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
But I wants the result be null for I have not complete my input.
What is problem? How does it work?
With while(1), you made an infinite loop.
Instead of else printf("Corrupt\n"); try
else {
printf("Corrupt\n");
break ;
}
NB: To clarify, you keep looping and call scanf without any input resulting in
i == 0 and numr == 0 and numc == 0
You probably also want to write an exit statement for when you succeed, but this is not the question you asked.

How to rearrange vector to be cols not rows?

I am solving systems of equations using Armadillo. I make a matrix from one array of doubles, specifying the rows and columns. The problem is that it doesn't read it the way I make the array, (it's a vector but then converted to an array) so I need to manipulate the vector.
To be clear, it takes a vector with these values:
2 0 0 0 2 1 1 1 0 1 1 0 3 0 0 1 1 1 1 0 0 1 0 1 2
And it makes this matrix:
2 1 1 1 0
0 1 0 1 1
0 1 3 1 0
0 0 0 1 1
2 1 0 0 2
But I want this matrix:
2 0 0 0 2
1 1 1 0 1
1 0 3 0 0
1 1 1 1 0
0 1 0 1 2
How do I manipulate my vector to make it like this?
I feel as if you are looking for a transposition of a matrix. There is relevant documentation here.

reading from a txt file to a 2D matrix in C++

I m trying to read from a txt file which is ;
0 1 1 1 0 0 0
1 0 0 0 1 0 0
1 0 0 0 1 1 0
1 0 0 0 0 1 0
0 1 1 0 0 1 0
0 0 1 1 1 0 0
0 0 0 0 0 0 0
and copy it into a 2D matrix. Following code tries to do it
int readFile(int indirectedAdjacencyList[][7])
{
ifstream dPathList;
dPathList.open ("input.txt");
for(int i=0; i<7; i++)
{
for (int j=0; j<7; j++)
{
dPathList >> indirectedAdjacencyList[i][j];
}
}
dPathList.close();
return 0;
}
but it seems there is a problem in my code. in 2D matrix I have only zeros but the size of matrix is ok (7x7) .is there anyone could tell me what is wrong with it ?