LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2005, 03:23 PM   #1
harrylee2003
LQ Newbie
 
Registered: Nov 2004
Posts: 23

Rep: Reputation: 15
array c++


i have a array like

int data[30];

for(int i =0 ;i<29; i++)
{
data[i] = i;
}

if i want to insert some other num in between

how can i write??

int cerrent = 5;
int used = 29;

for(cerrent; cerrent<used; i++)
{
int tem;
tem= data[used];
data[used +1] = tem;
used - 1;
}

data[cerrent] = newnumber;


i try the way above ....but it didn't work...
 
Old 10-14-2005, 03:35 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
As you declared data to have a fixed size of 30, you can't write a 31th element in it.
You should use alloc/realloc instead.

and all this is really C, not C++
 
Old 10-14-2005, 03:58 PM   #3
harrylee2003
LQ Newbie
 
Registered: Nov 2004
Posts: 23

Original Poster
Rep: Reputation: 15
ic......how about the array is 31.....how can i write the code?
 
Old 10-14-2005, 05:04 PM   #4
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 33
Re: array c++

Quote:
Originally posted by harrylee2003

int cerrent = 5;
int used = 29;

for(cerrent; cerrent<used; i++)
{
int tem;
tem= data[used];
data[used +1] = tem;
used - 1;
}


data[cerrent] = newnumber;


i try the way above ....but it didn't work...
I could not clearly understand what you were trying to do but I would like to point out a few things :
1) The above for loop is basically an infinite loop
2) The general sysntax of a for loop is
Code:
for(initialization expression ; test expression ; update expression) 
{
body of the loop
}
3) Any or all of the expressions might be skipped, and multiple expressions seperated by commas are also allowed.

So lets analyse your for loop
1) The initialisation expression is just cerrent which actually does not mean anything because it does not initialise current to any value. It's as good as left out.
2) The test condition says that the loop should run as long as cerrent < used but nowhere in the loop are cerrect or used modified. Since the initial values of these variables satisfy the test condition the above construct is basically an infinite loop.
3) The update condition i++ is meaningless an nowhere in the loop is i being used.
4) The statement used - 1 does not do anything. It is just an expression that has a value of used - 1. That is to say the statement does not change the value of used.

Maybe you intended doing something like :
Code:
used-- or,
used = used -1 or maybe,
used = used -i

Please be more clear in what your actual problem is. I would recommend you to first gain a thorough understanding of some basic constructs like for, do-while, while and then try them out.
Also, use code tags
Code:
code goes in here
when posting in code.


Hope this helps!

Last edited by koodoo; 10-14-2005 at 05:13 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Array help DropHit Linux - Software 0 06-30-2005 09:31 PM
c++ array init help esrrms Programming 5 05-16-2005 07:06 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM
Array Question? Gerardoj Programming 4 10-02-2003 12:21 AM
2-D Array clowns119 Programming 10 09-24-2003 10:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration