PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Error in this piece of code
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Error in this piece of code

Réponse
 
LinkBack Outils de la discussion
Vieux 16/07/2008, 15h37   #1
aki
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Error in this piece of code


Hi Frnds ,

Can u reply to my query ..
thanks
Aki

#include<iostream>
using namespace std;
const int NU=10;
struct node {
int data;
node *ptr;
};
int main()
{
int NU;
cout<<"enter nu"<<endl;
cin>>NU;

node* ARR[NU]; // Array of pointers to type node Is this Ok
for(int i=1;i<=NU;i++)
{
ARR[i]=new node;
ARR[i]->data=i;
}
for(int i=1;i<=NU-1;i++)
{
ARR[i]->ptr =ARR[i+1];

}
node *head=ARR[1];
ARR[NU]->ptr=NULL;
node *tmp =head;
while(tmp->ptr!=NULL)
{
static int c=1;
cout<<"node"<<c<<"="<<tmp->data<<endl;
c++;
tmp=tmp->ptr;
}
return 0;
}


output
enter nu
7
node1=1
node2=2
node3=3
node4=4
node5=5
node6=6

Why is 7th node not getting printed???

  Réponse avec citation
Vieux 16/07/2008, 15h47   #2
aki
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error in this piece of code

On Jul 16, 6:37 pm, aki <akhileshrawat...@gmail.com> wrote:
> Hi Frnds ,
>
> Can u reply to my query ..
> thanks
> Aki
>
> #include<iostream>
> using namespace std;
> const int NU=10;
> struct node {
> int data;
> node *ptr;
> };
> int main()
> {
> int NU;
> cout<<"enter nu"<<endl;
> cin>>NU;
>
> node* ARR[NU]; // Array of pointers to type node Is this Ok
> for(int i=1;i<=NU;i++)
> {
> ARR[i]=new node;
> ARR[i]->data=i;
> }
> for(int i=1;i<=NU-1;i++)
> {
> ARR[i]->ptr =ARR[i+1];
>
> }
> node *head=ARR[1];
> ARR[NU]->ptr=NULL;
> node *tmp =head;
> while(tmp->ptr!=NULL)
> {
> static int c=1;
> cout<<"node"<<c<<"="<<tmp->data<<endl;
> c++;
> tmp=tmp->ptr;
> }
> return 0;
>
> }
>
> output
> enter nu
> 7
> node1=1
> node2=2
> node3=3
> node4=4
> node5=5
> node6=6
>
> Why is 7th node not getting printed???


i got sth ...Correction is needed in while condition while(tmp!
=NULL) ...

else is everthing fine in code ....???
  Réponse avec citation
Vieux 16/07/2008, 15h50   #3
Tim Love
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error in this piece of code

aki <akhileshrawat007@gmail.com> writes:


> node* ARR[NU]; // Array of pointers to type node Is this Ok

Not in ANSI C++ because NU isn't a const. g++ by default might compile it.

>Why is 7th node not getting printed???

Because the node with data==7 also has ptr==NULL, so the while condition
stops the while body being run.
  Réponse avec citation
Vieux 16/07/2008, 16h50   #4
Lionel B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error in this piece of code

On Wed, 16 Jul 2008 06:37:00 -0700, aki wrote:

> Hi Frnds ,
>
> Can u reply to my query ..
> thanks
> Aki
>
> #include<iostream>
> using namespace std;
> const int NU=10;


Note that this NU will not be used in `main()', since you define there
another `int U'

> struct node {
> int data;
> node *ptr;
> };
> int main()
> {
> int NU;
> cout<<"enter nu"<<endl;
> cin>>NU;
>
> node* ARR[NU]; // Array of pointers to type node Is this
> Ok


No, not in standard C++, since an array size must be a constant known at
compile time. So your "global" NU defined before `main()' would be ok
here.

> for(int i=1;i<=NU;i++)


Array indices will run from 0 to NU-1, not 1 to NU. So in your code below
you reference (repeatedly) the unallocated location ARR[NU]

> {
> ARR[i]=new node;
> ARR[i]->data=i;
> }
> for(int i=1;i<=NU-1;i++)
> {
> ARR[i]->ptr =ARR[i+1];
>
> }
> node *head=ARR[1];
> ARR[NU]->ptr=NULL;
> node *tmp =head;
> while(tmp->ptr!=NULL)
> {
> static int c=1;
> cout<<"node"<<c<<"="<<tmp->data<<endl; c++;
> tmp=tmp->ptr;
> }
> return 0;
> }
>
>
> output
> enter nu
> 7
> node1=1
> node2=2
> node3=3
> node4=4
> node5=5
> node6=6
>
> Why is 7th node not getting printed???


Because you are indexing your array incorrectly.

--
Lionel B
  Réponse avec citation
Vieux 16/07/2008, 17h22   #5
Puppet_Sock
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error in this piece of code

On Jul 16, 9:37am, aki <akhileshrawat...@gmail.com> wrote:
> #include<iostream>
> using namespace std;


Don't do that. Use the std:: decorator on things from std.

> const int NU=10;
> struct node {
> int data;
> node *ptr;
> };
> int main()
> {
> int NU;
> cout<<"enter nu"<<endl;
> cin>>NU;


Note that this NU hides the NU you declared earlier.


> node* ARR[NU]; // Array of pointers to type node Is this Ok


Note that this should not compile on standard
compliant compiler as NU is not a compile time
constant. If you need values that are chosen
at run time, you need ot be looking at new[]
and delete[].

Your compiler may be allowing the extension.
If so, you should be looking for flags on your
compiler that make it picky about not allowing
extensions. Always make yor compiler as picky
as you can.

> for(int i=1;i<=NU;i++)
> {
> ARR[i]=new node;
> ARR[i]->data=i;
> }


This loop needs to go from 0 to NU-1. Read the
note following.

> for(int i=1;i<=NU-1;i++)
> {
> ARR[i]->ptr =ARR[i+1];
>
> }


Needs to go from 0 to NU-2.

It looks like you are setting up a linked list.
But the elements of that linked list are also
in an array. This looks very strange.

If you want a linked list, you need to be doing
a linked list. (Though you are much better off
learning the standard containers first.) If you
want an array, you need to be doing an array.
Overlapping them like this is whacky.

> node *head=ARR[1];


Needs to be ARR[0]

> ARR[NU]->ptr=NULL;


Needs to be ARR[NU-1]

You need to more carefully read your introductory
text on C++. Arrays go from 0 to dimension minus 1.
So, even if your compiler allowed the extension
previously mentioned, this is wrong. If you enter
7 for NU, the elements of the array would be ARR[0]
through AR[6]. Referencing ARR[7] is a problem.

It looks like (though I can't be sure) you accidentally
"got away with it" here.

But changes to your code might produce weird behaviour.

When you do this line

ARR[i]=new node;

with i == 7, it will be writing to memory outside the
array. You don't want to be doing that.

> node *tmp =head;
> while(tmp->ptr!=NULL)


This is saying, as long as the current node has a next
node, keep going. But when you get to the 7th node,
it does not have a next node.

Should be this.

while(tmp != NULL)

Which means, as long as there is a current node, keep going.
Note that this works even if the linked list is empty.

> {
> static int c=1;
> cout<<"node"<<c<<"="<<tmp->data<<endl;
> c++;


What is the variable c for? It does not seem to get used.

> tmp=tmp->ptr;
> }
> return 0;
>
> }
>
> output
> enter nu
> 7
> node1=1
> node2=2
> node3=3
> node4=4
> node5=5
> node6=6
>
> Why is 7th node not getting printed???


Hope you get it now.
Socks
  Réponse avec citation
Vieux 16/07/2008, 17h50   #6
Pete Becker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error in this piece of code

On 2008-07-16 11:22:29 -0400, Puppet_Sock <puppet_sock@hotmail.com> said:

> On Jul 16, 9:37Âam, aki <akhileshrawat...@gmail.com> wrote:
>> Â Â Â Â int NU;
>> Â Â Â Â cout<<"enter nu"<<endl;
>> Â Â Â Â cin>>NU;
>>     node* ARR[NU]; // Array of pointers to type node  Â

> Â ÂIs this Ok
>
> Note that this should not compile on standard
> compliant compiler as NU is not a compile time
> constant.


The standard requires only that the compiler issue a diagnostic. The
standard never requires that code not compile.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

  Réponse avec citation
Vieux 16/07/2008, 18h55   #7
Mirco Wahab
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Error in this piece of code

aki wrote:
> ...
> node* ARR[NU]; // Array of pointers to type node Is this Ok
> for(int i=1;i<=NU;i++)
> {
> ARR[i]=new node;
> ARR[i]->data=i;
> }
> for(int i=1;i<=NU-1;i++)
> {
> ARR[i]->ptr =ARR[i+1];
>
> }
> node *head=ARR[1];
> ARR[NU]->ptr=NULL;
> node *tmp =head;
> while(tmp->ptr!=NULL)
> {
> static int c=1;
> cout<<"node"<<c<<"="<<tmp->data<<endl;
> c++;
> tmp=tmp->ptr;
> ...
> Why is 7th node not getting printed???


There have been all sorts of corrections and critiques
already in this thread. This should have already answered
your questions. You obviously are trying to learn to
understand linked lists and worked trough it - which is fine.

BTW: your problem shows a "predetermined situation" - where
your number of links and your number of data items is
defined/known before the list is touched.

And: this is (therefoe, imho) *not* a good example for such
a data structure with "records involving pointers". If you
know how many data items you have and you have a simple
"linear chain" through them (in any order), you only need
a single vector to hold that (one vector for data of any
type and one vector of int "indexes") for the linked list.
You simply store in one index vector element (as a value)
the index of the "linked (next) element". This is quite
simple and much more instructive (imho):


...
void foo()
{
int nu;
cout << "enter nu" << endl;
cin >> nu;

vector<int> data_arr(nu); // data vector of size "nu"
vector<int> next_link(nu, -1); // link data, set to END_OF_CHAIN/STOP

for(int i=0; i<nu; i++) { // generate links and some data:
data_arr[i] = 2 * (i+1); // do the data
if(i < nu-1) next_link[i] = i+1; // (!!) keep last node at -1
}

int tmp=0, c=0; // now thats similar to yours:
while(tmp != -1) { // -1 is END_OF_CHAIN
cout << "node" << ++c << "=" << data_arr[tmp] << endl;
tmp = next_link[tmp];
}
}
....

To play with "Arrays of pointers to structures" here would
be (imho) much over the top (BYMMV).

(The above would require an additional include of <vector>)

Regards

M.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 16h51.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17414 seconds with 15 queries