Solved

void printarray (int arg[], int length)

Posted on 2003-11-27
14
798 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
  • 6
  • 6
  • 2
14 Comments
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
 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

by:SubodhKatiyar
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:SubodhKatiyar
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
hey thanks for the tip
i will do that
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sql how to exclude from a table 3 72
Specific format 21 140
stringclean challenge 26 54
mapAB Challlenge 35 84
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now