help with array and loops

Posted on 2007-10-11
Last Modified: 2010-04-15
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
Question by:tsburt
    LVL 2

    Expert Comment

    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++) {

    LVL 2

    Expert Comment

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

    LVL 1

    Author Comment

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

    Accepted Solution

    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.

    LVL 53

    Expert Comment

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

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    734 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

    26 Experts available now in Live!

    Get 1:1 Help Now