Bug in list of matrics represended as arays.....

D

Djuro.Tost

The problem is how to print matrics....
If you are from croatia pldease don't answer mee...

// lista_matrica.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include<stdio.h>
#include<malloc.h>

int ispisi(int *, int, int);

struct matrica{
int m;
int n;
int *A;
struct matrica *next;
};


int main(void)
{ int i, j, k, nn, mm;
int *matr;
char c='d';
struct matrica *pravi_prvi, *prvi, *tekuci;
prvi=(struct matrica*)malloc(sizeof(struct matrica));
tekuci=prvi;
ulaz:
printf("\nEnter a dimensions");
printf(" N=");
scanf("%d",&nn);
printf("\nM=");
scanf("%d",&mm);
tekuci->A=(int *)malloc(sizeof(int)*mm*nn);
tekuci->n=nn;
tekuci->m=mm;
for(i=0;i<nn;i++){
for(j=0;j<mm; j++){
printf("\nA[%d,%d]=",i,j);
scanf("%d",&k);
tekuci->A[i*nn+j]=k;
}
}
printf("\nNext (y/n)");
scanf("%c",&c);
scanf("%c",&c);
if(c!='y') goto izlaz;
else{
tekuci->next=(struct matrica*)malloc(sizeof(struct matrica));
tekuci=tekuci->next;
tekuci->next=NULL;
goto ulaz;
}
izlaz:
tekuci=prvi;
while(tekuci){
ispisi(tekuci->A,tekuci->n,tekuci->m);
tekuci=tekuci->next;
}

return 0;
}



int ispisi(int *A, int nn, int mm){
int i, j;
printf("\n----------------------------\n");
for(i=0;i<nn;i++){
printf("\n");
for(j=0;j<mm;j++){
printf("A[%d,%d]=%d\t%d",i,j, A[i*nn+j],i*nn+j);//here is a bug
when j=0
}
}
return 0;
}
 
D

Djuro.Tost

(e-mail address removed) je napisao/la:
The problem is how to print matrics....
If you are from croatia pldease don't answer mee...

// lista_matrica.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include<stdio.h>
#include<malloc.h>

int ispisi(int *, int, int);

struct matrica{
int m;
int n;
int *A;
struct matrica *next;
};


int main(void)
{ int i, j, k, nn, mm;
int *matr;
char c='d';
struct matrica *pravi_prvi, *prvi, *tekuci;
prvi=(struct matrica*)malloc(sizeof(struct matrica));
tekuci=prvi;
ulaz:
printf("\nEnter a dimensions");
printf(" N=");
scanf("%d",&nn);
printf("\nM=");
scanf("%d",&mm);
tekuci->A=(int *)malloc(sizeof(int)*mm*nn);
tekuci->n=nn;
tekuci->m=mm;
for(i=0;i<nn;i++){
for(j=0;j<mm; j++){
printf("\nA[%d,%d]=",i,j);
scanf("%d",&k);
tekuci->A[i*nn+j]=k;
}
}
printf("\nNext (y/n)");
scanf("%c",&c);
scanf("%c",&c);
if(c!='y') goto izlaz;
else{
tekuci->next=(struct matrica*)malloc(sizeof(struct matrica));
tekuci=tekuci->next;
tekuci->next=NULL;
goto ulaz;
}
izlaz:
tekuci=prvi;
while(tekuci){
ispisi(tekuci->A,tekuci->n,tekuci->m);
tekuci=tekuci->next;
}

return 0;
}



int ispisi(int *A, int nn, int mm){
int i, j;
printf("\n----------------------------\n");
for(i=0;i<nn;i++){
printf("\n");
for(j=0;j<mm;j++){
printf("A[%d,%d]=%d\t%d",i,j, A[i*nn+j],i*nn+j);//here is a bug
when j=0
}
}
return 0;
}



Maybe shorter question is how to print a matrics represented as
arrays...

Thanks in advance, Robert...;)
 
J

Jens Thoms Toerring

The problem is how to print matrics....
If you are from croatia pldease don't answer mee...

I hope very much that with this sentence you destroyed any
chance of getting an answer here.
 
D

Djuro.Tost

Jens Thoms Toerring je napisao/la:
I hope very much that with this sentence you destroyed any
chance of getting an answer here.

Whenewer I put something on croatian news group they hurt mee
wery much so this is a reason why I don't want any help from
croatia...

Thanks, Robert...;)
 
C

CBFalconer

The problem is how to print matrics....
If you are from croatia pldease don't answer mee...

// lista_matrica.cpp : Defines the entry point for the console

Assumes you have a C99 compiler.
application.

illegal syntax
//

#include "stdafx.h"

File not shown. No idea what it contains.
#include<stdio.h>

Missing blank after #include
#include<malloc.h>

No such header file in standard C. Probably wants stdlib.h

Since in the first 7 lines, we find at least 4, probably 5, errors,
it seems pointless to read further. Note that I have counted
errors only once, even though the same error was repeated.
 
K

Keith Thompson

CBFalconer said:
Assumes you have a C99 compiler.

Yes, perhaps he does. Or, more likely, he has a non-C99 compiler that
supports "//" comments as an extension.
illegal syntax

A result of the line being wrapped somewhere between his newsreader
and ours. This can be avoided by not using "//" comments when posting
to Usenet.
File not shown. No idea what it contains.

Not portable, but not an error.
Missing blank after #include

The blank is not required. I do agree that the blank is better style
and makes the line more readable, but it's certainly not an error.
No such header file in standard C. Probably wants stdlib.h

Agreed. (But it's likely that his implementation provides a
Since in the first 7 lines, we find at least 4, probably 5, errors,
it seems pointless to read further. Note that I have counted
errors only once, even though the same error was repeated.

Most of the errors you counted weren't errors.
 
D

Default User

Whenewer I put something on croatian news group they hurt mee
wery much so this is a reason why I don't want any help from
croatia...

This is not a Croatian newsgroup. Your post was offensive and you
deserve no assistance, although I see a few have given you some anyway.




Brian
 
C

CBFalconer

Keith said:
.... snip ...

Agreed. (But it's likely that his implementation provides a


Most of the errors you counted weren't errors.

Since the point of my post was to impress the extent of his
'errors' upon the newbie, why do people have to immediately degrade
any such impression? And <malloc.h> _is_ an error. It makes the
source depend on things not prescribed by the C standard. All you
are saying is that it may not get detected.
 
D

Djuro.Tost

CBFalconer je napisao/la:
Since the point of my post was to impress the extent of his
'errors' upon the newbie, why do people have to immediately degrade
any such impression? And <malloc.h> _is_ an error. It makes the
source depend on things not prescribed by the C standard. All you
are saying is that it may not get detected.

But the programm works compiled with Visual Studio 6.0,
and #include "stdafx.h" is needed...
The only problem is how to print a matric represented as array..

Thanks, Robert...;)
 
D

Djuro.Tost

Richard Heathfield je napisao/la:
CBFalconer said:


Because it's wrong. If they're not errors, why call them errors?


No, it isn't. It's just non-portable and off-topic. That's not quite the
same thing as an error.


So do lots of things. That doesn't make those things errors. For example,
the C standard doesn't prescribe that implementations' source character
sets must support the '@' character - but lots do (I actually know of none
that don't), and a fair amount of source code depends on the '@' character
being supported. To call that an "error" would be stretching the term
further than is useful.


Compilers can't always catch genuine errors, let alone made-up ones.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

But the program works with Visual studio , the:#include "stdafx.h"
is needeed, the only problem is how to print matric represented
as array....

Thanks, Robert...;)
 
D

Djuro.Tost

Richard Heathfield je napisao/la:
(e-mail address removed) said:



No, it isn't.


No, it isn't.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


I use a gcc where #include "stdafx.h"is not needeed,
but Visual Studio wants #include "stdafx.h"....
You coud put him in coment and try to commpile...

Thanks, Robert...;)
My Question is how to print a matric represented as array...
 
A

Antoninus Twink

Richard Heathfield je napisao/la:

My Question is how to print a matric represented as array...

As you can see, the "regulars" here have no interest in answering your
question, but only want to use you as a pawn in their stupid games of
polemics and "topicality".

The problem in your code is here:
for (i = 0; i < nn; i++) {
printf("\n");
for (j = 0; j < mm; j++) {
printf("A[%d,%d]=%d\t%d", i, j, A[i * nn + j], i * nn + j);
}
}

The (i,j)th entry of A should be A[i * mm + j], not A[i * nn + j].
 
C

CBFalconer

CBFalconer je napisao/la:

But the programm works compiled with Visual Studio 6.0,
and #include "stdafx.h" is needed...
The only problem is how to print a matric represented as array..

Visual Studio is not a C compiler, without making appropriate
provisions. This newsgroup discusses the C language, which is
portable to many hardware and software systems. Windows is a tiny
fraction of that environment. If you want Windows advice, go to a
Windows newsgroup.
 
N

Nick Keighley

The problem is how to print matrics....

other people have answered your question, this is just a few points
about
your code.
        prvi=(struct matrica*)malloc(sizeof(struct matrica));

the cast is unnecessary and may hide errors. Your code is easier
to read with more whitespace.

prvi = malloc (sizeof(struct matrica));
or
prvi = malloc (sizeof(*prvi));
or
prvi = malloc (sizeof *prvi);

you do not check if malloc() succeeded
                scanf("%d",&nn);

scanf() is error prone and difficult to use correctly. Better
is fgets() then sscanf() or fgets() and strtol(). You have not
checked scanf() has read anything (check the return value).
                printf("\nM=");

print() may not output the string until a newline is
encountered. Call fflush(stdout)
           if(c!='y') goto izlaz;

goto?! You can often structure your code to avoid this
                   goto ulaz;

that's two gotos to two different labels. Alarm bells
are *really* ringing

<snip>

--
Nick Keighley

"Take guns and rockets and nukes, Sammy.
Lots and lots of nukes. "
Pham Nuwen (a deepness in the sky)
 
J

Joachim Schmitz

Nick said:
other people have answered your question, this is just a few points
about
your code.


the cast is unnecessary and may hide errors.
The OP needs the cast as he's apparently compiling with the C++ part of
Visual C++, the filename lista_matrica.cpp hints that.

RH showed him how to correct that.

Bye, Jojo
 
B

Barry Schwarz

On Thu, 3 Jul 2008 22:25:57 -0700 (PDT), (e-mail address removed) wrote:

snip
But the programm works compiled with Visual Studio 6.0,
and #include "stdafx.h" is needed...

stdafx.h is not needed. I replaced malloc.h with stdlib.h and removed
stdafx.h and Visual Studio 6.0 compiled the program just fine (with
warnings for the two unused variables and the non-standard //
comments).


Remove del for email
 
J

Joachim Schmitz

Richard said:
<OFF-TOPIC>
No, it doesn't have to be like that. Look, it's really easy. Follow
these instructions to set up a new project in Visual C++. The process
looks long-winded but, once you're used to it, it takes only a few
seconds:

1) Start up Visual C++
2) Close any open projects
3) Menu selection: File/New...
4) If the Projects tab is not already selected, select it
5) Select the Win32 Console option in the list box on the left
6) On the right, type in a name for your project (e.g. Hello)
7) If you're not happy with the location, change it
8) Ensure that the Create New Workspace radio button is selected
9) Click OK
10) In the next dialog box, ensure that "An empty project" is selected
11) Click Finish
12) In the next dialog box, click OK
13) Menu selection: File/New...
14) If the Files tab is not already selected, select it
15) Click C++ Source File (we're using C, but bear with me)
16) Type in a name for the source file, ending in .c (e.g. hello.c)
17) Click OK. The dialog box closes, and an editor window opens,
entitled hello.c (or whatever you chose)
18) Menu selection: Project/Settings...
19) Click the C/C++ tab
20) Ensure that the General category is selected from the dropdown
list 21) Change the warning level from 3 to 4
22) In the category dropdown, select "Customize"
23) Put a tick in the "Disable language extensions" checkbox
24) In the category dropdown, select "Precompiled headers"
25) Select the "Not using precompiled headers" radio button
26) Click OK.

And that's it. The dialog closes, and you can type your code into the
hello.c window, and compile it in the usual way.

ALL of this information is off-topic in comp.lang.c, which deals with
the language, not implementations of the language.

</OFF-TOPIC>
Thanks for this anyway, it is slightly diffferent for Visual C++ Express
(and the German version) but it set me on the right track.

Bye, Jojo
 
R

Richard

CBFalconer said:
Visual Studio is not a C compiler, without making appropriate
provisions. This newsgroup discusses the C language, which is
portable to many hardware and software systems. Windows is a tiny
fraction of that environment. If you want Windows advice, go to a
Windows newsgroup.

He wants C programming help. C syntax. C semantics. And this is a C
newsgroup. Why you have to make such a fool of yourself with every post
is anyones guess. Considering so many of the regs here solve bugs in
20,000,000 line code bases without even using a debugger, one would have
thought you and your mighty brain could have spotted the error in his *C
Code*.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top