Thread: Loop only printing zeros??

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    Loop only printing zeros??

    I am writing a program for my programming class and I have run into a problem. I got the entire program completed/compiled, but for the output, it's printing only zeros?

    This is the HW problem:

    1. A class of students received scores of 81.4, 75.3, 90.0, 96.9, and 85.6. Declare and initialize an array named “scores” to store these values. Write a loop to display the contents of scores. Make sure that your printout is readable with spaces or commas between each grade. For example:


    The scores are: 81.4 75.3 90 96.9 85.6

    And here is my program:
    Code:
     
    # include <stdio.h>
    
    int main()
    {
            double score[5];
            int i;
    
            score[0]= 81.4;
            score[1]= 75.3;
            score[2]= 90.0;
            score[3]= 96.9;
            score[4]= 85.6;
    
            printf( "The grades are:\n" );
    
            for (i=0; i < sizeof(score)/sizeof(*score); i++)
            {
                    printf("%.1lf,", score[5]);
            }
            printf("\n");
            return 0;
    }
    Thank you in advance for any help!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    quick guess; try "%.1f," instead of "%.1lf,", printf does not want/need the leading "l".
    Code:
    printf("%.1lf,", score[5]);
    Tim S.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Ick, didn't work. This is the output I am getting:

    The grades are:
    0.0,0.0,0.0,0.0,0.0,

    Thank you for your help!

  4. #4
    Charming Registered User
    Join Date
    May 2011
    Posts
    49
    printf("%.1lf,", score[5]);
    If you are the smartest person in the room, you are in the wrong room.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    ardavirus is on the right track. You declare your array to have 5 elements, but arrays in C start at 0, so you only have score[0], score[1], score[2], score[3] and score[4]. score[5] doesn't actually exist, so you're in undefined behavior. Try using your loop variable i as an index.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Yup, I got it! Thanks! It's working now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing an array. (Using a for-loop)
    By Alan Gott in forum C Programming
    Replies: 5
    Last Post: 11-02-2011, 11:36 AM
  2. Printing letters using for loop
    By arighnasarkar in forum C Programming
    Replies: 2
    Last Post: 01-03-2011, 06:21 AM
  3. Loop isn't printing
    By disruptor108 in forum C Programming
    Replies: 5
    Last Post: 06-13-2006, 11:10 AM
  4. Loop printing arrays
    By CHurst in forum C Programming
    Replies: 2
    Last Post: 12-14-2005, 08:13 PM
  5. For loop ; printing records
    By Prezo in forum C Programming
    Replies: 3
    Last Post: 09-15-2002, 06:38 AM