Click here to Skip to main content
13,139,569 members (50,010 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I wanna read from one file which will be like this:
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("","r");
    fscanf(fp,"%d", &N);
    for(i=0; i<N; ++i)
             fscanf(fp,"%d %d", &x, &y);
             simeio[x][y] = i;
    for(i=0; i<N; ++i) printf("%d %d", simeio[i][i]);

Where is the problem? Please answer me ASAP.
Thanks for your time.
Posted 10-Feb-12 5:29am
SAKryukov 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?.. :-)
SAKryukov 10-Feb-12 12:06pm
Oh, I see... (sigh) This is beyond repair...
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
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).
SAKryukov 10-Feb-12 12:05pm
What's you problem? You cannot tell true from false? It won't compile, period.
SAKryukov 10-Feb-12 12:05pm
My 5. I would close the case of it.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Many mistakes!

First of all, you have initialized the array using the variable N, before assigning a value to it! Try using
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[^] for information on malloc().

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

Third, you need to correct the code
simeio[x][y] = i;
as following:
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 :)
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
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
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] still doesn't print the right 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.
SAKryukov 10-Feb-12 12:08pm
Do you did not. Calm down. You did not anything good so far. At least take some patience...
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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.
Christian Graus 10-Feb-12 12:14pm
Wow - I missed that one, the rest was so bad.
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

'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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 6

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 28 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100