Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wanna read from one file which will be like this:
VB
10
1 7
9 7
1 1
9 1
2 4
8 4
6 5
6 3
4 3
4 5


the numbers. The first is the N and tell us how much lines should we read.
After i read them i have to print them back to the user and thats the problem. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int N, simeio[N][N], i, x ,y;
    FILE *fp;
    
    fp = fopen("pulsars.in","r");
    fscanf(fp,"%d", &N);
    
    for(i=0; i<N; ++i)
    {
             fscanf(fp,"%d %d", &x, &y);
             simeio[x][y] = i;
    }
    
    fclose(fp);
    
    for(i=0; i<N; ++i) printf("%d %d", simeio[i][i]);
    
    getchar();
}


Where is the problem? Please answer me ASAP.
Thanks for your time.
Posted
Comments
Sergey Alexandrovich Kryukov 10-Feb-12 12:03pm    
ASAP. Why didn't you tell use what is the problem, before asking where?
If you rush, you are bound to do things again and again. Do you think going things fast can save your time?.. :-)
--SA
Sergey Alexandrovich Kryukov 10-Feb-12 12:06pm    
Oh, I see... (sigh) This is beyond repair...
--SA

This is your home work. So nobody will do it for you here.

However, I have one tip for you:

Compile your code. The compiler error messages will help you to locate most of your problems.
 
Share this answer
 
Comments
XristosMr 10-Feb-12 11:44am    
=.= my compiler doesn't show me any error......
Jochen Arndt 10-Feb-12 11:50am    
There is no C compiler that can compile the above code without errors.
XristosMr 10-Feb-12 11:54am    
its name is Dev-C++
Jochen Arndt 10-Feb-12 12:01pm    
As already said by others, this line can't be compiled:
int N, simeio[N][N], i, x ,y;
Dev-C++ is an IDE using the MinGW port of the GCC (Gnu Compiler Collection).
Sergey Alexandrovich Kryukov 10-Feb-12 12:05pm    
What's you problem? You cannot tell true from false? It won't compile, period.
--SA
Many mistakes!

First of all, you have initialized the array using the variable N, before assigning a value to it! Try using
C++
int * simeio;
(a pointer to int, which will act as an array later) and use malloc() to allocate memory after you have read the value for N from the file. Take a look at www.cplusplus.com/malloc[^] for information on malloc().

Second, according to your file "pulsars.in", it doesn't look like an NxN array. It looks like to me as an Nx2 array. So you can change the following code
C++
int simeio[N][N]
to
C++
int simeio[N][2]
.

Third, you need to correct the code
C++
simeio[x][y] = i;
as following:
C++
simeio[i][0] = x;
simeio[i][1] = y;

Additionally, insert some printf statements to check whether you read the values for N, x, and y from the file correctly. Then you should be OK.

Good luck :)
 
Share this answer
 
v2
Comments
Christian Graus 10-Feb-12 12:13pm    
I didn't offer this much detail because he's doing homework, copying and pasting this will not teach him anything
krumia 12-Feb-12 0:25am    
Agree with you... even I didn't compile. I made a dry run to the code in my head and posted the things came to my mind. Yes I think he should learn a lesson.
Don't ask us to answer ASAP. We do that, by definition, when 'possible' means, when we are inclined to give learners free help.

Do you not have access to a debugger ? Why do you post your code and say 'where is the problem ?' but not tell us WHAT the problem is ?

Where does your code read the first line to get a value for N ? I see lines that won't compile, you also need to define your array when N has a value, and using pointers. You should consider reading your error messages, googling them, and reading your textbooks instead of asking us to do all your work.
 
Share this answer
 
Comments
XristosMr 10-Feb-12 11:43am    
Don't i describe you my problem man? i told you that the wrong is inside the printf so if you can tell me what it is.....just this
Sergey Alexandrovich Kryukov 10-Feb-12 12:08pm    
Do you did not. Calm down. You did not anything good so far. At least take some patience...
--SA
Christian Graus 10-Feb-12 11:46am    
"After i read them i have to print them back to the user and thats the problem" But, what is the problem ? I really am surprised that this builds and runs, because you never set a value to N and because simeio[N][N] was not valid C the last time I used C.
XristosMr 10-Feb-12 11:52am    
ok...lets say that i make it simeio[10][10]...it still doesn't print the right things....do you know why?
Christian Graus 10-Feb-12 11:57am    
I've told you why. If you can't use a debugger or understand my reply, your teacher is the one you need to talk to.
Assuming it does compile somewhere, what do you think this line prints out?

for(i=0; i<N; ++i) printf("%d %d", simeio[i][i]);

Hint, think "diagonal".

I will not comment on the wisdom of using the first input "N" as both the number of items to read in and the limit on the value of each "X" and "Y" read in (otherwise you would exceed the bounds of the array). I'm sure your instructor has some purpose in doing that.

PS, I'm lying in bed, relaxing, reading Code Project. This is as "ASAP" as I get.
 
Share this answer
 
v5
Comments
Christian Graus 10-Feb-12 12:14pm    
Wow - I missed that one, the rest was so bad.
'The problem' is everywhere (and unfortunately it is not the same).
Your first tool is the compiler: it will actively refuse to compile such code (and give you the hints to fix it). The you may use your second tool: the debugger, it will gently show you runtime info about your program, helping you a lot on finding errors.
 
Share this answer
 
Your code shows a complete lack of understanding of basic concepts. That mess should not have compiled in the first place.

int N, simeio[N][N], i, x ,y;
You never initialized N. It will contain garbage. You are then allocating a two-dimensional array sized... well, duh... GARBAGExGARBAGE. This is not how you are supposed to allocate arrays. Moreover, the array will NOT change size when N changes size (which seems to have been your assumption).
Read about pointers, malloc(), calloc() and free().

simeio[x][y] = i;
If you want to store the integers in an array, this is not how you do it.
You are supposed to read N pairs of integers, rather than N pairs of integers no larger than N.

for(i=0; i<n;>Cut-and-paste voodoo programming.
Read about array addressing and prinf() output formatting.

http://www.cplusplus.com/reference/cstdio/
http://www.cplusplus.com/reference/cstdlib/
http://www.cplusplus.com/reference/cstring/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900