void printarray (int arg[], int length)

// arrays as parameters
#include <iostream.h>

Can someone explain the following code for me.
so u initial n, and when n is less than length
do you cout<< arg[] or
n++??


void printarray (int arg[], int length) {
  for (int n=0; n<length; n++)
    cout << arg[n] << " ";
  cout << "\n";
}

int main ()
{
  int firstarray[] = {5, 10, 15};
  int secondarray[] = {2, 4, 6, 8, 10};
  printarray (firstarray,3);
  printarray (secondarray,5);
  return 0;
}
mikedinh1234Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
The function is simple. All it does is that it prints the elements of the array from index 0 to length - 1. Its a basic for loop. Initally, n is 0. Then, the condition (n < length) is checked. If it is true, you print the value of arg[n] followed by a blank space. Then, the value of n is incremented by the n ++ statement. Again, the condition is checked and the same process repeats as long as the condition is true.

What more do you want to know?
0
SubodhKatiyarCommented:
hi
looks like there is some conf about the working of the loop
this is how it works

your code goes like this -----------------

void printarray (int arg[], int length)
{

  for (int n=0; n<length; n++)
//when code comes to this line for the first time
//it assigns a value zero to n and compares it with the condition
// if condition is true it will go down and do the following..
    cout << arg[n] << " ";
  cout << "\n";
} //now as soon as code encounters this curly braces it will go
// back at the loop construct again and this time it will increment
// n with 1 and compare it with the condition if still true
//it will keep on going in loop,



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikedinh1234Author Commented:
 for (int n=0; n<length; n++)
//when code comes to this line for the first time
//it assigns a value zero to n and compares it with the condition
// if condition is true it will go down and do the following..
    cout << arg[n] << " ";
  cout << "\n";
} //now as soon as code encounters this curly braces it will go
// back at the loop construct again and this time it will increment
// n with 1 and compare it with the condition if still true
//it will keep on going in loop,

WHY would a code be written this way since it confuse the heck out of me?
you do n++ then compare the condition again.

IS there different of writing this code?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

SubodhKatiyarCommented:
yes use a  while loop
like
while(n<length) // you have to initialize n to 0 in the begining
{
cout << arg[n] << " ";
  cout << "\n";
n++; // or you can do n=n+1 or n+=1

}
0
Mayank SAssociate Director - Product EngineeringCommented:
You can use a while loop, do-while loop or whatever you want, but actually, for loops are often used more because of their cleaner syntax. In fact, most authors call it less confusing :-) because you are bound to provide an initialization and an updation condition too, and everything is provided together. If you have a code fragment with a while loop of the type:

n = some_initial_value ;

while ( condition )
{
  // do something
  n = .... ; // some updation

}

there are chances of you forgetting to provide the initialization, or the updation, or both. But the syntax of the for loop is such that it will force you to remember that you have to give an initialization and an updation. (Of course, you also have the option of omitting them if you want.)

for ( n = some_initial_value ; condition ; updation )
{
 // do something

}

And its also easier for you to understand someone else's code (or your own code after a long period of time) because the moment you look at the for statement, you know that from which value to which value (and based on what condition) is the processing to be done. But in case of a while loop, you will have to first search where the initialization and updation statements are written (which becomes difficult in large programs where the length of the code is very long).

Mayank.
0
SubodhKatiyarCommented:
hey m guy
the choice is about the knack and not about the style
there are do loops infinite loops but one can do any thing in a while loop how will you wait for a certain condition in a for next loop like a socket or a flag?
or a stack over run or under run, in while you know do something as long as this while condition is true .
any way the guy got the answer no matter now you beat the track or what .

0
Mayank SAssociate Director - Product EngineeringCommented:
SubodhKatiyar,

Since you're new to EE, I'll not be rude for that (rude) comment of yours. But let me tell you that EE is a forum for people who need help and people who are willing to help. Its not a forum to judge who's better and who's got more technical knowledge. If you scroll up and see the list on your right, you might find my name there, but anyway. Let it be. That's not what I want to explain.

I had no business posting that comment because the question was already answered and you had got the points. But the only reason why I posted that comment was to tell mikedinh1234 not to be scared of for loops, and to show him/ her the advantage of for loops, because they are very commonly used in all programming languages. There is nothing to be scared with its syntax. Of course, I meant only to help him/ her, and did not mean to oppose you. I had nothing against you personally. But if you still wanna beat the track, then go ahead.

Please be more careful while posting such comments again. There are other experts on EE who are much more knowledgable and have a much worse temper and tolerance than I have. EE is not a place for posting rude comments.

Mayank.
0
SubodhKatiyarCommented:
oh i thought you will will take that as a simple comment looks like i touched some where soft,
i am sincerely sorry,
and yes there are people who get offended and i am glad you are not one of them
any way got to go back to work and yes there are many good programmers and that is why i love god so much coz he managed some way to show his intelligence so vividly.
my salutes to one of the smart creatures.
one of the good programmers and good humanbeing mayank
accept please.
0
Mayank SAssociate Director - Product EngineeringCommented:
Its ok, Subodh. Happens sometimes. Since you're new to this forum, I just wanted to warn you. There are lot more experts who take comments very offensively. Sometimes, its that bad that it even results in account-suspension. Just be careful.

Mayank.
0
mikedinh1234Author Commented:
Hey you both are nice people.
So do you get paid for answering the question or for the point?
Can you tell me a little more about EE

again, both of you help me a lot.
so what u guy do for a living???

thanks

mikedinh
0
Mayank SAssociate Director - Product EngineeringCommented:
No, we don't get paid :-) but its still a lot more things.... we are able to help and get the satisfaction of having been able to help, we keep in touch with all the knowledge that we gained (because very often, we lose practice with certain things), we explore more and also increase our own knowledge more by participating in such discussions and questions, etc....

EE is a forum where we achieve all this, and its also like a hobby. I like passing my time answering questions on EE rather than watching TV or movies or doing something else of that sort.... its a personal choice. We only get expert-points for answering the questions, nothing else. The amount of points that you allocate to a question are deducted from your question-points, and the person who answers your question gets those points in his expert-points (often multiplied by a certain factor, depending upon the grade that you give him). e.g., if its a 50-point question, and you give the answer a grade of C, then the person who answered might get 50 expert-points, whereas if the grade is B, he might get 100 experts-points, and if the grade is A, then he might get 200 expert-points. However, in every case, you will lose only 50 points from your question-points.

Now, the available question-points are limited, so you will need to buy more question-points from EE for asking more questions (if you are facing any more problems). That's how EE gets money. However, in order to keep their experts happy, EE also give away more question-points to experts who answer at a good rate (for that, one should have a minimum of 10000 expert-points and should score at least 3000 expert-points every month).

Personally, for a living, I'm a software engineer working in Infosys. I have worked more on J2EE. You can see my profile by clicking on my username.

Mayank.
0
SubodhKatiyarCommented:
hey mayank buddy
finally i landed up with some one from infosys
i was thinking to take the test though for analyst programmer,
i am analyst programmer right now with path infotech limited.
work with vb c++ and mostly dcom and mts stuffs.
i worked on 86 also and saw you too have a knack for it. great knowing you,alas gone are the days when i used to just push my floppy in switch on the system and i have my own custom menu enabled no dos nonsense environment ( all used bios stuffs with track and sector writes reads.)
you know some place where people still work on assembly
regards
0
Mayank SAssociate Director - Product EngineeringCommented:
Nopes, buddy, sorry. Actually, I am also into J2EE stuff. Have left assembly long back.... you can try places like NIIT R&D centre. I guess they are very much into assembly, microcontrollers, etc.
0
SubodhKatiyarCommented:
hey thanks for the tip
i will do that
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.