Link to home
Start Free TrialLog in
Avatar of tsburt
tsburtFlag for United States of America

asked on

help with array and loops

hey guys trying to start a program that does this...don't know where to start.
it suppose to do these things......

 create an integer array with 10 elements
 use a for loop to fill all 10 array elements
 the contents of the first element of the array should start with the number 23
 the contents of each successive element should be higher than the previous by 1
 use another for loop to print a new line for each element
 each printed line should print the element number and the element value
Avatar of ajcaruso00
ajcaruso00

This sounds like a homework problem.....

Do you have K&R's ANSI C book?  If not go get it.  As far as your code - not gonna write it, but effectively you need:

int v=23;  //my value
int i=0;    // loop control

for (i=0,i<10,i++) {

oops, don't try hitting tab, it will submit....

in side the for loop above, do your assignment to the array.  e.g.

array[i] = v;
v = v +1;
}

You will need to declare the array above and then use the same loop construct to print out.  Again, get K&R's book (it's only ~180 pages and teaches you everything about C).

-T
Avatar of tsburt

ASKER

well it is homework....i'm having problem completing the codes...
as far i have now is....

#include <stdio.h>

int main(void)
{
  int x[10]; /* this declares a 10-integer array */
  int t;

  /* load x with value 0 through 10 */
for (t=0; t<10: ++t) x[t] = 23;
int t=0;    // loop control

for (t=0,t<10, ++t) {
array[t] = v;
v = v +1;
}

/* display contents of x */
for (t=0; t<10; ++t) printf("%d ", x[t]);

return 0;


do u mean like this
ASKER CERTIFIED SOLUTION
Avatar of ajcaruso00
ajcaruso00

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 Infinity08
>> not gonna write it, but effectively you need:

Actually, you did write the code that way ... And then you do it once again in your next post :

>> so you have:

You could have just got him started by giving some general advice on how to use arrays and loops.

tsburt, I suggest that you don't take a look at the full code posted by ajcaruso00, and try to find your own solution. You'll learn a lot more that way.
Take a look at these tutorials on arrays and loops - they might help you understand them better :

        http://www.cplusplus.com/doc/tutorial/arrays.html
        http://www.cplusplus.com/doc/tutorial/control.html


>> 1.  in your for loops, you pre-increment t, post increment it (not a big deal now, but later it will bite you.

pre-increment is better. It doesn't matter either way for an int, but for more complex types (especially in C++), there is a clear advantage to pre-increment over post-increment (no temporary instance needs to be made).