Program doesn't work

When i try to run it says that my program doesnt work anymore?(sry for my bad english).

Problem's condition:
Some pirates have a map that shows where is the treasure.
In the map is written the number of operations(for example one operation says for the pirates to move y steps in the x directions ).
Once the pirates follow all the operations, they find the treasure.
There are two types of of directions, the direction 1 and direction 2.
For 1, (a and b are coordinates, their current coordinates)
if (x[t]==1)
a=a;
b=b+y[t];(y[t] is the number of steps)
For 2,
if (x[t]==2)
a=a;
b=b-y[t];.
You must make a program which shows you where is the tresure, in other words, the final position of the pirates.
You take the place where the pirates are as the coordinates (0 , 0)
Input will be the number of operations=m , then the direction with the steps, then again the direction with the steps.... this is done m times since m is the number of operations.
Output: will be the final coordinates.
Example:
Input: 3 1 2 2 3 1 5(here 1 2, 2 3 and 1 5 are the pairs);
Output: 0 5;
Of course that a will be 0 so dont ask me why i put in general a since it will be always 0.
Well thats because the actual problem has more than 2 direction, but 8 and there a is too changed.

Please show me where is the mistake?
Thanks in advance.

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
26
27
28
29
30
31
32
33
34
35
36
37
#include <fstream>
#include <iostream>

using namespace std;
int a, b;

void finding(int i, int x[], int y[]){
a=0;
b=0;

for (int t = 0; t<i; t++)
{
if (x[t]==1)
 a=a;
 b=b+y[t];
 if (x[t]==2)
  a=a;
 b=b-y[t];}
 
 
}
int main()
{   
 ifstream fin("comori.in");
	ofstream fout("comori.out");
	int m;
	fin>>m;
	int e[m], r[m];
	
	   for (int w = 0 ; w<m; w++ ){
	
	  fin>>e[w] >>r[w];}
	  finding(m, e, r);
	  fout<<a<<b;
	  fout.close ();
	    fin.close ();
	  }
Last edited on
I think you have to declare a file object first.

then try to open it.

something like this.

1
2
 ifstream fin;
 fin.open("comori.in");


or

1
2
ofstream fout;
fout.open("comori.out");



At least that's the only way I've ever done it.


Although that doesn't seem to be your problem. I tried stepping through each line to figure out where it breaks.

And this is what is causing it to stop working. Right after you fin the value of m.

int e[m], int r[m];

I wonder if when a character is read from a file it isn't automatically an integer.
Last edited on
Well can you please repair the program, and say if it works at you.
Also please read the condition of the problem.
I don't think you have to read or write to a file to accomplish the goal of your program.

Did your teacher say you had too?
No.
I am learning for olympiads at informatics. There you need to use file handling.
I take different problems and try to solve them.
I will really appreciate if you can solve it right.
Cause i am trying to solve this problem(EASY PROBLEM) for already a week.
I'll see if I can figure it out. sec.

I think I understand what they are asking.

The only thing I'm unsure of is this part

y[t];(y[t] is the number of steps)


Is y[t] suppose to be an array? if so do you know what the array's values are?

or is y[t] just suppose to be a variable for the number of steps?

If you posted the entire problem it might make more sense. No promises though. I'm pretty new at this stuff myself.
Last edited on
Yes.
You input the arrays y[t].
for example y[2]=2 then they will go 2 steps in that direction
for example (x,y) after that it will be (x,y+2) x and y are coordinates.
Also the maxim number of operations is 40 and of steps(y[i]) 100. Hope it helps.
sure. and you have to use this format?

1
2
3
4
5
6
if (x[t]==1)
 a=a;
 b=b+y[t];
 if (x[t]==2)
  a=a;
 b=b-y[t];}
Last edited on
Well the condition is right.
But the format i did it.
I simplified the problem.
There actually are 8 directions.
And if it works with 2 then it will also with 8.
Last edited on
ok, let me see if i can do something.
I hope this helps, in a way.
Its the problem itself.
http://campion.edu.ro/arhiva/index.php?page=problem&action=view&id=370
Just that its in romanian language.
I'm not sure, but I think this is what they are asking for.

Just for something to keep track of the number of steps yes?

I guess this could work with negative numbers too but I think this would pretty much do what it is suppose to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>


int main(){
   int steps, x = 0, y = 0;
   std::cout << "Please Enter The Number of Steps: ";
   std::cin >> steps;
   int data[steps];
   for (int i = 0; i < (steps*2); i = i + 2){
        std::cout << "\n\nPlease enter the direction for movment " << (i/2)+1 << ". x = 1 or y = 2: ";
        std::cin >> data[i];
        std::cout << "Please enter the number of steps to take for movement " << (i/2)+1 << ". 0 to 9: ";
        std::cin >> data[i+1];
    }

   for (int i = 0; i < (steps*2); i = i + 2){
        if (data[i] == 1){x = x + data[i+1];}
        if (data[i] == 2){y = y + data[i+1];}
   }

   std::cout << "The treasure was buried at coordinates ( " << x << " , " << y << " ).";
   return 0;
}
Last edited on

I'm not sure, but I think this is what they are asking for.

Just for something to keep track of the number of steps yes?

Basicaly yes but it must match the condition as it says.(input and output)
I must use file hanling.
When you send the program to the server , the pc can't input because there is no (file.in) .
Anyway thanks a lot.
It should work the same reading the values from an array or from a file.

The math doesn't change just where the data is stored. The above should be easy to convert to files if you have to use files.
Yes.
Thanks a lot.
Topic archived. No new replies allowed.