473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Error in this piece of code

aki

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???

Jul 16 '08 #1
5 1080
aki
On Jul 16, 6:37 pm, aki <akhileshrawat...@gmail.comwrote:
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 ....???
Jul 16 '08 #2
aki <ak**************@gmail.comwrites:

> 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.
Jul 16 '08 #3
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
Jul 16 '08 #4
On Jul 16, 9:37*am, aki <akhileshrawat...@gmail.comwrote:
#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
Jul 16 '08 #5
On 2008-07-16 11:22:29 -0400, Puppet_Sock <pu*********@hotmail.comsaid:
On Jul 16, 9:37Â*am, aki <akhileshrawat...@gmail.comwrote:
>Â* Â* Â* Â* 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)

Jul 16 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sean Abrahams | last post by:
The following is a reprint of a message I sent to the tutor list a long time ago, that I haven't gotten around to discussing with anyone else and failed to hear a reply on the tutor list. Hoping...
15
by: Johan | last post by:
Hi, I've been working on the following piece of code and it works fine in IE and Opera, dut keep getting the same error in Firefox. Can anyone tell me what's wrong with this code? What I'm...
4
by: lynology | last post by:
I need help trying to figure why this piece of code gives me a "Bad File descriptor error" everytime I try to run it and invoke fflush. This piece of code simple outputs a char string to the output...
9
by: Bern | last post by:
the following code produces an error: "The left-hand side of an assignment must be a variable, property or indexer" ----------------------------------------------------------- private void...
9
by: L | last post by:
Hi guys, I am writing some interop code to print graphics to an eps file and I use adobe printer driver. The piece of code I have written is working fine. but if I loop for more than 200 times,...
10
by: bob | last post by:
Hello, I use Microsoft Visual C++ .NET (version 7.1.3088) Sometimes (with big codes?) when I get a compile error and click on the error, the cursor is placed next to the wrong piece of code. The...
1
by: Oenone | last post by:
I have a piece of code (which I can't change) that uses On Error for its error handling. It calls into a piece of code in another assembly that Throws an exception. Is there any way in my On...
2
by: Captain Nemo | last post by:
I'm still using Office 2000 myself, but some of my clients have Office 2003. I've recently added a piece of code to create an instance of Word, open a document, fill in the blanks and become...
0
eboyjr14
by: eboyjr14 | last post by:
I have this xml metacity thing, and it is giving off this error: The file format is invalid. <?xml version="1.0"?> <metacity_theme> <info> <name>Royale</name> <author>Devin...
2
by: nicstel | last post by:
When we run this piece of code, we are getting a following error. output error File "src/lib/parametric/gen_freeze/Tkinter.py", line 1558, in init AttributeError: 'module' object has no...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.