I'm making Dijikstra algorithm in c++ (XCode).
I'm getting exc_bad_access code=1 at that moment:
for (k=0; k<M; k++){
input>>i>>j>>V;
Graf[i][j] = V;
}
Whole code:
#include <iostream>
#include <fstream>
int min(int[]);
int **Graf;
int *Label;
int *Active;
int i, j, k;
int Start, N, M, V, Last;
using namespace std;
int min(int array[]) {
int min, k, min_pos = -1;
min = 32767;
for (k=0; k<M; k++) {
if (array[k] < min && Active[k] == 1){
min = array[k];
min_pos = k;
}
}
return min_pos;
}
int main(){
ifstream input ("input.txt");
input>>N>>M>>Start>>Last;
if (!input){
cout << "File not found" << endl;
return 1;
}
Graf = new int *[N];
for (i=0; i<N; i++){
Graf[i] = new int [N];
}
for (i=0; i<N; i++){
for (j=0; j<N; j++){
Graf[i][j]=0;
}
}
Label = new int [N];
Active = new int [N];
for (i=0; i<N; i++){
Label[i] = 0;
Active[i] = 0;
}
for (k=0; k<M; k++){
input>>i>>j>>V;
Graf[i][j] = V;
}
for (i=0; i<M; i++){
Label[i] = 32767;
Active[Start] = 1;
}
Label[Start] = 0;
i = Start;
do
{
for (j=0; j<N; j++)
if (Graf[i][j] != 0 && Label[j] > Label[i] + Graf[i][j]){
Active[j] = 1;
Label[j] = Label[i] + Graf[i][j];
}
Active[i] = 0;
i=min(Label);
}
while (i != -1);
cout << Label[Last] << endl;
return 0;
}
I'm looking for a mistake for some days, but can't find.
Input.txt
5 9 0 4
0 0 5 2 4 0
0 0 0 7 0 6
5 0 0 9 1 15
2 7 9 0 0 12
4 0 1 0 0 8
0 6 15 12 8 0
I believe your error is in your input file: you read i and j and use it to access your arrays, without verifying that these value are within expected boundaries.
For instance, if you improve your questioned code with an error message, like:
for (k=0; k<M; k++){
input>>i>>j>>V;
if (i>=N || j>=N) {
cerr << "Bad Input for k="<<k<<": "<<i<<","<<j<<","<<V<<endl;
continue;
}
//assert(i<N && j<N); variant if you don't care for the values
Graf[i][j] = V;
}
You'll get the following results:
Bad Input for k=3: 7,0,6
Bad Input for k=4: 5,0,0
Bad Input for k=5: 9,1,15
Bad Input for k=6: 2,7,9
Related
How to read the content of matrix from file to again in the matrix. so I can use it for updating some value in it and again copy this in the matrix ...
In this first, I check that file is empty or not if it is empty then I write a matrix with all values zero, if it is not empty then I read the value from the file and put it into the matrix and then I make some update in it and write it back to the file with updating values...but I unable to read the matrix from file.
Thanks in advance (beginner).
please help me, I tried it so much time but I unable to do it, It is part of my project.
#include <bits/stdc++.h>
using namespace std;
class block{
public:
void inc(int arr[][10])
{ int k;
cout<<"by how much value do you want to increment the array's values"<<endl;
cin>>k;
for(int i=0 ; i<10; i++)
for(int j =0 ; j<10 ; j++)
arr[i][j] = arr[i][j] + k;
for(int i=0 ; i<10; i++)
{
for(int j =0 ; j<10 ; j++)
{
cout<<arr[i][j];
}
cout<<endl;
}
}
void details(int arr[][10])
{
int m1 = 10;
int n1 = 10;
int arr1[10][10];
ifstream fin1;
fin1.open("array.txt", ios::ate);
if (!fin1) {
cerr << strerror(errno) << "\n"; // handle open errors
}
if (fin1.tellg() == 0) {
cout << "NULL" << endl;
ofstream fout1;
fout1.open("array.txt");
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n1; j++) {
fout1 << arr[i][j]; //if file is empty write
//matrix with zero value
arr1[i][j] = arr[i][j];
}
}
fout1.close();
}
else {
fin1 >> m1 >> n1;
for (int i = 0; i < m1; i++) // this is the code of reading of
for (int j = 0; j < n1; j++) //matrix from the file
{
fin1 >> arr[i][j];
arr1[i][j] = arr[i][j];
}
}
fin1.close();
inc(arr1); // updating matrix
ofstream fout2;
fout2.open("array.txt", ios::trunc | ios::out);
for (int i = 0; i < m1; i++)
for (int j = 0; j < n1; j++) { // write back to its file with
//updated value
arr[i][j] = arr1[i][j];
fout2 << arr[i][j];
}
fout2.close();
}
};
int main()
{
block b1;
int arr[10][10];
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
arr[i][j] = 0;
b1.details(arr);
}
Here is an example of how to read a matrix from a file
#include <fstream>
#include <iostream>
#include <cstring>
int main() {
int arr[10][10];
for (std::size_t i = 0; i < 10; i++)
for (std::size_t j = 0; j < 10; j++)
arr[i][j] = 0;
std::size_t m1 = 10;
std::size_t n1 = 10;
std::ifstream fin1("array.txt");
if (!fin1) {
std::cerr << std::strerror(errno) << "\n"; // handle open errors
}
fin1 >> m1 >> n1;
for (std::size_t i = 0; i < m1; i++) // this is the code of reading of
for (std::size_t j = 0; j < n1; j++) { //matrix from the file
fin1 >> arr[i][j];
}
fin1.close();
for (std::size_t i = 0; i < 10; i++) {
for (std::size_t j = 0; j < 10; j++) {
std::cout << arr[i][j] << ' ';
}
std::cout << '\n';
}
}
with input file
10
10
1 2 3 4 5 6 7 8 9 0
11 2 3 4 5 6 7 8 9 0
21 2 3 4 5 6 7 8 9 0
31 2 3 4 5 6 7 8 9 0
41 2 3 4 5 6 7 8 9 0
51 2 3 4 5 6 7 8 9 0
61 2 3 4 5 6 7 8 9 0
71 2 3 4 5 6 7 8 9 0
81 2 3 4 5 6 7 8 9 0
91 2 3 4 5 6 7 8 9 0
I'm trying to find the smallest of the biggest sum of each column of every possible permutations of a given 2D array NxN, where the values in each row can shift towards the left. For example, the array
4 6
3 7
would have 4 possibles permutations:
4 6 6 4 4 6 6 4
3 7 3 7 7 3 7 3
The biggest sum of each permutation is respectively, 13, 11, 11, 13. Thus the smallest of the biggest sums is 11. I have written a recursive function that should work, but for some reason, it only works for arrays that are smaller than 6x6... I'm new at programming, and just recently learned about recursion, any help or counsel on how to think recursively and to debug code would be greatly appreciated...
For the array 4x4
7410 1371 2665 3195
4775 4130 6499 3414
300 2092 4009 7638
5351 210 7225 7207
The answer is 18349, and my code gives me the correct answer.
However, for the array 6x6
5219 842 7793 2098 5109 2621
1372 3253 3804 5652 810 1620
4894 6792 1784 4335 4772 6656
3203 1070 4716 5335 1157 6855
5529 2767 2205 408 7516 7454
375 7036 2597 5288 937 2893
The answer should be 23733, but I've got 24176. How is this possible?
Here's my code:
#include <iostream>
using namespace std;
#define MAX_N 1000
int n, matrix[MAX_N][MAX_N], shift[MAX_N] = {0}, minSum = 100000000;
void possibTree(int position){
//Base case
if(position == n){
for (int i = 0; i < n; i++) {
// Temporary array to store the values in the row that just shifted towards the left
int temp[MAX_N] = {0};
for (int j = 0; j < n; j++) {
if(j - shift[i] < 0)
temp[n+(j-shift[i])] = matrix[i][j];
else
temp[j-shift[i]] = matrix[i][j];
}
for (int k = 0; k < n; k++)
matrix[i][k] = temp[k];
}
int max = 0;
for (int i = 0; i < n; i++) {
int temp = 0;
for (int j = 0; j < n; j++) {
temp += matrix[j][i];
}
if(temp > max)
max = temp;
}
if(minSum > max)
minSum = max;
return;
}
for (int i = 0; i < n; i++) {
shift[position] = i;
possibTree(position+1);
}
return;
}
int main() {
while(cin >> n){
memset(matrix, 0, sizeof(matrix));
memset(shift, 0, sizeof(shift));
if(n == -1) // The user enters "-1" to end the loop and terminate the program.
return 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
possibTree(0);
cout << minSum << endl;
minSum = 100000000;
}
return 0;
}
Ok I believe I understand my mistake, I have to reset the matrix to its original state at the end of each base case, when the matrices are small, the code is still capable of finding all the possible biggest sums, but when the matrices got bigger, some of the possibilities weren't generated. Here's my code:
#include <iostream>
using namespace std;
#define MAX_N 1000
int n, matrix[MAX_N][MAX_N], OrigMatrix[MAX_N][MAX_N], shift[MAX_N] = {0}, minSum = 100000000;
void possibTree(int position){
//Base case
if(position == n){
for (int i = 0; i < n; i++) {
// Temporary array to store the values in the row that just shifted towards the left
int temp[MAX_N] = {0};
for (int j = 0; j < n; j++) {
if(j - shift[i] < 0)
temp[n+(j-shift[i])] = matrix[i][j];
else
temp[j-shift[i]] = matrix[i][j];
}
for (int k = 0; k < n; k++)
matrix[i][k] = temp[k];
}
int max = 0;
for (int i = 0; i < n; i++) {
int temp = 0;
for (int j = 0; j < n; j++) {
temp += matrix[j][i];
}
if(temp > max)
max = temp;
}
if(minSum > max)
minSum = max;
//EDITS
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = OrigMatrix[i][j];
}
}
return;
}
for (int i = 0; i < n; i++) {
shift[position] = i;
possibTree(position+1);
}
return;
}
int main() {
while(cin >> n){
memset(matrix, 0, sizeof(matrix));
memset(shift, 0, sizeof(shift));
if(n == -1) // The user enters "-1" to end the loop and terminate the program.
return 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
//EDITS
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
OrigMatrix[i][j] = matrix[i][j];
}
}
possibTree(0);
cout << minSum << endl;
minSum = 100000000;
}
return 0;
}
I was trying to do the following:
Have a matrix, print the entire thing out, print at end of every row the biggest element of said row and print at the bottom of every column the smallest element of said column.
I'm pretty much a beginner at C++.
So here's what I've done so far:
#include <iostream>
#include <iomanip>
#define M 50
#define N 50
using namespace std;
int main()
{
int m,n;
int a[M][N];
int b[M],c[N];
do {
cout<<"m=";
cin>>m;
cout<<endl<<"n=";
cin>>n;
cout<<endl;
}
while(m!=n);
for(int i=0;i<m; i++) {
for(int j=0; j<n; j++){
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
}
int max_row;
max_row=0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] > max_row) {
max_row = a[i][j];
b[i] = max_row;
}
}
}
for (int i=0; i<m; i++)
{ for(int j=0; j<n; j++){
cout<<setw(3)<<a[i][j]<<"\t";
}
cout<<"|"<<b[i];
cout<<endl;
}
for(int i=0; i<m; i++){
cout<<setw(3)<<"-";}
cout<<endl;
for(int j=0; j<n; j++)
{cout<<c[j]<<"\t";
}
system("pause");
}
Most of the time the max_row are the correct ones such as this case:
3 2 1 |3
4 6 5 |6
7 8 9 |9
Other times they get messed up and it goes like this:
1 2 3 |3
4 33 6 |33
7 8 9 |-858993460
I really have no idea what causes it and since there are no error messages I got really confused. Also I have no idea how to make the min column ones. Any help would be appreciated.
The problem with these loops
max_row=0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] > max_row) {
max_row = a[i][j];
b[i] = max_row;
}
}
}
is that the value of max_row should be initialized with each iteration of the outer loop. Otherwise all rows after the first row deal with the maximum value of the previous row and in general can not have en element that is greater than the current value of max_row. So the corresponding element of the array b will not be initialized.
Also the user can enter for the matrix negative values in this case your program will output zeroes instead of maximum values.
To find maximum elements in rows and minimum elements in columns it is enough to have one pair of nested loops/
Here is a demonstrative program/
#include <iostream>
#include <iomanip>
int main()
{
const size_t N = 3;
int a[N][N] =
{
{ 1, 2, 3 },
{ 4, 33, 6 },
{ 7, 8, 9 }
};
int b[N], c[N];
for ( size_t i = 0; i < N; i++ )
{
b[i] = a[i][0];
c[i] = a[0][i];
for ( size_t j = 1; j < N; j++ )
{
if ( b[i] < a[i][j] ) b[i] = a[i][j];
if ( a[j][i] < c[i] ) c[i] = a[j][i];
}
}
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
std::cout << std::setw( 3 ) << a[i][j] << '\t';
}
std::cout << '|' << b[i] << '\n';
}
for ( size_t i = 0; i < N; i++ )
{
std::cout << std::setw( 3 ) << '-' << '\t';
}
std::cout << '\n';
for ( size_t i = 0; i < N; i++ )
{
std::cout << std::setw( 3 ) << c[i] << '\t';
}
std::cout << '\n';
return 0;
}
Its output is
1 2 3 |3
4 33 6 |33
7 8 9 |9
- - -
1 2 3
I am working on the problem at http://orac.amt.edu.au/cgi-bin/train/problem.pl?set=simple3&problemid=416. Below is the code which I came up. Sample data executed successfully but when I submit the code I am able to execute only 71% of the test cases. Not sure where went wrong. Please guide me.
Briefly the problem is as below:
I am trying to get the max friend list from the friend linking.
1 2
2 3
3 7
1 8
1 9
Output is 1 as it has more friend list
If the friend list match with max the list all.
1 2
2 3
3 7
1 8
9 10
Output should be. 1 2 3 as they have equal number of friends.
#include <bits/stdc++.h>
using namespace std;
int main(){
freopen("listin.txt","r",stdin);
freopen("listout.txt","w",stdout);
int f=0, a= 0, b= 0, m = 1;
long size = 1001;
cin >> f;
int arr[size] = {};
int x[size] = {};
for(int i = 0; i < f; i++){
cin >> a >> b;
arr[a] += 1;
arr[b] += 1;
}
for(int j = 0; j < size; j++){
if(m < arr[j]){
m = arr[j];
}
}
for(int j = 0; j < size; j++){
if(m == arr[j]){
x[j] = j;
}
}
for(int k = 0; k < size; k++){
if(x[k] > 0)
cout << x[k] << endl;
}
return 0;
}
I have following program. with Input 3 5
3 rows
5 growth of numbers
The output should be:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
But my program gives:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Here is what I have tried so far
int main() {
int n, m, c = 0;
cin >> n >> m;
int a[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i][j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << setw(4) << a[i][j];
cout << endl;
}
}
What I am doing wrong or missing?
About the spaces: Can't find reason for such behavior(first spaces are ignored), displayed on screenshot. Tried to run in different IDE's with different compilers and had such problem only in testing system.
Hi try to use tab instead.
#include <iostream>
using namespace std;
int main() {
int n, m, c = 0;
cin >> n >> m;
int *a = new int[n * m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i * n + j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << "\t" << a[i * n + j];
cout << endl;
}
delete[] a;
return 0;
}
Can't remember how I solved this problem in secondary school, but with n less than m, the following code works:
#include <iostream>
using namespace std;
void nextij(long n,long m,long& i,long& j) {
if (i==n-1) { //bottom row
if (j<n-1) { //in the left square
j = i+j+1;
i = 0;
}
else { //out of the left square
i = j-(n-1)+1;
j = m-1;
}
}
else { //other rows
if (j==0) { //left most column
j = i+1;
i = 0;
}
else { //other columns
i++;
j--;
}
}
}
int main() {
long n = 3;
long m = 5;
long a[3][5];
long i = 0;
long j = 0;
long c = 1;
while (c<=n*m) {
a[i][j] = c;
nextij(n,m,i,j);
c++;
}
for (i=0; i<n; i++) {
for (j=0; j<m; j++)
cout <<a[i][j] <<" ";
cout <<endl;
}
}
/*
output:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
*/