Thread: help with the basic's

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    14

    help with the basic's

    Howdy,

    I am just learning the c languge from Sam's Teach yourself C.
    I am on to Understanding Arrays, there is a question I had that was not answered in the book.

    The question was write a program to display the total memory space taken by the array(I understand this part), what I was wondering if you had a array of floating numbers to print the array out would I use a for loop or a while loop, because I cant figure out how to get it to work with a for loop, If i change i to a double or float it still wont work here is my code, And thank you all for your time and your suggestions.

    Code:
    #include <stdio.h>
    
    main() {
        
        double list_data[6] = {
                1.12345,
                2.12345,
                3.12345,
                4.12345,
                5.12345,
                6.12345};
                
        int i, size;
        size = sizeof(list_data);
        printf("The size of list_data is %d\n", size); 
            for (i=0; i<list_data[i]; i++) 
               printf(" %.5f\n", list_data[i]);
                   
            
                
        return (0);
    }
    Last edited by clb2003; 02-09-2009 at 07:07 AM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    So the normal structure for a for loop is:
    Code:
        for(i=0; i<ARRAY_SIZE; ++i)
    in your case array size is 6.

    Similarly to get the ith element in the array for printing, you use list_data[i]
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    If you know how to use while loop than is simple to use a for loop instead of the while loop. The similarities between this 2 loop are this:
    *while loop:
    Code:
    int contor = 0;
    
    while(cnd) {
    /* do smthg */
    contor++;
    }
    *for loop:
    Code:
    int contor;
    
    for (contor = 0; cnd; contor++) {
    /* do smthg */
    }
    The 2 of them are equivalent.
    I have stopped reading Stephen King novels. Now I just read C code instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learned the basics, SO ?
    By TuxPayne in forum C Programming
    Replies: 4
    Last Post: 09-11-2005, 12:32 PM
  2. "Modern C++ and Basics of OO Programming" (e-learning course)
    By Erhard Henkes in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2004, 03:01 PM
  3. C++ Basics part two
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-22-2003, 11:34 AM
  4. Desire & Fear: Two Basics of Human..
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-16-2002, 11:11 PM
  5. The Basics
    By Granger9 in forum Windows Programming
    Replies: 5
    Last Post: 09-13-2002, 05:12 PM