Writing a for loop

D

danu

Hello all,

How to write a for loop ( or any other loop) to get this kind of a
desired result?

/* al and a2 are int arrays */

a2[0]= a1[5];
a2[1]= a1[6];
a2[2]= a1[7];

thanks. appreciate you'r help.
 
K

Keyser Soze

danu said:
Hello all,

How to write a for loop ( or any other loop) to get this kind of a
desired result?

/* al and a2 are int arrays */

a2[0]= a1[5];
a2[1]= a1[6];
a2[2]= a1[7];

thanks. appreciate you'r help.
Perhaps:

int a1[8], a2[3];

int main(int argc, char *argv[])
{
a1[5] = 0;
a1[6] = 0;
a1[7] = 0;

for(int i=1; i>0; --i)
{
a2[0] = a1[5];
a2[1] = a1[6];
a2[2] = a1[7];
}
}
 
D

Default User

danu said:
Hello all,

How to write a for loop ( or any other loop) to get this kind of a
desired result?

/* al and a2 are int arrays */

a2[0]= a1[5];
a2[1]= a1[6];
a2[2]= a1[7];

thanks. appreciate you'r help.

Do you know how to add? Do you what the difference between 1 and 5 is?
Or 1 and 6?

Try to come with something on your own, that's the only way to learn.



Brian
 
M

Mike Wahler

danu said:
Hello all,

How to write a for loop ( or any other loop) to get this kind of a
desired result?

/* al and a2 are int arrays */

a2[0]= a1[5];
a2[1]= a1[6];
a2[2]= a1[7];

thanks. appreciate you'r help.

int main()
{
int a1[8] = {0};
int a2[3] = {0};

int i = 0;

for(i = 0; i != 42; i = 42)
{
a2[0] = a1[5];
a2[1] = a1[6];
a2[2] = a1[7];
}
return 0;
}

Please have your instructor email me and tell me
my score. (I already know what yours will be).

-Mike
 
B

Barry

Mike Wahler said:
danu said:
Hello all,

How to write a for loop ( or any other loop) to get this kind of a
desired result?

/* al and a2 are int arrays */

a2[0]= a1[5];
a2[1]= a1[6];
a2[2]= a1[7];

thanks. appreciate you'r help.

int main()
{
int a1[8] = {0};
int a2[3] = {0};

int i = 0;

for(i = 0; i != 42; i = 42)
{
a2[0] = a1[5];
a2[1] = a1[6];
a2[2] = a1[7];
}
return 0;
}

Please have your instructor email me and tell me
my score. (I already know what yours will be).

-Mike
The answer is 42.
 
A

August Karlstrom

danu said:
Hello all,

How to write a for loop ( or any other loop) to get this kind of a
desired result?

/* al and a2 are int arrays */

a2[0]= a1[5];
a2[1]= a1[6];
a2[2]= a1[7];

thanks. appreciate you'r help.

If you're stuck at this level, try to switch to a safer (simpler)
language, Java perhaps. C is definitely not a beginner's language.


August
 
I

Ian Malone

August said:
danu said:
Hello all,

How to write a for loop ( or any other loop) to get this kind of a
desired result?

/* al and a2 are int arrays */

a2[0]= a1[5];
a2[1]= a1[6];
a2[2]= a1[7];

thanks. appreciate you'r help.

If you're stuck at this level, try to switch to a safer (simpler)
language, Java perhaps. C is definitely not a beginner's language.

If you're stuck at this level Java is not going to help.

For the OP; the for loop is a way of running a set of
statements multiple times. There are many possible
uses but the simplest is to keep a counter variable
which gets incremented every time the loop is run.
C99 style:

for (int ii=0; ii<some_value;ii++) {
do_something_without(); /* ii */
do_something_with(ii);
}

The first time the loop is run, we have ii=0,
and check ii<some_value, if true we run the loop.
The next time it is run we increment ii, check
ii<some_value and run the loop again. When we
get round to ii==some_value the check will fail
and the loop ends, the loop statements are not
run with ii=some_value.

In C89/ANSI C you must have declared all your
variables at the start of the function, so the
start of the loop looks like:
for (ii=0; ii<some_value; ii++) {

Once the loop has ended in this case ii is left
equal to some_value (but, again, the loop
contents are not run on ii=some_value).

HTH
 
S

Skarmander

Ian said:
August said:
danu said:
Hello all,

How to write a for loop ( or any other loop) to get this kind of a
desired result?

/* al and a2 are int arrays */

a2[0]= a1[5];
a2[1]= a1[6];
a2[2]= a1[7];

thanks. appreciate you'r help.

If you're stuck at this level, try to switch to a safer (simpler)
language, Java perhaps. C is definitely not a beginner's language.

If you're stuck at this level Java is not going to help.

For the OP; the for loop is a way of running a set of
statements multiple times. There are many possible
uses but the simplest is to keep a counter variable
which gets incremented every time the loop is run.
C99 style:

for (int ii=0; ii<some_value;ii++) {
do_something_without(); /* ii */
do_something_with(ii);
}

The first time the loop is run, we have ii=0,
and check ii<some_value, if true we run the loop.
The next time it is run we increment ii, check
ii<some_value and run the loop again. When we
get round to ii==some_value the check will fail
and the loop ends, the loop statements are not
run with ii=some_value.

In C89/ANSI C you must have declared all your
variables at the start of the function, so the
start of the loop looks like:
for (ii=0; ii<some_value; ii++) {

Once the loop has ended in this case ii is left
equal to some_value (but, again, the loop
contents are not run on ii=some_value).

To be atrociously precise, what's guaranteed at the end of the loop is
that ii >= some_value. In this case we know that ii == some_value since
we've neatly incremented the value by 1 every time, but for trickier
increments the difference matters.

S.
 
A

August Karlstrom

Ian said:
August said:
danu said:
Hello all,

How to write a for loop ( or any other loop) to get this kind of a
desired result?

/* al and a2 are int arrays */

a2[0]= a1[5];
a2[1]= a1[6];
a2[2]= a1[7];

thanks. appreciate you'r help.

If you're stuck at this level, try to switch to a safer (simpler)
language, Java perhaps. C is definitely not a beginner's language.

If you're stuck at this level Java is not going to help.

But it will let you know when you are out of bounds.


August
 
M

Mark McIntyre

But it will let you know when you are out of bounds.
So would some thought about the algorithm. And anyway being out of
bounds was the least of the OP's worries. Mention of Java was
egregious.
 
A

August Karlstrom

Mark said:
So would some thought about the algorithm. And anyway being out of
bounds was the least of the OP's worries. Mention of Java was
egregious.

When you start to learn to program you need as much help as you can get
from the programming language/compiler/runtime. Java was just an example
of a well known "safe" language. If I get to choose freely I'd recommend
the less known programming language Oberon.


August
 
C

Christopher Benson-Manica

August Karlstrom said:
When you start to learn to program you need as much help as you can get
from the programming language/compiler/runtime. Java was just an example
of a well known "safe" language. If I get to choose freely I'd recommend
the less known programming language Oberon.

Java's only "safe" if you catch and handle the exceptions properly.
There's no substitute for knowing what you're doing.
 
D

Dennis Willson

Christopher said:
Java's only "safe" if you catch and handle the exceptions properly.
There's no substitute for knowing what you're doing.
Yes, that's true... I programed (still do) in C and C++ for years. I started doing Java and JSP for web applications. I found that a
LOT of programmers don't catch errors except where they're forced to by the compiler and even then sometimes it's an empty catch
statement. I did initially have more problems with NULL pointers after switching to Java than I ever had with C/C++ and Java doesn't
even let you directly do anything with pointers (I think that's a lacking in the language myself IMHO)

I originally learned assembly language (Macro Assembler), then C and so forth, so C wasn't too much of a stretch. I know some people
that just can't get certain concepts regarding program execution and therefore are completely unable to program. A couple of them
are really smart in other areas, just can't get by some things on the computer, no matter how much they really want to.

Actually Pascal was the "training" language wasn't it?

Dennis
 
W

William J. Leary Jr.

August Karlstrom said:
When you start to learn to program you need as much help as you can get
from the programming language/compiler/runtime. Java was just an example
of a well known "safe" language. If I get to choose freely I'd recommend
the less known programming language Oberon.

What a pleasant surprise! Although I make my living in C and assembler, Oberon
is my favorite language (so far, anyway).

- Bill
 
W

Wayne Farmer

danu said:
Hello all,

How to write a for loop ( or any other loop) to get this kind of a
desired result?

/* al and a2 are int arrays */

a2[0]= a1[5];
a2[1]= a1[6];
a2[2]= a1[7];

thanks. appreciate you'r help.

Along with the array-based solutions (and pseudo-solutions) offered, you
should also become familiar with this type of solution:

<homework>
int *s = &a1[5]; /* pointer to source */
int *d = &a2[0]; /* pointer to destination */

for (int i=0; i<3; i++) /* execute this loop 3 times */
*d++ = *s++; /* copy source to destination, and increment
pointers */
</homework>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,927
Members
47,469
Latest member
benny001

Latest Threads

Top