Formatting Arrays

This is a discussion on Formatting Arrays within the C Programming forums, part of the General Programming Boards category; I'm practicing printing some arrays and I am having trouble figuring out how to properly print one out. I have ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    2

    Unhappy Formatting Arrays

    I'm practicing printing some arrays and I am having trouble figuring out how to properly print one out. I have attached the assignment I am working on. I have solutions to the problems, but when I try to check my work and print out the arrays I can't get the formatting right.

    Here is what I have for 2)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void) {
        int i, square[ 5 ] [ 5 ] = { 0 };
        
        for ( i = 0; i < 5; i++) {
          for ( i = 0; i < 5; i++) {
            square[ i ] [ i ] = ( i + 1 ) * ( i + 1 );               
            printf("%d", square[ i ] [ i ] );
          }
          printf("\n");   
        }
                  
        system("pause");
        
        return 0;
        
    }

    The output comes out in 1 single line instead of as a square. I thought the printf("\n"); inside the rows loop would fix the issue, but I think the issue might have something to do with the square array not being completely populated in the calculation sequence. I thought that the values not entered in that step would automatically just be zero or empty.

    Please help/offer input. Thanks
    Attached Images Attached Images

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    Write out on paper a 2d array and step through the process of your nested for loop to see exactly where you are going wrong. Hint: You need to index your rows and columns with separate variables.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    2

    Thumbs up whoops nvm

    okay nvm i got it i just didnt recognize it at first because all the zeroes made it look ugly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting using arrays
    By Alienchicken in forum C Programming
    Replies: 12
    Last Post: 02-09-2009, 01:05 AM
  2. formatting a mac
    By DavidP in forum Tech Board
    Replies: 7
    Last Post: 01-17-2007, 10:01 AM
  3. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 05:52 PM
  4. O/I formatting
    By Downbound in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2004, 06:43 AM
  5. Formatting
    By MethodMan in forum Tech Board
    Replies: 6
    Last Post: 10-19-2002, 10:38 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21