• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

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
  • 3
1 Solution
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).

tsburtAuthor Commented:
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
OK, you are doing your work.... :-)

A few things....

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

2.  you don't need the second for loop and the array "array[t]".  You have array x[t].

3.  There isn't curly braces around at the end for main (after return 0).

A few things.  A variable (e.g. int x;) is simply an allocation of memory.  There is some bit of memory I want to set aside in the computer.  Instead of calling 0xa483727b8, I want to call it x.

An array is setting aside a block of contiguous memory (right next to each other).  I want to have 0xa483727b8 + the next 10 bytes (assuming an int is 1 byte).  I will call that x+0 for my first element (x[0]), x+1 for the next element (x[1]), etc.  However, I really don't want to write:

etc.  That's just boring.  Instead, I want to loop... give me another bit of memory and let's call this one 't' (convention is to use i for a loop, but who cares, it is just a name, right Bob?)

Now I can say,

for (t=0;t>10;t++) {
    x[t] = 23;  //oops, that's not going to work out nicely... ever block
                    //of memory has a 23 in it.

OK, so lets do this.  Gimme some memory (v?, why not that's fun - oh, and I can say it stands for "value").  Let's start v at 23..

int v=23;

now, let re-try that loop....

for (t=0;t>10;t++) {

now, the value stored in memory location 0xadfa28282, that I will call v, gets incremented by one (1) each time I go through the loop.   That's what the bearded man in the front of the classroom wanted....  Who am I to question him :-)

so you have:

int main(void) {
    int x[10];
    int t;
    int v=23

    for (t=0, t<10, t++) {
          x[t] = v++;   // by post incrementing v, you add after assigning it.
    for (t=0, t<10, t++) {
         printf("%d\n",x[t]);  \\ \n is a newline
      return 0;

you need to double-check my syntax - it's been a while :-)

Let me know if the memory conversation made sense.  It is a very important concept in C because you are basically two levels away from computer instructions (machine code).  By the way, I applaud the professor for teaching C.  Most students now learn w/ Java or (egad) C#.  That doesn't get you close enough to the computer's brain.

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


>> 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).

Featured Post


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

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now