?
Solved

void printarray (int arg[], int length)

Posted on 2003-11-27
14
Medium Priority
?
837 Views
Last Modified: 2006-11-17
// 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
Comment
Question by:mikedinh1234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
14 Comments
 
LVL 30

Expert Comment

by:Mayank S
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

by:
SubodhKatiyar earned 200 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

by:mikedinh1234
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:SubodhKatiyar
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

by:Mayank S
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

by:SubodhKatiyar
ID: 9857371
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
 
LVL 30

Expert Comment

by:Mayank S
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

by:SubodhKatiyar
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
accept please.
0
 
LVL 30

Expert Comment

by:Mayank S
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

by:mikedinh1234
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

by:Mayank S
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....

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
 
LVL 1

Expert Comment

by:SubodhKatiyar
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

by:Mayank S
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

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

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question