Link to home
Start Free TrialLog in
Avatar of mikedinh1234
mikedinh1234

asked on

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;
}
Avatar of Mayank S
Mayank S
Flag of India image

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?
ASKER CERTIFIED SOLUTION
Avatar of SubodhKatiyar
SubodhKatiyar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mikedinh1234
mikedinh1234

ASKER

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

}
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.
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 .

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.
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.
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.
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
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.
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
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.
hey thanks for the tip
i will do that