Solved

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

Posted on 2003-11-27
805 Views
// 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;
}
0
Question by:mikedinh1234
• 6
• 6
• 2

LVL 30

Expert Comment

ID: 9835043
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

LVL 1

Accepted Solution

SubodhKatiyar earned 50 total points
ID: 9842058
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

Author Comment

ID: 9851166
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

LVL 1

Expert Comment

ID: 9856780
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

LVL 30

Expert Comment

ID: 9856871
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

LVL 1

Expert Comment

ID: 9857371
hey m guy
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

LVL 30

Expert Comment

ID: 9857741
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

LVL 1

Expert Comment

ID: 9857898
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
0

LVL 30

Expert Comment

ID: 9863773
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

Author Comment

ID: 9867259
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

LVL 30

Expert Comment

ID: 9872097
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....

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

LVL 1

Expert Comment

ID: 9880133
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

LVL 30

Expert Comment

ID: 9880272
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

LVL 1

Expert Comment

ID: 9880419
hey thanks for the tip
i will do that
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

count7 challenge 12 101
noX challenge 17 91
Export Table to CSV - Access to CSV - using python 18 96
How to obtain Administrator permission when you are the Administrator 6 58
This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …